Passed
Push — master ( f58ad4...2006a7 )
by Florian
16:38 queued 11s
created

ParuVenduParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 35
ccs 7
cts 10
cp 0.7
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createCrawler() 0 6 1
A parseLocation() 0 14 3
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('/- ([-\s\w]+ \(\d+\))(?:$| -)/', $description, $matches)) {
40 1
            return $matches[1];
41
        }
42
43
        return null;
44
    }
45
}
46