1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Famdirksen\LaravelGoogleIndexing; |
4
|
|
|
|
5
|
|
|
use Google_Client; |
6
|
|
|
use Google_Service_Indexing; |
7
|
|
|
use Google_Service_Indexing_UrlNotification; |
8
|
|
|
|
9
|
|
|
class LaravelGoogleIndexing |
10
|
|
|
{ |
11
|
|
|
/** @var Google_Client */ |
12
|
|
|
private $googleClient; |
13
|
|
|
|
14
|
|
|
/** @var Google_Service_Indexing */ |
15
|
|
|
private $indexingService; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->googleClient = new Google_Client(); |
20
|
|
|
|
21
|
|
|
$this->googleClient->setAuthConfig(config('laravel-google-indexing.google.auth_config')); |
22
|
|
|
|
23
|
|
|
foreach (config('laravel-google-indexing.google.scopes', []) as $scope) { |
24
|
|
|
$this->googleClient->addScope($scope); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->indexingService = new Google_Service_Indexing($this->googleClient); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function create(): self |
31
|
|
|
{ |
32
|
|
|
return new static(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function status(string $url) |
36
|
|
|
{ |
37
|
|
|
return $this->indexingService |
38
|
|
|
->urlNotifications |
39
|
|
|
->getMetadata([ |
40
|
|
|
'url' => urlencode($url), |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function update(string $url) |
45
|
|
|
{ |
46
|
|
|
return $this->publish($url, 'URL_UPDATED'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function delete(string $url) |
50
|
|
|
{ |
51
|
|
|
return $this->publish($url, 'URL_DELETED'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function publish(string $url, string $action) |
55
|
|
|
{ |
56
|
|
|
$urlNotification = new Google_Service_Indexing_UrlNotification(); |
57
|
|
|
|
58
|
|
|
$urlNotification->setUrl($url); |
59
|
|
|
$urlNotification->setType($action); |
60
|
|
|
|
61
|
|
|
return $this->indexingService |
62
|
|
|
->urlNotifications |
63
|
|
|
->publish($urlNotification); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|