Completed
Push — master ( d1d74d...5571f9 )
by Robin
01:55
created

LaravelGoogleIndexing   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 56
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A create() 0 4 1
A status() 0 7 1
A update() 0 4 1
A delete() 0 4 1
A publish() 0 11 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 $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