LaravelGoogleIndexing::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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