|
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\Collection; |
|
9
|
|
|
|
|
10
|
|
|
class SearchConsoleClient |
|
11
|
|
|
{ |
|
12
|
|
|
const CHUNK_SIZE = 5000; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Google_Client |
|
16
|
|
|
*/ |
|
17
|
|
|
private $googleClient; |
|
18
|
|
|
|
|
19
|
|
|
private $queryOptParams = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* SearchConsoleClient constructor. |
|
23
|
|
|
* @param Google_Client $googleClient |
|
24
|
|
|
* @internal param Google_Service_Webmasters $service |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(Google_Client $googleClient) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->googleClient = $googleClient; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function performQuery($siteUrl, $rows, $request): Collection |
|
32
|
|
|
{ |
|
33
|
|
|
$searchanalyticsResource = $this->getWebmastersService()->searchanalytics; |
|
34
|
|
|
|
|
35
|
|
|
$maxQueries = 2000; |
|
36
|
|
|
$currentRequest = 1; |
|
37
|
|
|
$dataRows = new Collection(); |
|
38
|
|
|
|
|
39
|
|
|
while ($currentRequest < $maxQueries) { |
|
40
|
|
|
$startRow = ($currentRequest-1) * self::CHUNK_SIZE; |
|
41
|
|
|
|
|
42
|
|
|
$request->setRowLimit(self::CHUNK_SIZE); |
|
43
|
|
|
$request->setStartRow($startRow); |
|
44
|
|
|
|
|
45
|
|
|
$backoff = new ExponentialBackoff(10); |
|
46
|
|
|
$response = $backoff->execute(function () use ($searchanalyticsResource, $siteUrl, $request) { |
|
47
|
|
|
return $searchanalyticsResource->query($siteUrl, $request, $this->queryOptParams); |
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
|
|
// Stop if no more rows returned |
|
51
|
|
|
if (count($response->getRows()) == 0) { |
|
52
|
|
|
break; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
foreach ($response->getRows() as $row) { |
|
56
|
|
|
/* |
|
57
|
|
|
* Use a unique hash as key to prevent duplicates caused by the query dimension problem with the google api |
|
58
|
|
|
* Google give less than 5000 rows back when two or more dimension with the query dimension are choosen, repeated calls give back more rows |
|
59
|
|
|
* https://productforums.google.com/forum/?hl=en#!topic/webmasters/wF_Rm9CGr4U |
|
60
|
|
|
*/ |
|
61
|
|
|
|
|
62
|
|
|
$uniqueHash = md5(str_random()); |
|
63
|
|
|
if (count($row->getKeys())) { |
|
64
|
|
|
$item = array_combine($request->getDimensions(), $row->getKeys()); |
|
65
|
|
|
$uniqueHash = md5(implode('', $row->getKeys()) . $request->getSearchType()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$item['clicks'] = $row->getClicks(); |
|
|
|
|
|
|
69
|
|
|
$item['impressions'] = $row->getImpressions(); |
|
70
|
|
|
$item['ctr'] = $row->getCtr(); |
|
71
|
|
|
$item['position'] = $row->getPosition(); |
|
72
|
|
|
$item['searchType'] = $request->getSearchType(); |
|
73
|
|
|
|
|
74
|
|
|
$dataRows->put($uniqueHash, $item); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
//Stop if the requested row count are reached |
|
78
|
|
|
if ($dataRows->count() >= $rows) { |
|
79
|
|
|
break; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$currentRequest++; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $dataRows->take($rows); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $quotaUser |
|
90
|
|
|
* |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setQuotaUser(string $quotaUser) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->queryOptParams['quotaUser'] = $quotaUser; |
|
95
|
|
|
|
|
96
|
|
|
$guzzleConfig = $this->googleClient->getHttpClient()->getConfig(); |
|
97
|
|
|
|
|
98
|
|
|
array_set($guzzleConfig, 'base_uri', Google_Client::API_BASE_PATH . '?quotaUser=' . $quotaUser); |
|
99
|
|
|
|
|
100
|
|
|
$guzzleClient = new Client($guzzleConfig); |
|
101
|
|
|
|
|
102
|
|
|
$this->googleClient->setHttpClient($guzzleClient); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $accessToken |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setAccessToken(string $accessToken) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->googleClient->setAccessToken($accessToken); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
public function getWebmastersService(): Google_Service_Webmasters |
|
115
|
|
|
{ |
|
116
|
|
|
return new Google_Service_Webmasters($this->googleClient); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: