SearchResultUrlParser   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 174
Duplicated Lines 9.2 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 20
c 3
b 2
f 0
lcom 1
cbo 5
dl 16
loc 174
ccs 52
cts 52
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A next() 8 8 2
A getCategory() 0 10 2
A getSearchArea() 0 12 3
A getLocation() 0 4 1
A getSortType() 0 8 2
A previous() 8 8 2
A getIndexUrl() 0 7 1
A current() 0 14 2
A getNav() 0 11 1
A getType() 0 11 3

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\Parser;
4
5
use League\Uri\Modifiers\MergeQuery;
6
use League\Uri\Modifiers\RemoveQueryKeys;
7
use League\Uri\Schemes\Http;
8
use Psr\Http\Message\UriInterface;
9
10
/**
11
 * Class SearchResultUrlParser
12
 * @package Lbc\Parser
13
 */
14
class SearchResultUrlParser
15
{
16
    /**
17
     * @var Http
18
     */
19
    protected $url;
20
21
    /**
22
     * @var int
23
     */
24
    protected $nbPages;
25
26
    /**
27
     * @param string $url
28
     * @param int $nbPages
29
     */
30 44
    public function __construct($url, $nbPages = 1)
31
    {
32 44
        $this->url = Http::createFromString($url);
33
34 44
        $this->nbPages = $nbPages;
35 44
    }
36
37
    /**
38
     * @return Http
39
     */
40 36
    public function current()
41
    {
42
        // set the default page to 1 unless it is set
43 36
        if (!$this->url->query->hasKey('o')) {
44 24
            $newUrl = new MergeQuery('o=1');
45 24
            $this->url = $newUrl($this->url);
46
        }
47
48
        // remove th (thumb image)
49 36
        $newUrl = new RemoveQueryKeys(['th']);
50 36
        $this->url = $newUrl($this->url);
51
52 36
        return $this->url;
53
    }
54
55
    /**
56
     * Return the next page URL or null if non.
57
     *
58
     * @return UriInterface
59
     */
60 10 View Code Duplication
    public function next()
61
    {
62 10
        if ((int) $this->current()->query->getValue('o') >= $this->nbPages) {
63 2
            return null;
64
        }
65
66 8
        return $this->getIndexUrl(+1);
67
    }
68
69
    /**
70
     * Return the previous page URL or null if none
71
     *
72
     * @return UriInterface
73
     */
74 10 View Code Duplication
    public function previous()
75
    {
76 10
        if ((int) $this->current()->query->getValue('o') === 1) {
77 4
            return null;
78
        }
79
80 8
        return $this->getIndexUrl(-1);
81
    }
82
83
    /**
84
     * @param int $index
85
     *
86
     * @return UriInterface
87
     */
88 10
    public function getIndexUrl($index)
89
    {
90 10
        $oParam = (int) $this->current()->query->getValue('o') + $index;
91 10
        $newQuery = new MergeQuery('o='.$oParam);
92
93 10
        return $newQuery($this->url);
94
    }
95
96
    /**
97
     * Return a meta array containing the nav links and the page
98
     *
99
     * @return array
100
     */
101 6
    public function getNav()
102
    {
103
        return [
104 6
            'page' => (int) $this->current()->query->getValue('o'),
105
            'links' => [
106 6
                'current'  => (string) $this->current(),
107 6
                'previous' => (string) $this->previous(),
108 6
                'next'     => (string) $this->next(),
109
            ]
110
        ];
111
    }
112
113
    /**
114
     * Return the category
115
     *
116
     * @return string
117
     */
118 10
    public function getCategory()
119
    {
120 10
        $category = $this->current()->path->getSegment(0);
121
122 10
        if ($category === 'annonces') {
123 4
            return null;
124
        }
125
126 6
        return $category;
127
    }
128
129
    /**
130
     * Return the search area
131
     *
132
     * @return string
133
     */
134 8
    public function getSearchArea()
135
    {
136 8
        if ($this->current()->path->getSegment(3) === 'occasions') {
137 2
            return 'toute la france';
138
        }
139
140 8
        if ($this->current()->path->getSegment(3) === 'bonnes_affaires') {
141 2
            return 'regions voisines';
142
        }
143
144 8
        return $this->current()->path->getSegment(2);
145
    }
146
147
    /**
148
     * Return one or more location (separated by a comma) or null if none
149
     *
150
     * @return null
151
     */
152 10
    public function getLocation()
153
    {
154 10
        return $this->current()->query->getValue('location', null);
155
    }
156
157
    /**
158
     * Return the type of the ads
159
     *
160
     * @return string all|part|pro
161
     */
162 8
    public function getType()
163
    {
164 8
        switch ($this->current()->query->getValue('f')) {
165 8
            case 'p':
166 2
                return 'part';
167 8
            case 'c':
168 2
                return 'pro';
169
        }
170
171 8
        return 'all';
172
    }
173
174
    /**
175
     * Return the sorting type
176
     *
177
     * @return string price|date
178
     */
179 8
    public function getSortType()
180
    {
181 8
        if ((int) $this->current()->query->getValue('sp') === 1) {
182 2
            return 'price';
183
        }
184
185 8
        return 'date';
186
    }
187
}
188