Api::getClient()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 25
rs 9.7998
1
<?php
2
/**
3
 * @link      https://dukt.net/analytics/
4
 * @copyright Copyright (c) 2022, Dukt
5
 * @license   https://github.com/dukt/analytics/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\analytics\base;
9
10
use craft\base\Component;
11
use Google_Client;
12
use dukt\analytics\Plugin as Analytics;
13
14
abstract class Api extends Component
15
{
16
    /**
17
     * Returns a Google client.
18
     *
19
     * @return Google_Client
20
     * @throws \yii\base\InvalidConfigException
21
     */
22
    protected function getClient()
23
    {
24
        $token = Analytics::$plugin->getOauth()->getToken();
25
26
        if ($token) {
27
            // make token compatible with Google Client
28
29
            $arrayToken = json_encode([
30
                'created' => 0,
31
                'access_token' => $token->getToken(),
32
                'expires_in' => $token->getExpires(),
33
            ]);
34
35
            // client
36
            $client = new Google_Client();
37
            $client->setApplicationName('Google+ PHP Starter Application');
38
            $client->setClientId('clientId');
39
            $client->setClientSecret('clientSecret');
40
            $client->setRedirectUri('redirectUri');
41
            $client->setAccessToken($arrayToken);
42
43
            return $client;
44
        }
45
46
        return null;
47
    }
48
}
49