Completed
Push — master ( 56f5c3...fdee41 )
by Rémi
15s
created

SearchResultAdCrawler::getIsPro()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Lbc\Crawler;
4
5
use Lbc\Filter\PrixSanitizer;
6
use Lbc\Parser\AdUrlParser;
7
use Lbc\Parser\SearchResultUrlParser;
8
use League\Uri\Schemes\Http;
9
10
/**
11
 * Class SearchResultAdCrawler
12
 * @package Lbc\Crawler
13
 */
14
class SearchResultAdCrawler extends CrawlerAbstract
15
{
16
    /**
17
     * @var AdUrlParser
18
     */
19
    protected $url;
20
21
    /**
22
     * @param $url
23
     * @return SearchResultUrlParser
24
     */
25 14
    protected function setUrlParser($url)
26
    {
27 14
        $this->url = new AdUrlParser($url);
28 14
    }
29
30
    /**
31
     * Return the Ad's ID
32
     *
33
     * @return string
34
     */
35 14
    public function getId()
36 14
    {
37 10
        return $this->url->getId();
38
    }
39
40
41
    /**
42
     * Return the title
43
     *
44
     * @return string
45
     */
46 10
    public function getTitle()
47
    {
48 10
        return $this->getFieldValue($this->node->filter('h2'), '');
49
    }
50
51
    /**
52
     * Return the price
53
     *
54
     * @return int
55
     */
56 10
    public function getPrice()
57
    {
58 10
        return $this->getFieldValue(
59 10
            $this->node->filter('*[itemprop=price]'),
60 10
            0,
61
            function ($value) {
62 10
                return PrixSanitizer::clean($value);
63
            }
64 10
        );
65
    }
66
67
    /**
68
     * Return the Ad's URL
69
     *
70
     * @return string
71
     */
72 10
    public function getUrl()
73
    {
74 10
        return (string)Http::createFromString($this->url)->withScheme('https');
75
    }
76
77
    /**
78
     * Return the data and time the ad was created
79
     *
80
     * @return string
81
     */
82 10
    public function getCreatedAt()
83
    {
84 10
        return $this->node
85 10
            ->filter('*[itemprop=availabilityStarts]')
86 10
            ->first()
87 10
            ->attr('content');
88
    }
89
90
    /**
91
     * Return the thumb picture url
92
     *
93
     * @return null|string
94
     */
95 12
    public function getThumb()
96
    {
97 12
        $image = $this->node
98 12
            ->filter('.item_imagePic .lazyload[data-imgsrc]')
99 12
            ->first();
100
101 12
        if (0 === $image->count()) {
102 2
            return null;
103
        }
104
105
        $src = $image
106 10
            ->attr('data-imgsrc');
107
108 10
        return (string)Http::createFromString($src)->withScheme('https');
109
    }
110
111
    /**
112
     * Return the number of picture of the ad
113
     *
114
     * @return int
115
     */
116 12
    public function getNbImage()
117
    {
118 12
        $node = $this->node->filter('.item_imageNumber');
119
120 12
        return $this->getFieldValue($node, 0);
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126 10
    public function getPlacement()
127
    {
128 10
        $node = $this->node->filter('*[itemprop=availableAtOrFrom]');
129
130
        return $this->getFieldValue($node, '', function ($value) {
131 10
            return preg_replace('/\s+/', ' ', trim($value));
132 10
        });
133
    }
134
135
    /**
136
     * @return mixed
137
     */
138 10
    public function getIsPro()
139
    {
140 10
        return $this->getFieldValue(
141 10
            $this->node->filter('.ispro'),
142 10
            false,
143 8
            function ($value) {
144 8
                return true || $value;
145
            }
146 10
        );
147
    }
148
149
    /**
150
     * @return array
151
     */
152 10
    public function getAll()
153
    {
154
        return [
155 10
            'id'            => $this->getId(),
156 10
            'titre'         => $this->getTitle(),
157 10
            'is_pro'        => $this->getIsPro(),
158 10
            'prix'          => $this->getPrice(),
159 10
            'url'           => $this->getUrl(),
160 10
            'created_at'    => $this->getCreatedAt(),
161 10
            'images_thumbs' => $this->getThumb(),
162 10
            'nb_image'      => $this->getNbImage(),
163 10
            'placement'     => $this->getPlacement(),
164 10
        ];
165
    }
166
}
167