Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
13 / 13 |
|
55.56% |
5 / 9 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| LegoDataProvider | |
100.00% |
11 / 11 |
|
100.00% |
13 / 13 |
|
55.56% |
5 / 9 |
|
100.00% |
3 / 3 |
11.30 | |
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% |
5 / 5 |
|
100.00% |
6 / 6 |
|
50.00% |
2 / 4 |
|
100.00% |
1 / 1 |
4.12 | |||
| findParts | |
100.00% |
5 / 5 |
|
100.00% |
6 / 6 |
|
50.00% |
2 / 4 |
|
100.00% |
1 / 1 |
4.12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\CollectionManagement\Domain\Service; |
| 4 | |
| 5 | use App\CollectionManagement\Domain\Model\PartCollection; |
| 6 | use App\CollectionManagement\Domain\Model\SetCollection; |
| 7 | |
| 8 | /*** |
| 9 | * @author Wilhelm Zwertvaegher |
| 10 | * This provider may be used to search Lego data |
| 11 | * Several loader implementing LegoDataLoader may be defined by the infra and use various sources, |
| 12 | * including cache, local DB, multiple external sources... |
| 13 | * Result MUST be Set[], no matter the source |
| 14 | * As PHP does not allow generics, we rely on custom collections to ensure types are as expected |
| 15 | * |
| 16 | */ |
| 17 | readonly class LegoDataProvider |
| 18 | { |
| 19 | /** |
| 20 | * @param LegoDataLoader[] $legoDataLoaders |
| 21 | */ |
| 22 | public function __construct( |
| 23 | private readonly array $legoDataLoaders, |
| 24 | ) { |
| 25 | } |
| 26 | |
| 27 | public function findSets(string $search): SetCollection |
| 28 | { |
| 29 | // loaders may load from cache, from an external source, or any other source |
| 30 | // we let the infrastructure set the optimal order |
| 31 | foreach ($this->legoDataLoaders as $legoDataLoader) { |
| 32 | $sets = $legoDataLoader->findSets($search); |
| 33 | if (!empty($sets)) { |
| 34 | return $sets; |
| 35 | } |
| 36 | } |
| 37 | return new SetCollection([]); |
| 38 | } |
| 39 | |
| 40 | public function findParts(string $search): PartCollection |
| 41 | { |
| 42 | // loaders may load from cache, from an external source, or any other source |
| 43 | // infrastructure must set them in the optimal order, e.g. cache first, then external source |
| 44 | foreach ($this->legoDataLoaders as $legoDataLoader) { |
| 45 | $parts = $legoDataLoader->findParts($search); |
| 46 | if (!empty($parts)) { |
| 47 | return $parts; |
| 48 | } |
| 49 | } |
| 50 | return new PartCollection([]); |
| 51 | } |
| 52 | } |