Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DefaultListingPipelineSeeder | |
100.00% |
33 / 33 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| ensureForTenant | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
3 | |||
| stageByKey | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Owners\Application\Services; |
| 6 | |
| 7 | use ImovelZapAi\Owners\Domain\Enums\ListingStageKey; |
| 8 | use ImovelZapAi\Owners\Domain\Exceptions\OwnerException; |
| 9 | use ImovelZapAi\Owners\Infrastructure\Persistence\Eloquent\ListingPipelineModel; |
| 10 | use ImovelZapAi\Owners\Infrastructure\Persistence\Eloquent\ListingStageModel; |
| 11 | |
| 12 | final class DefaultListingPipelineSeeder |
| 13 | { |
| 14 | public function ensureForTenant(string $tenantId): ListingPipelineModel |
| 15 | { |
| 16 | $pipeline = ListingPipelineModel::query() |
| 17 | ->where('tenant_id', $tenantId) |
| 18 | ->where('is_default', true) |
| 19 | ->first(); |
| 20 | |
| 21 | if ($pipeline !== null) { |
| 22 | return $pipeline->load('stages'); |
| 23 | } |
| 24 | |
| 25 | $pipeline = ListingPipelineModel::query()->create([ |
| 26 | 'tenant_id' => $tenantId, |
| 27 | 'name' => 'Pipeline de captação', |
| 28 | 'is_default' => true, |
| 29 | ]); |
| 30 | |
| 31 | foreach (ListingStageKey::defaultPipelineDefinition() as $stage) { |
| 32 | ListingStageModel::query()->create([ |
| 33 | 'tenant_id' => $tenantId, |
| 34 | 'pipeline_id' => $pipeline->id, |
| 35 | 'key' => $stage['key'], |
| 36 | 'name' => $stage['name'], |
| 37 | 'position' => $stage['position'], |
| 38 | 'is_won' => $stage['is_won'], |
| 39 | 'is_lost' => $stage['is_lost'], |
| 40 | 'color' => $stage['color'], |
| 41 | ]); |
| 42 | } |
| 43 | |
| 44 | return $pipeline->load('stages'); |
| 45 | } |
| 46 | |
| 47 | public function stageByKey(string $tenantId, string $key, ?string $pipelineId = null): ListingStageModel |
| 48 | { |
| 49 | $pipeline = $pipelineId !== null |
| 50 | ? ListingPipelineModel::query()->where('tenant_id', $tenantId)->whereKey($pipelineId)->firstOrFail() |
| 51 | : $this->ensureForTenant($tenantId); |
| 52 | |
| 53 | $stage = ListingStageModel::query() |
| 54 | ->where('pipeline_id', $pipeline->id) |
| 55 | ->where('key', $key) |
| 56 | ->first(); |
| 57 | |
| 58 | if ($stage === null) { |
| 59 | throw OwnerException::stageNotFound($key); |
| 60 | } |
| 61 | |
| 62 | return $stage; |
| 63 | } |
| 64 | } |