LeBonCoinParser::parsePhoto()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
ccs 4
cts 7
cp 0.5714
rs 10
cc 3
nc 5
nop 1
crap 3.7085
1
<?php
2
3
namespace App\Parser;
4
5
use App\Enum\Provider;
6
use App\Exception\ParseException;
7
use Exception;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
class LeBonCoinParser extends AbstractParser
11
{
12
    protected const PROVIDER = Provider::LEBONCOIN;
13
14
    protected const SELECTOR_AD_WRAPPER  = 'table[style*="border: solid 1px #e6ebef"]';
15
    protected const SELECTOR_TITLE       = 'td:nth-child(2) > a > span:first-of-type';
16
    protected const SELECTOR_LOCATION    = 'td:nth-child(2) > a > div > span';
17
    protected const SELECTOR_PHOTO       = 'td:nth-child(1) div';
18
19
    /**
20
     * {@inheritDoc}
21
     */
22 1
    protected function parsePhoto(Crawler $crawler): ?string
23
    {
24
        try {
25 1
            $backgroundImage = $crawler->filter(static::SELECTOR_PHOTO)->attr('style');
26
27 1
            if (1 === preg_match('/background-image: url\((.*)\)/', $backgroundImage, $matches)) {
0 ignored issues
show
Bug introduced by
It seems like $backgroundImage can also be of type null; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
            if (1 === preg_match('/background-image: url\((.*)\)/', /** @scrutinizer ignore-type */ $backgroundImage, $matches)) {
Loading history...
28 1
                return $matches[1];
29
            }
30
31
            throw new ParseException('Error while parsing the photo: no property "background-image" found');
32
        } catch (Exception $e) {
33
            throw new ParseException('Error while parsing the photo: ' . $e->getMessage());
34
        }
35
    }
36
}
37