Completed
Push — develop ( 5dcff7...028707 )
by Rémi
05:31
created

SearchResultAdCrawler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 155
Duplicated Lines 12.9 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 13
c 6
b 0
f 0
lcom 2
cbo 5
dl 20
loc 155
ccs 53
cts 53
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUrlParser() 0 4 1
A getId() 0 4 1
A getTitle() 0 4 1
A getUrl() 0 4 1
A getCreatedAt() 0 7 1
A getPrice() 0 10 1
A getThumb() 0 15 2
A getNbImage() 0 6 1
A getPlacement() 8 8 1
A getType() 12 12 2
A getAll() 0 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 14
    protected function setUrlParser($url)
28
    {
29 14
        $this->url = new AdUrlParser($url);
30 14
    }
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
            function ($value) {
64 10
                return PrixSanitizer::clean($value);
65
            }
66 10
        );
67
    }
68
69
    /**
70
     * Return the Ad's URL
71
     *
72
     * @return string
73
     */
74 10
    public function getUrl()
75
    {
76 10
        return (string)Http::createFromString($this->url)->withScheme('https');
77
    }
78
79
    /**
80
     * Return the data and time the ad was created
81
     *
82
     * @return string
83
     */
84 10
    public function getCreatedAt()
85
    {
86 10
        return $this->node
87 10
            ->filter('*[itemprop=availabilityStarts]')
88 10
            ->first()
89 10
            ->attr('content');
90
    }
91
92
    /**
93
     * Return the thumb picture url
94
     *
95
     * @return null|string
96
     */
97 12
    public function getThumb()
98
    {
99 12
        $image = $this->node
100 12
            ->filter('.item_imagePic .lazyload[data-imgsrc]')
101 12
            ->first();
102
103 12
        if (0 === $image->count()) {
104 2
            return null;
105
        }
106
107
        $src = $image
108 10
            ->attr('data-imgsrc');
109
110 10
        return (string)Http::createFromString($src)->withScheme('https');
111
    }
112
113
    /**
114
     * Return the number of picture of the ad
115
     *
116
     * @return int
117
     */
118 12
    public function getNbImage()
119
    {
120 12
        $node = $this->node->filter('.item_imageNumber');
121
122 12
        return $this->getFieldValue($node, 0);
123
    }
124
125
    /**
126
     * @return mixed
127
     */
128 10 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...
129
    {
130 10
        $node = $this->node->filter('*[itemprop=availableAtOrFrom]');
131
132
        return $this->getFieldValue($node, '', function ($value) {
133 10
            return preg_replace('/\s+/', ' ', trim($value));
134 10
        });
135
    }
136
137
    /**
138
     * @return mixed
139
     */
140 10 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...
141
    {
142 10
        $node = $this->node->filter('*[itemprop=category]');
143
144 10
        return $this->getFieldValue($node, false, function ($value) {
145 10
            if ('pro' === preg_replace('/[\s()]+/', '', $value)) {
146 8
                return 'pro';
147
            }
148
149 10
            return 'part';
150 10
        });
151
    }
152
153
    /**
154
     * @return object
155
     */
156 10
    public function getAll()
157
    {
158
        return (object)[
159 10
            'id'            => $this->getId(),
160 10
            'titre'         => $this->getTitle(),
161 10
            'prix'          => $this->getPrice(),
162 10
            'url'           => $this->getUrl(),
163 10
            'created_at'    => $this->getCreatedAt(),
164 10
            'images_thumbs' => $this->getThumb(),
165 10
            'nb_image'      => $this->getNbImage(),
166 10
            'placement'     => $this->getPlacement(),
167 10
            'type'          => $this->getType(),
168 10
        ];
169
    }
170
}
171