Lines 77.77% 7 / 9
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
CityParser 77.77% 7 / 9 50.00% 1 / 2 5.27 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 extractDistrictUrls 75.00% 6 / 8 0.00% 0 / 1 4.25
1<?php
2
3declare(strict_types=1);
4
5namespace Districts\Scraper\Domain\Krakow;
6
7use Districts\Scraper\Domain\Exception\InvalidHtmlException;
8use Districts\Scraper\Domain\Exception\ParsingException;
9use Districts\Scraper\Domain\HtmlFinder;
10
11readonly class CityParser
12{
13    public function __construct(
14        private HtmlFinder $htmlFinder,
15    ) {
16    }
17
18    /**
19     * @return iterable<string>
20     */
21    public function extractDistrictUrls(string $html): iterable
22    {
23        // phpcs:ignore Generic.Files.LineLength.TooLong
24        $xpath = "//a[contains(@class, 'nav-link') and .='Dzielnice']/following::ul/li/a[@class='nav-link' and starts-with(., 'Dzielnica ')]";
25        try {
26            $nodes = $this->htmlFinder->findNodes($html, $xpath);
27        } catch (InvalidHtmlException) {
28            throw new ParsingException();
29        }
30        if (count($nodes) < 1) {
31            throw new ParsingException();
32        }
33        foreach ($nodes as $node) {
34            yield $this->htmlFinder->getAttribute($node, "href");
35        }
36    }
37}