Completed
Push — master ( 1758f1...601966 )
by Felix
08:03
created

SearchConsoleClientFactory::createForConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 Madewithlove\IlluminatePsrCacheBridge\Laravel\CacheItemPool;
9
10
class SearchConsoleClientFactory
11
{
12
    public static function createForConfig(array $searchConsoleConfig): SearchConsoleClient
13
    {
14
        $authenticatedClient = self::createAuthenticatedGoogleClient($searchConsoleConfig);
15
16
        return new SearchConsoleClient($authenticatedClient);
17
    }
18
19
    public static function createAuthenticatedGoogleClient(array $config): Google_Client
20
    {
21
        $client = new Google_Client();
22
23
        self::configureAuthentication($client, $config);
24
25
        $client->addScope(Google_Service_Webmasters::WEBMASTERS);
26
        $client->setAccessType('offline');
27
28
        self::configureGzip($client, $config['application_name']);
29
        self::configureCache($client, $config['cache']);
30
31
        return $client;
32
    }
33
34
    protected static function configureCache(Google_Client $client, $config)
35
    {
36
        $config = collect($config);
37
38
        $store = \Cache::store($config->get('store'));
39
40
        $cache = new CacheItemPool($store);
41
42
        $client->setCache($cache);
43
44
        $client->setCacheConfig(
45
            $config->except('store')->toArray()
46
        );
47
    }
48
49
    private static function configureGzip(Google_Client $client, $application_name)
50
    {
51
        $client->setApplicationName($application_name . ' (gzip)');
52
53
        $options['base_uri'] = Google_Client::API_BASE_PATH;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
54
        $options['headers'] = [
55
            'User-Agent' => $application_name . ' (gzip)',
56
            'Accept-Encoding' => 'gzip'
57
        ];
58
59
        $guzzleClient = new Client($options);
60
61
        $client->setHttpClient($guzzleClient);
62
    }
63
64
    private static function configureAuthentication(Google_Client $client, $config)
65
    {
66
        switch ($config['auth_type']):
67
            case 'oauth':
68
                $client->setClientId($config['connections']['oauth']['client_id']);
69
                $client->setClientSecret($config['connections']['oauth']['client_secret']);
70
                break;
71
            case 'oauth_json':
72
                $client->setAuthConfig($config['connections']['oauth_json']['auth_config']);
73
            break;
74
            case 'service_account':
75
                $client->useApplicationDefaultCredentials($config['connections']['service_account']['application_credentials']);
76
            break;
77
        endswitch;
78
    }
79
80
}