Completed
Push — master ( 0b2f36...19d6a1 )
by Freek
08:35
created

AnalyticsClientFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 34
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createForConfig() 0 8 1
A createAuthenticatedGoogleClient() 0 13 1
A createAnalyticsClient() 0 8 1
1
<?php
2
3
namespace Spatie\Analytics;
4
5
use Google_Client;
6
use Google_Service_Analytics;
7
use Illuminate\Contracts\Cache\Repository;
8
9
class AnalyticsClientFactory
10
{
11
    public static function createForConfig(array $analyticsConfig)
12
    {
13
        $authenticatedClient = self::createAuthenticatedGoogleClient($analyticsConfig);
14
15
        $googleService = new Google_Service_Analytics($authenticatedClient);
16
17
        return self::createAnalyticsClient($analyticsConfig, $googleService);
18
    }
19
20
    public static function createAuthenticatedGoogleClient(array $config): Google_Client
21
    {
22
        $client = new Google_Client();
23
24
        $credentials = $client->loadServiceAccountJson(
25
            $config['service_account_credentials_json'],
26
            'https://www.googleapis.com/auth/analytics.readonly'
0 ignored issues
show
Documentation introduced by
'https://www.googleapis....uth/analytics.readonly' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
        );
28
29
        $client->setAssertionCredentials($credentials);
30
31
        return $client;
32
    }
33
34
    protected static function createAnalyticsClient(array $analyticsConfig, Google_Service_Analytics $googleService): AnalyticsClient
35
    {
36
        $client = new AnalyticsClient($googleService, app(Repository::class));
37
38
        $client->setCacheLifeTimeInMinutes($analyticsConfig['cache_lifetime_in_minutes']);
39
40
        return $client;
41
    }
42
}
43