ParuVenduParser::createCrawler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Parser;
4
5
use App\Enum\Provider;
6
use Exception;
7
use Symfony\Component\DomCrawler\Crawler;
8
9
class ParuVenduParser extends AbstractParser
10
{
11
    protected const PROVIDER = Provider::PARUVENDU;
12
13
    protected const SELECTOR_AD_WRAPPER = 'table[style*="border:1px solid #d9d8d4"]';
14
    protected const SELECTOR_LOCATION   = 'tr:nth-child(3) td:nth-child(2) span:first-of-type';
15
16
    /**
17
     * {@inheritDoc}
18
     */
19 1
    protected function createCrawler(string $html): Crawler
20
    {
21
        // Fixes the page encoding
22 1
        $html = str_replace('iso-8859-1', 'UTF-8', $html);
23
24 1
        return new Crawler($html);
25
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30 1
    protected function parseLocation(Crawler $crawler): ?string
31
    {
32
        try {
33 1
            $description = trim($crawler->filter(static::SELECTOR_LOCATION)->text());
34
        } catch (Exception) {
35
            return null;
36
        }
37
38
        // E.g.: "- Saint-Herblain (44800)"
39 1
        if (1 === preg_match('/- ((?:\w+(?:(?:-|\s)\w+)?)+ \(\d+\))(?:$| -)/u', $description, $matches)) {
40 1
            return $matches[1];
41
        }
42
43
        return null;
44
    }
45
}
46