SearchFilter   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Coupling/Cohesion

Components 8
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 27
lcom 8
cbo 1
dl 0
loc 256
ccs 85
cts 85
cp 1
rs 10
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A withToken() 0 6 1
A withinInterval() 0 27 3
A considerImageSearch() 0 6 1
A considerGoogleShoppingSearch() 0 6 1
A considerYoutubeSearch() 0 6 1
A considerNewsSearch() 0 6 1
A considerWebSearch() 0 6 1
A withRisingMetrics() 0 6 1
A withTopMetrics() 0 6 1
A withLanguage() 0 6 1
A withCategory() 0 6 1
A withLocation() 0 6 1
A withSearchTerm() 0 6 1
A getToken() 0 4 1
A getLocation() 0 4 1
A getLanguage() 0 4 1
A getCategory() 0 4 1
A getSearchTerm() 0 4 1
A getSearchType() 0 4 1
A getTime() 0 4 1
A getCompareTime() 0 4 1
A getMetrics() 0 4 1
A isConsideringTopMetrics() 0 4 1
A isConsideringRisingMetrics() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace GSoares\GoogleTrends\Search;
4
5
use DateTimeImmutable;
6
use GSoares\GoogleTrends\Error\GoogleTrendsException;
7
8
/**
9
 * @author Gabriel Felipe Soares <[email protected]>
10
 */
11
class SearchFilter
12
{
13
    public const SEARCH_SOURCE_WEB = '';
14
    public const SEARCH_SOURCE_NEWS = 'news';
15
    public const SEARCH_SOURCE_IMAGES = 'images';
16
    public const SEARCH_SOURCE_YOUTUBE = 'youtube';
17
    public const SEARCH_SOURCE_GOOGLE_SHOPPING = 'froogle';
18
19
    private const DEFAULT_LANG = 'en-US';
20
    private const DEFAULT_COUNTRY = 'US';
21
22
    /**
23
     * @var string
24
     */
25
    private $token;
26
27
    /**
28
     * @var string
29
     */
30
    private $location;
31
32
    /**
33
     * @var string
34
     * @deprecated Will be removed, cause other languages do not work as filter. We should utilize only location
35
     */
36
    private $language;
37
38
    /**
39
     * @var int
40
     */
41
    private $category;
42
43
    /**
44
     * @var string
45
     */
46
    private $searchTerm;
47
48
    /**
49
     * @var string[]
50
     */
51
    private $metrics;
52
53
    /**
54
     * @var string
55
     */
56
    private $time;
57
58
    /**
59
     * @var string
60
     */
61
    private $compareTime;
62
63
    /**
64
     * @var string
65
     */
66
    private $searchType;
67
68
    /**
69
     * @var DateTimeImmutable
70
     */
71
    private $currentDate;
72
73 41
    public function __construct(DateTimeImmutable $currentDate = null)
74
    {
75 41
        $this->metrics = [];
76 41
        $this->searchTerm = '';
77 41
        $this->category = 0;
78 41
        $this->currentDate = $currentDate ?? new DateTimeImmutable();
79
80 41
        $this->withinInterval($this->currentDate->modify('-1 month'), $this->currentDate)
0 ignored issues
show
Deprecated Code introduced by
The method GSoares\GoogleTrends\Sea...hFilter::withLanguage() has been deprecated with message: Will be removed, cause other languages do not work as filter. We should utilize only location

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81 41
            ->withLanguage(self::DEFAULT_LANG)
82 41
            ->withLocation(self::DEFAULT_COUNTRY)
83 41
            ->considerWebSearch();
84 41
    }
85
86 15
    public function withToken(string $token): self
87
    {
88 15
        $this->token = $token;
89
90 15
        return $this;
91
    }
92
93
    /**
94
     * @param DateTimeImmutable $from
95
     * @param DateTimeImmutable $to
96
     *
97
     * @return $this
98
     *
99
     * @throws GoogleTrendsException
100
     */
101 41
    public function withinInterval(DateTimeImmutable $from, DateTimeImmutable $to): self
102
    {
103 41
        if ($from >= $to || $from->format('Ymd') === $to->format('Ymd')) {
104 2
            throw new GoogleTrendsException(
105 2
                sprintf(
106 2
                    'Invalid interval. From %s to %s',
107 2
                    $from->format(DATE_ATOM),
108 2
                    $to->format(DATE_ATOM)
109
                )
110
            );
111
        }
112
113 41
        $from = $from->setTime(0, 0, 0);
114 41
        $to = $to->setTime(23, 59, 50);
115
116 41
        $this->time = $from->format('Y-m-d') . ' ' . $to->format('Y-m-d');
117
118 41
        $daysDifference = (int)ceil(($to->getTimestamp() - $from->getTimestamp()) / 60 / 60 / 24);
119
120 41
        $this->compareTime = $from->modify('-' . $daysDifference . ' days')
121 41
            ->format('Y-m-d')
122 41
            . ' '
123 41
            . $to->modify('-' . $daysDifference . ' days')
124 41
            ->format('Y-m-d');
125
126 41
        return $this;
127
    }
128
129 2
    public function considerImageSearch(): self
130
    {
131 2
        $this->searchType = self::SEARCH_SOURCE_IMAGES;
132
133 2
        return $this;
134
    }
135
136 2
    public function considerGoogleShoppingSearch(): self
137
    {
138 2
        $this->searchType = self::SEARCH_SOURCE_GOOGLE_SHOPPING;
139
140 2
        return $this;
141
    }
142
143 3
    public function considerYoutubeSearch(): self
144
    {
145 3
        $this->searchType = self::SEARCH_SOURCE_YOUTUBE;
146
147 3
        return $this;
148
    }
149
150 2
    public function considerNewsSearch(): self
151
    {
152 2
        $this->searchType = self::SEARCH_SOURCE_NEWS;
153
154 2
        return $this;
155
    }
156
157 41
    public function considerWebSearch(): self
158
    {
159 41
        $this->searchType = self::SEARCH_SOURCE_WEB;
160
161 41
        return $this;
162
    }
163
164 10
    public function withRisingMetrics(): self
165
    {
166 10
        $this->metrics[] = 'RISING';
167
168 10
        return $this;
169
    }
170
171 10
    public function withTopMetrics(): self
172
    {
173 10
        $this->metrics[] = 'TOP';
174
175 10
        return $this;
176
    }
177
178
    /**
179
     * @deprecated Will be removed, cause other languages do not work as filter. We should utilize only location
180
     */
181 41
    public function withLanguage(string $language): self
182
    {
183 41
        $this->language = $language;
0 ignored issues
show
Deprecated Code introduced by
The property GSoares\GoogleTrends\Sea...SearchFilter::$language has been deprecated with message: Will be removed, cause other languages do not work as filter. We should utilize only location

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
184
185 41
        return $this;
186
    }
187
188 9
    public function withCategory(int $category): self
189
    {
190 9
        $this->category = $category;
191
192 9
        return $this;
193
    }
194
195 41
    public function withLocation(string $location): self
196
    {
197 41
        $this->location = $location;
198
199 41
        return $this;
200
    }
201
202 26
    public function withSearchTerm(string $searchTerm): self
203
    {
204 26
        $this->searchTerm = $searchTerm;
205
206 26
        return $this;
207
    }
208
209 15
    public function getToken(): string
210
    {
211 15
        return (string)$this->token;
212
    }
213
214 19
    public function getLocation(): string
215
    {
216 19
        return $this->location;
217
    }
218
219
    /**
220
     * @deprecated Will be removed, cause other languages do not work as filter. We should utilize only location
221
     */
222 19
    public function getLanguage(): string
223
    {
224 19
        return $this->language;
0 ignored issues
show
Deprecated Code introduced by
The property GSoares\GoogleTrends\Sea...SearchFilter::$language has been deprecated with message: Will be removed, cause other languages do not work as filter. We should utilize only location

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
225
    }
226
227 19
    public function getCategory(): int
228
    {
229 19
        return $this->category;
230
    }
231
232 19
    public function getSearchTerm(): string
233
    {
234 19
        return $this->searchTerm;
235
    }
236
237 23
    public function getSearchType(): string
238
    {
239 23
        return $this->searchType;
240
    }
241
242 18
    public function getTime(): string
243
    {
244 18
        return $this->time;
245
    }
246
247 11
    public function getCompareTime(): string
248
    {
249 11
        return $this->compareTime;
250
    }
251
252 3
    public function getMetrics(): array
253
    {
254 3
        return $this->metrics;
255
    }
256
257 7
    public function isConsideringTopMetrics(): bool
258
    {
259 7
        return in_array('TOP', $this->metrics);
260
    }
261
262 9
    public function isConsideringRisingMetrics(): bool
263
    {
264 9
        return in_array('RISING', $this->metrics);
265
    }
266
}
267