Test Failed
Pull Request — master (#27)
by
unknown
02:03
created

SearchFilter::withResolution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 = 'frgoogle';
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
     */
35
    private $language;
36
37
    /**
38
     * @var int
39
     */
40
    private $category;
41
42
    /**
43
     * @var string
44
     */
45
    private $searchTerm;
46
47
    /**
48
     * @var string[]
49
     */
50
    private $metrics;
51
52
    /**
53
     * @var string
54
     */
55
    private $time;
56
57
    /**
58
     * @var string
59
     */
60
    private $compareTime;
61
62
    /**
63
     * @var string
64
     */
65
    private $searchType;
66
67
    /**
68
     * @var DateTimeImmutable
69
     */
70
    private $currentDate;
71
    
72
    /**
73
     * @var string
74
     */
75
    private $resolution;
76
77 38
    public function __construct(DateTimeImmutable $currentDate = null)
78
    {
79 38
        $this->metrics = [];
80 38
        $this->searchTerm = '';
81 38
        $this->category = 0;
82 38
        $this->currentDate = $currentDate ?? new DateTimeImmutable();
83 38
        $this->resolution = '';
84
85 38
        $this->withinInterval($this->currentDate->modify('-1 month'), $this->currentDate)
86 38
            ->withLanguage(self::DEFAULT_LANG)
87 38
            ->withLocation(self::DEFAULT_COUNTRY)
88 38
            ->considerWebSearch();
89 38
    }
90
91 12
    public function withToken(string $token): self
92
    {
93 12
        $this->token = $token;
94
95 12
        return $this;
96
    }
97
98
    /**
99
     * @param DateTimeImmutable $from
100
     * @param DateTimeImmutable $to
101
     *
102
     * @return $this
103
     *
104
     * @throws GoogleTrendsException
105
     */
106 38
    public function withinInterval(DateTimeImmutable $from, DateTimeImmutable $to): self
107
    {
108 38
        if ($from >= $to || $from->format('Ymd') === $to->format('Ymd')) {
109 2
            throw new GoogleTrendsException(
110 2
                sprintf(
111 2
                    'Invalid interval. From %s to %s',
112 2
                    $from->format(DATE_ATOM),
113 2
                    $to->format(DATE_ATOM)
114
                )
115
            );
116
        }
117
118 38
        $from = $from->setTime(0, 0, 0);
119 38
        $to = $to->setTime(23, 59, 50);
120
121 38
        $this->time = $from->format('Y-m-d') . ' ' . $to->format('Y-m-d');
122
123 38
        $daysDifference = (int)ceil(($to->getTimestamp() - $from->getTimestamp()) / 60 / 60 / 24);
124
125 38
        $this->compareTime = $from->modify('-' . $daysDifference . ' days')
126 38
            ->format('Y-m-d')
127 38
            . ' '
128 38
            . $to->modify('-' . $daysDifference . ' days')
129 38
            ->format('Y-m-d');
130
131 38
        return $this;
132
    }
133
134 2
    public function considerImageSearch(): self
135
    {
136 2
        $this->searchType = self::SEARCH_SOURCE_IMAGES;
137
138 2
        return $this;
139
    }
140
141 2
    public function considerGoogleShoppingSearch(): self
142
    {
143 2
        $this->searchType = self::SEARCH_SOURCE_GOOGLE_SHOPPING;
144
145 2
        return $this;
146
    }
147
148 3
    public function considerYoutubeSearch(): self
149
    {
150 3
        $this->searchType = self::SEARCH_SOURCE_YOUTUBE;
151
152 3
        return $this;
153
    }
154
155 2
    public function considerNewsSearch(): self
156
    {
157 2
        $this->searchType = self::SEARCH_SOURCE_NEWS;
158
159 2
        return $this;
160
    }
161
162 38
    public function considerWebSearch(): self
163
    {
164 38
        $this->searchType = self::SEARCH_SOURCE_WEB;
165
166 38
        return $this;
167
    }
168
169 10
    public function withRisingMetrics(): self
170
    {
171 10
        $this->metrics[] = 'RISING';
172
173 10
        return $this;
174
    }
175
176 10
    public function withTopMetrics(): self
177
    {
178 10
        $this->metrics[] = 'TOP';
179
180 10
        return $this;
181
    }
182
183 38
    public function withLanguage(string $language): self
184
    {
185 38
        $this->language = $language;
186
187 38
        return $this;
188
    }
189
190 9
    public function withCategory(int $category): self
191
    {
192 9
        $this->category = $category;
193
194 9
        return $this;
195
    }
196
197 38
    public function withLocation(string $location): self
198
    {
199 38
        $this->location = $location;
200
201 38
        return $this;
202
    }
203
204 23
    public function withSearchTerm(string $searchTerm): self
205
    {
206 23
        $this->searchTerm = $searchTerm;
207
208 23
        return $this;
209
    }
210
211 12
    public function getToken(): string
212
    {
213 12
        return (string)$this->token;
214
    }
215
216 16
    public function getLocation(): string
217
    {
218 16
        return $this->location;
219
    }
220
221 16
    public function getLanguage(): string
222
    {
223 16
        return $this->language;
224
    }
225
226 16
    public function getCategory(): int
227
    {
228 16
        return $this->category;
229
    }
230
231 16
    public function getSearchTerm(): string
232
    {
233 16
        return $this->searchTerm;
234
    }
235
236 23
    public function getSearchType(): string
237
    {
238 23
        return $this->searchType;
239
    }
240
241 15
    public function getTime(): string
242
    {
243 15
        return $this->time;
244
    }
245
246 11
    public function getCompareTime(): string
247
    {
248 11
        return $this->compareTime;
249
    }
250
251 3
    public function getMetrics(): array
252
    {
253 3
        return $this->metrics;
254
    }
255
256 7
    public function isConsideringTopMetrics(): bool
257
    {
258 7
        return in_array('TOP', $this->metrics);
259
    }
260
261 9
    public function isConsideringRisingMetrics(): bool
262
    {
263 9
        return in_array('RISING', $this->metrics);
264
    }
265
    
266
    public function withResolution(string $resolution): self
267
    {
268
        $this->resolution = $resolution;
269
270
        return $this;
271
    }
272
    
273
    public function getResolution(): string
274
    {
275
        return $this->resolution;
276
    }
277
}
278