Completed
Push — develop ( 0d5516...ece576 )
by Rémi
05:52
created

SearchResultAdCrawler::getFieldValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 9
nc 2
nop 5
crap 2.0625
1
<?php
2
3
namespace Lbc\Crawler;
4
5
use Lbc\Filter\DefaultSanitizer;
6
use Lbc\Filter\PrixSanitizer;
7
use Lbc\Parser\AdUrlParser;
8
use Lbc\Parser\SearchResultUrlParser;
9
use League\Uri\Schemes\Http;
10
use Symfony\Component\DomCrawler\Crawler;
11
12
/**
13
 * Class SearchResultAdCrawler
14
 * @package Lbc\Crawler
15
 */
16
class SearchResultAdCrawler extends CrawlerAbstract
17
{
18
    /**
19
     * @var AdUrlParser
20
     */
21
    protected $url;
22
23
    /**
24
     * @param $url
25
     * @return SearchResultUrlParser
26
     */
27 10
    protected function setUrlParser($url)
28
    {
29 10
        $this->url = new AdUrlParser($url);
30 10
    }
31
32
    /**
33
     * Return the Ad's ID
34
     *
35
     * @return string
36
     */
37 10
    public function getId()
38
    {
39 10
        return $this->url->getId();
40
    }
41
42
43
    /**
44
     * Return the title
45
     *
46
     * @return string
47
     */
48 10
    public function getTitle()
49
    {
50 10
        return $this->getFieldValue($this->node->filter('h2'), '');
51
    }
52
53
    /**
54
     * Return the price
55
     *
56
     * @return int
57
     */
58 10
    public function getPrice()
59
    {
60 10
        return $this->getFieldValue(
61 10
            $this->node->filter('*[itemprop=price]'),
62 10
            0,
63 10
            PrixSanitizer::class . '::clean'
0 ignored issues
show
Documentation introduced by
\Lbc\Filter\PrixSanitizer::class . '::clean' is of type string, but the function expects a object<Closure>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
        );
65
    }
66
67
    /**
68
     * Return the Ad's URL
69
     *
70
     * @return string
71
     */
72
    public function getUrl()
73
    {
74 10
        return (string)Http::createFromString($this->url)->withScheme('https');
75
    }
76 10
77
    /**
78
     * Return the data and time the ad was created
79
     *
80
     * @return string
81
     */
82
    public function getCreatedAt()
83
    {
84 10
        return $this->node
85
            ->filter('*[itemprop=availabilityStarts]')
86 10
            ->first()
87 10
            ->attr('content');
88 10
    }
89 10
90
    /**
91
     * Return the thumb picture url
92
     *
93
     * @return null|string
94
     */
95
    public function getThumb()
96
    {
97 10
        $image = $this->node
98
            ->filter('.item_imagePic .lazyload[data-imgsrc]')
99 10
            ->first();
100 10
101 10
        if (0 === $image->count()) {
102
            return null;
103 10
        }
104
105
        $src = $image
106
            ->attr('data-imgsrc');
107
108 10
        return (string)Http::createFromString($src)->withScheme('https');
109
    }
110 10
111
    /**
112
     * Return the number of picture of the ad
113
     *
114
     * @return int
115
     */
116
    public function getNbImage()
117
    {
118 10
        $node = $this->node->filter('.item_imageNumber');
119
120 10
        return $this->getFieldValue($node, 0);
121
    }
122
123 10
    /**
124 10
     * @return mixed
125
     */
126 View Code Duplication
    public function getPlacement()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128
        $node = $this->node->filter('*[itemprop=availableAtOrFrom]');
129
130 10
        return $this->getFieldValue($node, '', function ($value) {
131
            return preg_replace('/\s+/', ' ', trim($value));
132 10
        });
133
    }
134
135 10
    /**
136 10
     * @return mixed
137
     */
138 View Code Duplication
    public function getType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $node = $this->node->filter('*[itemprop=category]');
141
142 10
        return $this->getFieldValue($node, false, function ($value) {
143
            if ('pro' === preg_replace('/[\s()]+/', '', $value)) {
144 10
                return 'pro';
145
            }
146 10
147 10
            return 'part';
148 8
        });
149
    }
150
151 10
    /**
152 10
     * @return object
153
     */
154
    public function getAll()
155
    {
156
        return (object)[
157
            'id'            => $this->getId(),
158 10
            'titre'         => $this->getTitle(),
159
            'prix'          => $this->getPrice(),
160
            'url'           => $this->getUrl(),
161 10
            'created_at'    => $this->getCreatedAt(),
162 10
            'images_thumbs' => $this->getThumb(),
163 10
            'nb_image'      => $this->getNbImage(),
164 10
            'placement'     => $this->getPlacement(),
165 10
            'type'          => $this->getType(),
166 10
        ];
167 10
    }
168
}
169