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