Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
4 / 4 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DefaultSetService | |
100.00% |
19 / 19 |
|
100.00% |
4 / 4 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findSets | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\CollectionManagement\Domain\Service; |
| 4 | |
| 5 | use App\CollectionManagement\Domain\Model\EnrichedSet; |
| 6 | use App\CollectionManagement\Domain\Model\EnrichedSetCollection; |
| 7 | use App\CollectionManagement\Domain\Port\Driven\UserSetRepository; |
| 8 | use App\Shared\Domain\Model\EntityId; |
| 9 | use Override; |
| 10 | |
| 11 | readonly class DefaultSetService implements SetService |
| 12 | { |
| 13 | |
| 14 | public function __construct( |
| 15 | private LegoDataProvider $legoDataProvider, |
| 16 | private UserSetRepository $userSetRepository |
| 17 | ) { |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @inheritDoc |
| 22 | */ |
| 23 | #[Override] |
| 24 | public function findSets(string $search, ?EntityId $userId = null): EnrichedSetCollection |
| 25 | { |
| 26 | // get sets from external data provider |
| 27 | $externalSets = $this->legoDataProvider->findSets($search); |
| 28 | // if current user is not set, then we can return found data as is |
| 29 | if ($userId === null) { |
| 30 | return new EnrichedSetCollection( |
| 31 | array_map(fn ($set) => new EnrichedSet($set, null), $externalSets->toArray()) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | // enrich with available user data |
| 36 | // get user's UserSets as an array |
| 37 | $userSets = $this->userSetRepository->findByUserAndExternalIds( |
| 38 | $userId, |
| 39 | array_values(array_map(fn ($set) => $set->getExternalId(), $externalSets->toArray())), |
| 40 | )->toArray(); |
| 41 | |
| 42 | // merge external sets and user sets in a EnrichedSetCollection |
| 43 | return new EnrichedSetCollection( |
| 44 | array_map( |
| 45 | fn ($set) => new EnrichedSet( |
| 46 | $set, |
| 47 | array_find($userSets, fn ($s) => $s->getLocalSet()->getExternalId() === $set->getExternalId()) |
| 48 | ), |
| 49 | $externalSets->toArray() |
| 50 | ) |
| 51 | ); |
| 52 | } |
| 53 | } |