InterestByRegionSearch::buildQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 21
cts 21
cp 1
rs 9.0036
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace GSoares\GoogleTrends\Search;
4
5
use GSoares\GoogleTrends\Error\GoogleTrendsException;
6
use GSoares\GoogleTrends\Result\InterestByRegionCollection;
7
use GSoares\GoogleTrends\Result\InterestByRegionResult;
8
use GSoares\GoogleTrends\Result\ResultCollectionInterface;
9
10
/**
11
 * @author Gabriel Felipe Soares <[email protected]>
12
 */
13
class InterestByRegionSearch implements SearchInterface
14
{
15
    private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/comparedgeo';
16
17
    /**
18
     * @var ExploreSearch
19
     */
20
    protected $exploreSearch;
21
22
    /**
23
     * @var SearchRequest
24
     */
25
    protected $searchRequest;
26
27 3
    public function __construct(ExploreSearch $exploreSearch = null, SearchRequest $searchRequest = null)
28
    {
29 3
        $this->searchRequest = $searchRequest ?: new SearchRequest();
30 3
        $this->exploreSearch = $exploreSearch ?: new ExploreSearch($this->searchRequest);
31 3
    }
32
33
    /**
34
     * @param SearchFilter $searchFilter
35
     *
36
     * @return InterestByRegionCollection
37
     *
38
     * @throws GoogleTrendsException
39
     */
40 3
    public function search(SearchFilter $searchFilter): ResultCollectionInterface
41
    {
42 3
        $token = $this->exploreSearch
43 3
            ->search($searchFilter)
44 3
            ->getInterestByRegionResult()
45 3
            ->getToken();
46
47 3
        $searchUrl = $this->buildQuery($searchFilter->withToken($token));
48
49 3
        $responseDecoded = $this->searchRequest->search($searchUrl);
50
51 3
        if (!isset($responseDecoded['default']['geoMapData'])) {
52 1
            throw new GoogleTrendsException(
53 1
                sprintf(
54 1
                    'Invalid google response body "%s"...',
55 1
                    substr(var_export($responseDecoded, true), 100)
56
                )
57
            );
58
        }
59
60 2
        $results = [];
61
62 2
        foreach ($responseDecoded['default']['geoMapData'] ?? [] as $row) {
63 2
            if (!isset($row['geoCode'], $row['geoName'], $row['value'], $row['maxValueIndex'])) {
64 1
                throw new GoogleTrendsException(
65 1
                    sprintf(
66 1
                        'Google compared geo list does not contain all keys. Only has: %s',
67 1
                        implode(', ', array_keys($row))
68
                    )
69
                );
70
            }
71
72 1
            $results[] = new InterestByRegionResult(
73 1
                $row['geoCode'],
74 1
                $row['geoName'],
75 1
                (int)($row['value'][0] ?? 0),
76 1
                (int)$row['maxValueIndex'],
77 1
                (bool)($row['hasData'] ?? false)
78
            );
79
        }
80
81 1
        return new InterestByRegionCollection($searchUrl, ...$results);
82
    }
83
84 3
    private function buildQuery(SearchFilter $searchFilter): string
85
    {
86
        $request = [
87
            'geo' => [
88 3
                'country' => $searchFilter->getLocation(),
89
            ],
90
            'comparisonItem' => [
91
                [
92 3
                    'time' => $searchFilter->getTime(),
93
                ],
94
            ],
95 3
            'resolution' => 'REGION',
96 3
            'locale' => $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...
97
            'requestOptions' => [
98 3
                'property' => '',
99 3
                'backend' => 'IZG',
100 3
                'category' => $searchFilter->getCategory(),
101
            ]
102
        ];
103
104 3
        if (!empty($searchFilter->getSearchTerm())) {
105 3
            $request['comparisonItem'][0]['complexKeywordsRestriction'] = [
106
                'keyword' => [
107
                    [
108 3
                        'type' => 'BROAD',
109 3
                        'value' => $searchFilter->getSearchTerm(),
110
                    ],
111
                ],
112
            ];
113
        }
114
115
        $query = [
116 3
            '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...
117 3
            'tz' => '-60',
118 3
            'req' => json_encode($request),
119 3
            'token' => $searchFilter->getToken()
120
        ];
121
122 3
        $queryString = str_replace(
123
            [
124 3
                '%3A',
125
                '%2C',
126
                '%2B'
127
            ],
128
            [
129 3
                ':',
130
                ',',
131
                '+',
132
            ],
133 3
            http_build_query($query)
134
        );
135
136 3
        return self::SEARCH_URL . '?' . $queryString;
137
    }
138
}
139