Issues (127)

src/services/Oauth.php (2 issues)

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\services;
9
10
use yii\base\Component;
11
use League\OAuth2\Client\Token\AccessToken;
12
use League\OAuth2\Client\Provider\Google;
13
use craft\helpers\UrlHelper;
14
use dukt\analytics\Plugin as Analytics;
0 ignored issues
show
This use statement conflicts with another class in this namespace, dukt\analytics\services\Analytics. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
16
class Oauth extends Component
17
{
18
    // Public Methods
19
    // =========================================================================
20
21
    /**
22
     * Save Token
23
     *
24
     * @param AccessToken $token
25
     */
26
    public function saveToken(AccessToken $token)
27
    {
28
        $info = Analytics::getInstance()->getInfo();
29
30
        $info->token = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('accessToken' => $...=> $token->getValues()) of type array<string,array|integer|string> is incompatible with the declared type null|string of property $token.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
            'accessToken' => $token->getToken(),
32
            'expires' => $token->getExpires(),
33
            'refreshToken' => $token->getRefreshToken(),
34
            'resourceOwnerId' => $token->getResourceOwnerId(),
35
            'values' => $token->getValues(),
36
        ];
37
38
        Analytics::getInstance()->saveInfo($info);
39
    }
40
41
    /**
42
     * Returns the OAuth Token.
43
     *
44
     * @param bool $refresh
45
     *
46
     * @return AccessToken|null
47
     */
48
    public function getToken($refresh = true)
49
    {
50
        $info = Analytics::getInstance()->getInfo();
51
52
        if (!$info->token) {
53
            return null;
54
        }
55
56
        $token = new AccessToken([
57
            'access_token' => $info->token['accessToken'] ?? null,
58
            'expires' => $info->token['expires'] ?? null,
59
            'refresh_token' => $info->token['refreshToken'] ?? null,
60
            'resource_owner_id' => $info->token['resourceOwnerId'] ?? null,
61
            'values' => $info->token['values'] ?? null,
62
        ]);
63
64
        if ($refresh && $token->hasExpired()) {
65
            $provider = $this->getOauthProvider();
66
            $grant = new \League\OAuth2\Client\Grant\RefreshToken();
67
            $newToken = $provider->getAccessToken($grant, ['refresh_token' => $token->getRefreshToken()]);
68
69
            $token = new AccessToken([
70
                'access_token' => $newToken->getToken(),
71
                'expires' => $newToken->getExpires(),
72
                'refresh_token' => $info->token['refreshToken'],
73
                'resource_owner_id' => $newToken->getResourceOwnerId(),
74
                'values' => $newToken->getValues(),
75
            ]);
76
77
            $this->saveToken($token);
78
        }
79
80
        return $token;
81
    }
82
83
    /**
84
     * Delete Token
85
     *
86
     * @return bool
87
     */
88
    public function deleteToken()
89
    {
90
        $info = Analytics::getInstance()->getInfo();
91
        $info->token = null;
92
        Analytics::getInstance()->saveInfo($info);
93
        return true;
94
    }
95
96
    /**
97
     * Returns a Twitter provider (server) object.
98
     *
99
     * @return Google
100
     */
101
    public function getOauthProvider()
102
    {
103
        $options = [
104
            'clientId' => Analytics::$plugin->getSettings()->oauthClientId,
105
            'clientSecret' => Analytics::$plugin->getSettings()->oauthClientSecret
106
        ];
107
108
        $options = array_merge($options, Analytics::$plugin->getSettings()->oauthProviderOptions);
109
110
        if (!isset($options['redirectUri'])) {
111
            $options['redirectUri'] = $this->getRedirectUri();
112
        }
113
114
        $options = array_map('Craft::parseEnv', $options);
115
116
        return new Google($options);
117
    }
118
119
    /**
120
     * Returns the javascript orgin URL.
121
     *
122
     * @return string
123
     * @throws \craft\errors\SiteNotFoundException
124
     */
125
    public function getJavascriptOrigin()
126
    {
127
        $url = UrlHelper::baseUrl();
128
129
        return rtrim($url, '/');
130
    }
131
132
    /**
133
     * Returns the redirect URI.
134
     *
135
     * @return string
136
     */
137
    public function getRedirectUri()
138
    {
139
        return UrlHelper::actionUrl('analytics/oauth/callback');
140
    }
141
}
142