Issues (127)

src/controllers/OauthController.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\controllers;
9
10
use Craft;
11
use craft\web\Controller;
12
use dukt\analytics\Plugin as Analytics;
13
use yii\web\Response;
14
15
class OauthController extends Controller
16
{
17
    // Public Methods
18
    // =========================================================================
19
20
    /**
21
     * OAuth connect.
22
     *
23
     * @return Response
24
     */
25
    public function actionConnect()
26
    {
27
        $provider = Analytics::$plugin->oauth->getOauthProvider();
28
29
        Craft::$app->getSession()->set('analytics.oauthState', $provider->getState());
30
31
        $authorizationUrl = $provider->getAuthorizationUrl([
32
            'scope' => [
33
                'https://www.googleapis.com/auth/userinfo.profile',
34
                'https://www.googleapis.com/auth/userinfo.email',
35
                'https://www.googleapis.com/auth/analytics',
36
                'https://www.googleapis.com/auth/analytics.edit',
37
            ],
38
            'access_type' => 'offline',
39
            'prompt' => 'consent'
40
        ]);
41
42
        return $this->redirect($authorizationUrl);
43
    }
44
45
    /**
46
     * OAuth disconnect.
47
     *
48
     * @return Response
49
     */
50
    public function actionDisconnect()
51
    {
52
        if (Analytics::$plugin->oauth->deleteToken()) {
53
            Analytics::$plugin->cache->delete(['accountExplorerData']);
0 ignored issues
show
array('accountExplorerData') of type array<integer,string> is incompatible with the type string expected by parameter $id of dukt\analytics\services\Cache::delete(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            Analytics::$plugin->cache->delete(/** @scrutinizer ignore-type */ ['accountExplorerData']);
Loading history...
54
55
            Craft::$app->getSession()->setNotice(Craft::t('analytics', 'Disconnected from Google Analytics.'));
56
        } else {
57
            Craft::$app->getSession()->setError(Craft::t('analytics', 'Couldn’t disconnect from Google Analytics'));
58
        }
59
60
61
        // redirect
62
63
        $redirect = Craft::$app->getRequest()->referrer;
64
65
        return $this->redirect($redirect);
66
    }
67
68
    /**
69
     * OAuth callback.
70
     *
71
     * @return Response
72
     */
73
    public function actionCallback()
74
    {
75
        $provider = Analytics::$plugin->oauth->getOauthProvider();
76
77
        $code = Craft::$app->getRequest()->getParam('code');
78
79
        try {
80
            // Try to get an access token (using the authorization code grant)
81
            $token = $provider->getAccessToken('authorization_code', [
82
                'code' => $code
83
            ]);
84
85
            // Save token
86
            Analytics::$plugin->oauth->saveToken($token);
87
88
            // Todo: Reset session variables
89
90
            $info = Analytics::getInstance()->getInfo();
91
92
            if ($info->forceConnect === true) {
93
                $info->forceConnect = false;
94
                Analytics::getInstance()->saveInfo($info);
95
            }
96
97
            // Redirect
98
            Craft::$app->getSession()->setNotice(Craft::t('analytics', 'Connected to Google Analytics.'));
99
        } catch (Exception $e) {
0 ignored issues
show
The type dukt\analytics\controllers\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
100
            // Failed to get the token credentials or user details.
101
            Craft::$app->getSession()->setError($e->getMessage());
102
        }
103
104
        return $this->redirect('analytics/settings');
105
    }
106
}
107