SearchConsoleClientFactory::createForConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SchulzeFelix\SearchConsole;
4
5
use Google_Client;
6
use Google_Service_Webmasters;
7
use GuzzleHttp\Client;
8
use Symfony\Component\Cache\Adapter\Psr16Adapter;
9
10
class SearchConsoleClientFactory
11
{
12
    /**
13
     * @param array $searchConsoleConfig
14
     * @return SearchConsoleClient
15
     */
16
    public static function createForConfig(array $searchConsoleConfig): SearchConsoleClient
17
    {
18
        $authenticatedClient = self::createAuthenticatedGoogleClient($searchConsoleConfig);
19
20
        return new SearchConsoleClient($authenticatedClient);
21
    }
22
23
    /**
24
     * @param array $config
25
     * @return Google_Client
26
     */
27
    public static function createAuthenticatedGoogleClient(array $config): Google_Client
28
    {
29
        $client = new Google_Client();
30
31
        self::configureAuthentication($client, $config);
32
33
        $client->addScope(Google_Service_Webmasters::WEBMASTERS);
34
        $client->setAccessType('offline');
35
36
        self::configureGzip($client, $config['application_name']);
37
        self::configureCache($client, $config['cache']);
38
39
        return $client;
40
    }
41
42
    /**
43
     * @param Google_Client $client
44
     * @param $config
45
     */
46
    protected static function configureCache(Google_Client $client, $config)
47
    {
48
        $config = collect($config);
49
50
        $store = \Cache::store($config->get('store'));
51
52
        $cache = new Psr16Adapter($store);
53
54
        $client->setCache($cache);
55
56
        $client->setCacheConfig(
57
            $config->except('store')->toArray()
58
        );
59
    }
60
61
    /**
62
     * @param Google_Client $client
63
     * @param $application_name
64
     */
65
    private static function configureGzip(Google_Client $client, $application_name)
66
    {
67
        $client->setApplicationName($application_name.' (gzip)');
68
69
        $options = [];
70
        $options['base_uri'] = Google_Client::API_BASE_PATH;
71
        $options['headers'] = [
72
            'User-Agent' => $application_name.' (gzip)',
73
            'Accept-Encoding' => 'gzip',
74
        ];
75
76
        $guzzleClient = new Client($options);
77
78
        $client->setHttpClient($guzzleClient);
79
    }
80
81
    /**
82
     * @param Google_Client $client
83
     * @param $config
84
     */
85
    private static function configureAuthentication(Google_Client $client, $config)
86
    {
87
        switch ($config['auth_type']):
88
            case 'oauth':
89
                $client->setClientId($config['connections']['oauth']['client_id']);
90
                $client->setClientSecret($config['connections']['oauth']['client_secret']);
91
                break;
92
            case 'oauth_json':
93
                $client->setAuthConfig($config['connections']['oauth_json']['auth_config']);
94
                break;
95
            case 'service_account':
96
                $client->useApplicationDefaultCredentials($config['connections']['service_account']['application_credentials']);
97
                break;
98
        endswitch;
99
    }
100
}
101