1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SchulzeFelix\SearchConsole; |
4
|
|
|
|
5
|
|
|
use Google_Service_Webmasters_ApiDimensionFilter; |
6
|
|
|
use Google_Service_Webmasters_ApiDimensionFilterGroup; |
7
|
|
|
use Google_Service_Webmasters_SearchAnalyticsQueryRequest; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
class SearchConsole |
11
|
|
|
{ |
12
|
|
|
/** @var SearchConsoleClient */ |
13
|
|
|
protected $client; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param SearchConsoleClient $client |
17
|
|
|
*/ |
18
|
|
|
public function __construct(SearchConsoleClient $client) |
19
|
|
|
{ |
20
|
|
|
$this->client = $client; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $quotaUser |
25
|
|
|
* |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
|
|
public function setQuotaUser(string $quotaUser) |
29
|
|
|
{ |
30
|
|
|
$this->client->setQuotaUser($quotaUser); |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string|array $accessToken |
37
|
|
|
* |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
|
public function setAccessToken($accessToken) |
41
|
|
|
{ |
42
|
|
|
if (is_array($accessToken)) { |
43
|
|
|
$accessToken = trim(json_encode($accessToken)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->client->setAccessToken($accessToken); |
47
|
|
|
|
48
|
|
|
$this->client->getGoogleClient()->getCache()->clear(); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $siteUrl |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getSite(string $siteUrl) |
58
|
|
|
{ |
59
|
|
|
$sites = $this->client->getWebmastersService()->sites; |
60
|
|
|
|
61
|
|
|
$siteInfo = $sites->get($siteUrl); |
62
|
|
|
|
63
|
|
|
$response = [ |
64
|
|
|
'siteUrl' => $siteInfo->getSiteUrl(), |
65
|
|
|
'permissionLevel' => $siteInfo->getPermissionLevel(), |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
return $response; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function listSites() |
72
|
|
|
{ |
73
|
|
|
$sites = $this->client->getWebmastersService()->sites; |
74
|
|
|
$siteList = $sites->listSites(); |
75
|
|
|
|
76
|
|
|
$sitesCollection = new Collection(); |
77
|
|
|
foreach ($siteList->getSiteEntry() as $site) { |
78
|
|
|
$sitesCollection->push([ |
79
|
|
|
'siteUrl' => $site->siteUrl, |
80
|
|
|
'permissionLevel' => $site->permissionLevel, |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
$sitesCollection = $sitesCollection->sortBy('siteUrl'); |
84
|
|
|
|
85
|
|
|
return $sitesCollection; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Call the query method on the authenticated client. |
90
|
|
|
* |
91
|
|
|
* @param string $siteUrl |
92
|
|
|
* @param Period $period |
93
|
|
|
* @param array $dimensions |
94
|
|
|
* @param array $filters |
95
|
|
|
* @param int $rows |
96
|
|
|
* @param string $searchType |
97
|
|
|
* @return Collection |
98
|
|
|
* @throws \Exception |
99
|
|
|
*/ |
100
|
|
|
public function searchAnalyticsQuery(string $siteUrl, Period $period, array $dimensions = [], array $filters = [], int $rows = 1000, string $searchType = 'web') |
101
|
|
|
{ |
102
|
|
|
$request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest(); |
103
|
|
|
$request->setStartDate($period->startDate->toDateString()); |
104
|
|
|
$request->setEndDate($period->endDate->toDateString()); |
105
|
|
|
$request->setSearchType($searchType); |
106
|
|
|
$request->setDimensions($dimensions); |
107
|
|
|
$request = $this->applyFilters($request, $filters); |
108
|
|
|
|
109
|
|
|
return $this->client->performQuery($siteUrl, $rows, $request); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function isAccessTokenExpired() |
113
|
|
|
{ |
114
|
|
|
$googleClient = $this->client->getGoogleClient(); |
115
|
|
|
|
116
|
|
|
$newToken = $googleClient->fetchAccessTokenWithRefreshToken(); |
117
|
|
|
|
118
|
|
|
if (isset($newToken['error'])) { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$googleClient->setAccessToken($newToken); |
123
|
|
|
|
124
|
|
|
return $googleClient->isAccessTokenExpired(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/* |
128
|
|
|
* Get the underlying Google_Service_Webmasters object. You can use this |
129
|
|
|
* to basically call anything on the Google Search Console API. |
130
|
|
|
*/ |
131
|
|
|
public function getWebmastersService(): \Google_Service_Webmasters |
132
|
|
|
{ |
133
|
|
|
return $this->client->getWebmastersService(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function applyFilters(Google_Service_Webmasters_SearchAnalyticsQueryRequest $request, $filters) |
137
|
|
|
{ |
138
|
|
|
$filterArray = []; |
139
|
|
|
foreach ($filters as $filterItem) { |
140
|
|
|
if (strlen($filterItem['expression']) === 0) { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
$filter = new Google_Service_Webmasters_ApiDimensionFilter(); |
144
|
|
|
$filter->setDimension($filterItem['dimension']); |
145
|
|
|
$filter->setOperator($filterItem['operator']); |
146
|
|
|
$filter->setExpression($filterItem['expression']); |
147
|
|
|
$filterArray[] = $filter; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (count($filterArray)) { |
151
|
|
|
$filtergroup = new Google_Service_Webmasters_ApiDimensionFilterGroup(); |
152
|
|
|
$filtergroup->setFilters($filterArray); |
153
|
|
|
$request->setDimensionFilterGroups([$filtergroup]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $request; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|