Lines 100.00% 14 / 14
Methods 100.00% 5 / 5
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getCityName 100.00% 1 / 1 100.00% 1 / 1 1
 scrape 100.00% 4 / 4 100.00% 1 / 1 1
 scrapeDistricts 100.00% 4 / 4 100.00% 1 / 1 2
 scrapeDistrictUrls 100.00% 4 / 4 100.00% 1 / 1 2
15final readonly class CityScraper implements CityScraperInterface
16{
17    public function __construct(
18        private HtmlFetcher $htmlFetcher,
19        private CityParser $cityParser,
20        private DistrictParser $districtParser,
21    ) {
22    }
23
24    public function getCityName(): string
25    {
26        return "GdaƄsk";
27    }
28
29    public function scrape(?ProgressReporter $progressReporter = null): CityDTO
30    {
31        $districtUrls = iterator_to_array($this->scrapeDistrictUrls());
32        $progressReporter?->setTotal(count($districtUrls));
33        $districts = $this->scrapeDistricts($districtUrls, $progressReporter);
34        return new CityDTO($this->getCityName(), iterator_to_array($districts));
35    }
36
37    /**
38     * @param string[] $districtUrls
39     *
40     * @return Iterator<DistrictDTO>
41     */
42    private function scrapeDistricts(array $districtUrls, ?ProgressReporter $progressReporter): Iterator
43    {
44        foreach ($districtUrls as $url) {
45            $districtHtml = $this->htmlFetcher->fetchHtml($url);
46            yield $this->districtParser->parse($districtHtml);
47            $progressReporter?->advance();
48        }
49    }
50
51    /**
52     * @return iterable<string>
53     */
54    private function scrapeDistrictUrls(): iterable
55    {
56        $startUrl = new Url("https://www.gdansk.pl/dzielnice");
57        $startHtml = $this->htmlFetcher->fetchHtml($startUrl->toAsciiString());
58        foreach ($this->cityParser->extractDistrictUrls($startHtml) as $href) {
59            yield $startUrl->resolve($href)->toAsciiString();
60        }
61    }
62}