SearchConsoleClient::getWebmastersService()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SchulzeFelix\SearchConsole;
4
5
use Google_Client;
6
use Google_Service_Webmasters;
7
use GuzzleHttp\Client;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
11
class SearchConsoleClient
12
{
13
    const CHUNK_SIZE = 25000;
14
15
    /**
16
     * @var Google_Client
17
     */
18
    private $googleClient;
19
20
    private $queryOptParams = [];
21
22
    /**
23
     * SearchConsoleClient constructor.
24
     * @param Google_Client $googleClient
25
     * @internal param Google_Service_Webmasters $service
26
     */
27
    public function __construct(Google_Client $googleClient)
28
    {
29
        $this->googleClient = $googleClient;
30
    }
31
32
    /**
33
     * @param string $siteUrl
34
     * @param int $rows
35
     * @param \Google_Service_Webmasters_SearchAnalyticsQueryRequest $request
36
     * @return Collection
37
     * @throws \Exception
38
     */
39
    public function performQuery($siteUrl, $rows, $request): Collection
40
    {
41
        $searchanalyticsResource = $this->getWebmastersService()->searchanalytics;
42
43
        $maxQueries = 2000;
44
        $currentRequest = 1;
45
        $dataRows = new Collection();
46
47
        while ($currentRequest < $maxQueries) {
48
            $startRow = ($currentRequest - 1) * self::CHUNK_SIZE;
49
50
            $request->setRowLimit(self::CHUNK_SIZE);
51
            $request->setStartRow($startRow);
52
53
            $backoff = new ExponentialBackoff(10);
54
            $response = $backoff->execute(function () use ($searchanalyticsResource, $siteUrl, $request) {
55
                return $searchanalyticsResource->query($siteUrl, $request, $this->queryOptParams);
56
            });
57
58
            // Stop if no more rows returned
59
            if (count($response->getRows()) == 0) {
60
                break;
61
            }
62
63
            foreach ($response->getRows() as $row) {
64
                /*
65
                 * Use a unique hash as key to prevent duplicates caused by the query dimension problem with the google api
66
                 * Google give less than 5000 rows back when two or more dimension with the query dimension are choosen, repeated calls give back more rows
67
                 * https://productforums.google.com/forum/?hl=en#!topic/webmasters/wF_Rm9CGr4U
68
                 */
69
70
                if (count($row->getKeys())) {
71
                    $item = array_combine($request->getDimensions(), $row->getKeys());
72
                    $uniqueHash = $this->getUniqueItemHash($row, $request);
73
                } else {
74
                    $uniqueHash = md5(Str::random(20));
75
                    $item = [];
76
                }
77
78
                $item['clicks'] = $row->getClicks();
79
                $item['impressions'] = $row->getImpressions();
80
                $item['ctr'] = $row->getCtr();
81
                $item['position'] = $row->getPosition();
82
                $item['searchType'] = $request->getSearchType();
83
84
                $dataRows->put($uniqueHash, $item);
85
            }
86
87
            //Stop if the requested row count are reached
88
            if ($dataRows->count() >= $rows) {
89
                break;
90
            }
91
92
            $currentRequest++;
93
        }
94
95
        return $dataRows->take($rows);
96
    }
97
98
    /**
99
     * @param string $quotaUser
100
     */
101
    public function setQuotaUser(string $quotaUser)
102
    {
103
        $quotaUser = md5($quotaUser);
104
105
        $this->queryOptParams['quotaUser'] = $quotaUser;
106
107
        $guzzleConfig = $this->googleClient->getHttpClient()->getConfig();
108
109
        Arr::set($guzzleConfig, 'base_uri', Google_Client::API_BASE_PATH.'?quotaUser='.$quotaUser);
110
111
        $guzzleClient = new Client($guzzleConfig);
112
113
        $this->googleClient->setHttpClient($guzzleClient);
114
    }
115
116
    /**
117
     * @param string $accessToken
118
     */
119
    public function setAccessToken(string $accessToken)
120
    {
121
        $this->googleClient->setAccessToken($accessToken);
122
    }
123
124
    /**
125
     * @return Google_Client
126
     */
127
    public function getGoogleClient(): Google_Client
128
    {
129
        return $this->googleClient;
130
    }
131
132
    /**
133
     * @return Google_Service_Webmasters
134
     */
135
    public function getWebmastersService(): Google_Service_Webmasters
136
    {
137
        return new Google_Service_Webmasters($this->googleClient);
138
    }
139
140
    /**
141
     * @param $row
142
     * @param $request
143
     * @return string
144
     */
145
    private function getUniqueItemHash($row, $request)
146
    {
147
        $keys = implode('', $row->getKeys());
148
149
        $filters = [];
150
        foreach ($request->getDimensionFilterGroups() as $dimensionFilterGroup) {
151
            foreach ($dimensionFilterGroup->filters as $filter) {
152
                $filters[] = $filter->dimension.$filter->expression.$filter->operator;
153
            }
154
        }
155
        $filters = implode('', $filters);
156
157
        return md5($keys.$filters.$request->getSearchType().$request->endDate.$request->startDate);
158
    }
159
}
160