Lines
85.71%
12 / 14
Functions and Methods
66.66%
2 / 3
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| CityParser | 85.71% | 12 / 14 | 66.66% | 2 / 3 | 7.14 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| extractDistrictUrls | 77.77% | 7 / 9 | 0.00% | 0 / 1 | 4.18 | |||||
| fixPolygonId | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Districts\Scraper\Domain\Gdansk; | |
| 6 | ||
| 7 | use Districts\Scraper\Domain\Exception\InvalidHtmlException; | |
| 8 | use Districts\Scraper\Domain\Exception\ParsingException; | |
| 9 | use Districts\Scraper\Domain\HtmlFinder; | |
| 10 | ||
| 11 | readonly 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 | $xpath = "//polygon[@id]"; | |
| 24 | try { | |
| 25 | $nodes = $this->htmlFinder->findNodes($html, $xpath); | |
| 26 | } catch (InvalidHtmlException) { | |
| 27 | throw new ParsingException(); | |
| 28 | } | |
| 29 | if (count($nodes) < 1) { | |
| 30 | throw new ParsingException(); | |
| 31 | } | |
| 32 | foreach ($nodes as $node) { | |
| 33 | $id = $this->fixPolygonId($this->htmlFinder->getAttribute($node, "id")); | |
| 34 | yield "subpages/dzielnice/html/4-dzielnice_mapa_alert.php?id={$id}"; | |
| 35 | } | |
| 36 | } | |
| 37 | ||
| 38 | private function fixPolygonId(string $id): string | |
| 39 | { | |
| 40 | $match = []; | |
| 41 | if (preg_match("/^([0-9]+)/", $id, $match) !== 1) { | |
| 42 | throw new ParsingException(); | |
| 43 | } | |
| 44 | return $match[1]; | |
| 45 | } | |
| 46 | } |