ExploreSearch   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 86
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A search() 0 24 3
A buildUrl() 0 39 2
1
<?php declare(strict_types=1);
2
3
namespace GSoares\GoogleTrends\Search;
4
5
use GSoares\GoogleTrends\Error\GoogleTrendsException;
6
use GSoares\GoogleTrends\Result\ExploreResult;
7
use GSoares\GoogleTrends\Result\ExploreResultCollection;
8
9
/**
10
 * @author Gabriel Felipe Soares <[email protected]>
11
 */
12
class ExploreSearch
13
{
14
    private const EXPLORE_URL = 'https://trends.google.com/trends/api/explore';
15
16
    /**
17
     * @var SearchRequest
18
     */
19
    private $searchRequest;
20
21 2
    public function __construct(SearchRequest $searchRequest = null)
22
    {
23 2
        $this->searchRequest = $searchRequest ?: new SearchRequest();
24 2
    }
25
26
    /**
27
     * @param SearchFilter $searchFilter
28
     *
29
     * @return ExploreResultCollection
30
     *
31
     * @throws GoogleTrendsException
32
     */
33 2
    public function search(SearchFilter $searchFilter): ExploreResultCollection
34
    {
35 2
        $response = $this->searchRequest->search($this->buildUrl($searchFilter));
36
37 2
        $results = [];
38
39 2
        foreach ($response['widgets'] as $widget) {
40 2
            if (!isset($widget['token'], $widget['id'])) {
41 1
                throw new GoogleTrendsException(
42 1
                    sprintf(
43 1
                        'Missing request data for explore search. Got %s',
44 1
                        implode(', ', array_keys($widget))
45
                    )
46
                );
47
            }
48
49 1
            $results[] = new ExploreResult(
50 1
                $widget['id'],
51 1
                $widget['token']
52
            );
53
        }
54
55 1
        return new ExploreResultCollection(...$results);
56
    }
57
58 2
    private function buildUrl(SearchFilter $searchFilter): string
59
    {
60
        $request = [
61
            'comparisonItem' => [
62
                [
63 2
                    'geo' => $searchFilter->getLocation(),
64 2
                    'time' => $searchFilter->getTime()
65
                ]
66
            ],
67 2
            'category' => $searchFilter->getCategory(),
68 2
            'property' => $searchFilter->getSearchType(),
69
        ];
70
71 2
        if (!empty($searchFilter->getSearchTerm())) {
72 2
            $request['comparisonItem'][0]['keyword'] = $searchFilter->getSearchTerm();
73
        }
74
75
        $query = [
76 2
            'hl' => $searchFilter->getLanguage(),
0 ignored issues
show
Deprecated Code introduced by
The method GSoares\GoogleTrends\Sea...chFilter::getLanguage() 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...
77 2
            'tz' => '-120',
78 2
            'req' => json_encode($request),
79
        ];
80
81 2
        $queryString = str_replace(
82
            [
83 2
                '%3A',
84
                '%2C',
85
                '%2B'
86
            ],
87
            [
88 2
                ':',
89
                ',',
90
                '+',
91
            ],
92 2
            http_build_query($query)
93
        );
94
95 2
        return self::EXPLORE_URL . '?' . $queryString;
96
    }
97
}
98