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 $googleClient */ |
12
|
|
|
private $googleClient; |
13
|
|
|
|
14
|
|
|
/** @var Google_Service_Indexing $indexingService */ |
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
|
|
|
return $this->indexingService |
37
|
|
|
->urlNotifications |
38
|
|
|
->getMetadata([ |
39
|
|
|
"url" => urlencode($url) |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function update(string $url) |
44
|
|
|
{ |
45
|
|
|
return $this->publish($url, "URL_UPDATED"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function delete(string $url) |
49
|
|
|
{ |
50
|
|
|
return $this->publish($url, "URL_DELETED"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function publish(string $url, string $action) |
54
|
|
|
{ |
55
|
|
|
$urlNotification = new Google_Service_Indexing_UrlNotification(); |
56
|
|
|
|
57
|
|
|
$urlNotification->setUrl($url); |
58
|
|
|
$urlNotification->setType($action); |
59
|
|
|
|
60
|
|
|
return $this->indexingService |
61
|
|
|
->urlNotifications |
62
|
|
|
->publish($urlNotification); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|