Skip to content

Commit

Permalink
Merge pull request #2 from TimothePearce/refactor-into-projection
Browse files Browse the repository at this point in the history
Refactor into projection
  • Loading branch information
timothepearce authored Nov 23, 2021
2 parents 183f261 + d1caa07 commit d2b778d
Show file tree
Hide file tree
Showing 37 changed files with 361 additions and 335 deletions.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/timothepearce/laravel-cargo/discussions/new?category=q-a
url: https://github.com/timothepearce/laravel-quasar/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/timothepearce/laravel-cargo/discussions/new?category=ideas
url: https://github.com/timothepearce/laravel-quasar/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a bug
url: https://github.com/timothepearce/laravel-cargo/issues/new
url: https://github.com/timothepearce/laravel-quasar/issues/new
about: Report a reproducable bug
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ composer require laravelcargo/laravel-cargo
You can publish and run the migrations with:

```bash
php artisan vendor:publish --provider="Laravelcargo\LaravelCargo\LaravelCargoServiceProvider" --tag="laravel-cargo-migrations"
php artisan vendor:publish --provider="Laravelcargo\LaravelCargo\QuasarServiceProvider" --tag="laravel-cargo-migrations"
php artisan migrate
```

You can publish the config file with:
```bash
php artisan vendor:publish --provider="Laravelcargo\LaravelCargo\LaravelCargoServiceProvider" --tag="laravel-cargo-config"
php artisan vendor:publish --provider="Laravelcargo\LaravelCargo\QuasarServiceProvider" --tag="laravel-cargo-config"
```

This is the contents of the published config file:
Expand Down
20 changes: 12 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"name": "timothepearce/laravel-cargo",
"name": "timothepearce/laravel-quasar",
"description": "This is my package LaravelCargo",
"keywords": [
"LaravelCargo",
"LaravelQuasar",
"laravel-quasar",
"laravel",
"laravel-cargo"
"quasar",
"statistics",
"projections",
"projectors"
],
"homepage": "https://github.com/timothepearce/laravel-cargo",
"homepage": "https://github.com/timothepearce/laravel-quasar",
"license": "MIT",
"authors": [
{
Expand All @@ -29,13 +33,13 @@
},
"autoload": {
"psr-4": {
"Laravelcargo\\LaravelCargo\\": "src"
"TimothePearce\\Quasar\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Laravelcargo\\LaravelCargo\\Tests\\": "tests",
"Laravelcargo\\LaravelCargo\\Tests\\Database\\Factories\\": "tests/database/factories"
"TimothePearce\\Quasar\\Tests\\": "tests",
"TimothePearce\\Quasar\\Tests\\Database\\Factories\\": "tests/database/factories"
}
},
"scripts": {
Expand All @@ -49,7 +53,7 @@
"extra": {
"laravel": {
"providers": [
"Laravelcargo\\LaravelCargo\\LaravelCargoServiceProvider"
"TimothePearce\\Quasar\\QuasarServiceProvider"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
<?php

namespace Laravelcargo\LaravelCargo\Commands;
namespace TimothePearce\Quasar\Commands;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;

class CreateProjectorCommand extends GeneratorCommand
class CreateProjectionCommand extends GeneratorCommand
{
/**
* The name of the console command.
*
* @var string
*/
public $name = 'make:projector';
public $name = 'make:projection';

/**
* The console command description.
*
* @var string
*/
public $description = 'Create a new projector class';
public $description = 'Create a new projection model';

/**
* Get the stub used for the file generation.
*/
protected function getStub()
{
return $this->option('key') ?
__DIR__ . '/stubs/KeyedProjector.php.stub' :
__DIR__ . '/stubs/Projector.php.stub';
__DIR__ . '/stubs/KeyedProjection.php.stub' :
__DIR__ . '/stubs/Projection.php.stub';
}

/**
* Get the default namespace of the generated class.
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Projectors';
return $rootNamespace . '\Models\Projections';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace {{ namespace }};

use Illuminate\Database\Eloquent\Model;
use Laravelcargo\LaravelCargo\Models\Projection;
use Laravelcargo\LaravelCargo\Projector;
use TimothePearce\Quasar\Models\Projection;
use TimothePearce\Quasar\Contracts\ProjectionContract;

class {{ class }} extends Projector
class {{ class }} extends Projection implements ProjectionContract
{
/**
* Lists the time intervals used to compute the projections.
* Lists the available periods.
*
* @var string[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace {{ namespace }};

use Illuminate\Database\Eloquent\Model;
use Laravelcargo\LaravelCargo\Models\Projection;
use Laravelcargo\LaravelCargo\Projector;
use TimothePearce\Quasar\Models\Projection;
use TimothePearce\Quasar\Contracts\ProjectionContract;

class {{ class }} extends Projector
class {{ class }} extends Projection implements ProjectionContract
{
/**
* Lists the time intervals used to compute the projections.
* Lists the available periods.
*
* @var string[]
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Laravelcargo\LaravelCargo\Contracts;
namespace TimothePearce\Quasar\Contracts;

use Illuminate\Database\Eloquent\Model;

interface ProjectorContract
interface ProjectionContract
{
/**
* The default projection content.
Expand All @@ -14,5 +14,5 @@ public static function defaultContent(): array;
/**
* Compute the projection.
*/
public function handle(array $content, Model $model): array;
public static function handle(array $content, Model $model): array;
}
2 changes: 1 addition & 1 deletion src/Exceptions/EmptyProjectionCollectionException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravelcargo\LaravelCargo\Exceptions;
namespace TimothePearce\Quasar\Exceptions;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/MissingProjectionPeriodException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravelcargo\LaravelCargo\Exceptions;
namespace TimothePearce\Quasar\Exceptions;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/MissingProjectorNameException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravelcargo\LaravelCargo\Exceptions;
namespace TimothePearce\Quasar\Exceptions;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/MultiplePeriodsException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravelcargo\LaravelCargo\Exceptions;
namespace TimothePearce\Quasar\Exceptions;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/MultipleProjectorsException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravelcargo\LaravelCargo\Exceptions;
namespace TimothePearce\Quasar\Exceptions;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/OverlappingFillBetweenDatesException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravelcargo\LaravelCargo\Exceptions;
namespace TimothePearce\Quasar\Exceptions;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/ProcessProjection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravelcargo\LaravelCargo\Jobs;
namespace TimothePearce\Quasar\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Projection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravelcargo\LaravelCargo\Models;
namespace TimothePearce\Quasar\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -9,9 +9,9 @@
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Laravelcargo\LaravelCargo\Exceptions\MissingProjectionPeriodException;
use Laravelcargo\LaravelCargo\Exceptions\MissingProjectorNameException;
use Laravelcargo\LaravelCargo\ProjectionCollection;
use TimothePearce\Quasar\Exceptions\MissingProjectionPeriodException;
use TimothePearce\Quasar\Exceptions\MissingProjectorNameException;
use TimothePearce\Quasar\ProjectionCollection;

class Projection extends Model
{
Expand Down
12 changes: 6 additions & 6 deletions src/ProjectionCollection.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace Laravelcargo\LaravelCargo;
namespace TimothePearce\Quasar;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Laravelcargo\LaravelCargo\Exceptions\EmptyProjectionCollectionException;
use Laravelcargo\LaravelCargo\Exceptions\MultiplePeriodsException;
use Laravelcargo\LaravelCargo\Exceptions\MultipleProjectorsException;
use Laravelcargo\LaravelCargo\Exceptions\OverlappingFillBetweenDatesException;
use Laravelcargo\LaravelCargo\Models\Projection;
use TimothePearce\Quasar\Exceptions\EmptyProjectionCollectionException;
use TimothePearce\Quasar\Exceptions\MultiplePeriodsException;
use TimothePearce\Quasar\Exceptions\MultipleProjectorsException;
use TimothePearce\Quasar\Exceptions\OverlappingFillBetweenDatesException;
use TimothePearce\Quasar\Models\Projection;

class ProjectionCollection extends Collection
{
Expand Down
60 changes: 37 additions & 23 deletions src/Projector.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
<?php

namespace Laravelcargo\LaravelCargo;
namespace TimothePearce\Quasar;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Laravelcargo\LaravelCargo\Contracts\ProjectorContract;
use Laravelcargo\LaravelCargo\Models\Projection;
use ReflectionException;
use ReflectionProperty;
use TimothePearce\Quasar\Models\Projection;

abstract class Projector implements ProjectorContract
class Projector
{
/**
* Lists the time intervals used to compute the projections.
*/
protected array $periods;

public function __construct(protected Model $model)
public function __construct(protected Model $projectedModel, protected string $projectionName)
{
}

/**
* The key used to query the projection.
*/
public function key(Model $model): bool | int | string
{
return false;
}

/**
* Parses the periods defined as class attribute.
* @throws ReflectionException
*/
public function parsePeriods(): void
{
collect($this->periods)->each(fn (string $period) => $this->parsePeriod($period));
$periods = (new ReflectionProperty($this->projectionName, 'periods'))->getValue();

collect($periods)->each(fn (string $period) => $this->parsePeriod($period));
}

/**
Expand All @@ -54,25 +50,27 @@ private function parsePeriod(string $period): void
*/
private function findProjection(string $period, int $quantity, string $periodType): Projection | null
{
return Projection::firstWhere([
['projector_name', $this::class],
['key', $this->hasKey() ? $this->key($this->model) : null],
$query = Projection::where([
['projector_name', $this->projectionName],
['key', $this->hasKey() ? $this->key() : null],
['period', $period],
['start_date', Carbon::now()->floorUnit($periodType, $quantity)],
]);

return $query->first();
}

/**
* Creates the projection.
*/
private function createProjection(string $period, int $quantity, string $periodType): void
{
$this->model->projections()->create([
'projector_name' => $this::class,
'key' => $this->hasKey() ? $this->key($this->model) : null,
$this->projectedModel->projections()->create([
'projector_name' => $this->projectionName,
'key' => $this->hasKey() ? $this->key() : null,
'period' => $period,
'start_date' => Carbon::now()->floorUnit($periodType, $quantity),
'content' => $this->handle($this::defaultContent(), $this->model),
'content' => $this->getProjectedContent($this->projectionName::defaultContent()),
]);
}

Expand All @@ -81,7 +79,7 @@ private function createProjection(string $period, int $quantity, string $periodT
*/
private function updateProjection(Projection $projection): void
{
$projection->content = $this->handle($projection->content, $this->model);
$projection->content = $this->getProjectedContent($projection->content);

$projection->save();
}
Expand All @@ -91,6 +89,22 @@ private function updateProjection(Projection $projection): void
*/
private function hasKey(): bool
{
return $this->key($this->model) !== false;
return method_exists($this->projectionName, 'key');
}

/**
* The key used to query the projection.
*/
public function key(): bool | int | string
{
return $this->projectionName::key($this->projectedModel);
}

/**
* Get the projected content.
*/
private function getProjectedContent(array $baseContent): array
{
return $this->projectionName::handle($baseContent, $this->projectedModel);
}
}
Loading

0 comments on commit d2b778d

Please sign in to comment.