Lines
94.11%
16 / 17
Functions and Methods
66.66%
2 / 3
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ScraperCollection | 94.11% | 16 / 17 | 66.66% | 2 / 3 | 6.01 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| fromArray | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| filterByCityNames | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 4 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Districts\Scraper\Application; | |
| 6 | ||
| 7 | use Districts\Scraper\Domain\CityScraper; | |
| 8 | use InvalidArgumentException; | |
| 9 | ||
| 10 | readonly class ScraperCollection | |
| 11 | { | |
| 12 | /** | |
| 13 | * @var CityScraper[] | |
| 14 | */ | |
| 15 | private array $scrapers; | |
| 16 | ||
| 17 | public function __construct(CityScraper ...$scrapers) | |
| 18 | { | |
| 19 | $this->scrapers = $scrapers; | |
| 20 | } | |
| 21 | ||
| 22 | /** | |
| 23 | * @param CityScraper[] $scrapers | |
| 24 | */ | |
| 25 | public static function fromArray(array $scrapers): self | |
| 26 | { | |
| 27 | return new self(...$scrapers); | |
| 28 | } | |
| 29 | ||
| 30 | /** | |
| 31 | * @param string[] $names | |
| 32 | * | |
| 33 | * @return CityScraper[] | |
| 34 | */ | |
| 35 | public function filterByCityNames(array $names): array | |
| 36 | { | |
| 37 | if ($names === []) { | |
| 38 | return $this->scrapers; | |
| 39 | } | |
| 40 | ||
| 41 | $supportedNames = array_map( | |
| 42 | static fn ($scraper) => $scraper->getCityName(), | |
| 43 | $this->scrapers, | |
| 44 | ); | |
| 45 | foreach ($names as $name) { | |
| 46 | if (!in_array($name, $supportedNames, true)) { | |
| 47 | throw new InvalidArgumentException("unsupported city name: {$name}"); | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | return array_values( | |
| 52 | array_filter( | |
| 53 | $this->scrapers, | |
| 54 | static fn ($scraper) => in_array($scraper->getCityName(), $names, true), | |
| 55 | ) | |
| 56 | ); | |
| 57 | } | |
| 58 | } |