LoginController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 106
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A actionLogin() 0 27 5
A getRedirectUrl() 0 9 2
A handleRedirect() 0 13 2
A restoreSession() 0 4 1
A actionCallback() 0 14 3
1
<?php
2
/**
3
 * Socializer plugin for Craft CMS 3.x
4
 *
5
 * @link      https://enupal.com/
6
 * @copyright Copyright (c) 2019 Enupal LLC
7
 */
8
9
10
namespace enupal\socializer\controllers;
11
12
use Craft;
13
use craft\helpers\UrlHelper;
14
use enupal\socializer\Socializer;
15
use enupal\socializer\controllers\FrontEndController;
16
17
class LoginController extends FrontEndController
18
{
19
    const SESSION_REDIRECT_URL = "socializer.redirectUrl";
20
    const SESSION_PROVIDER_HANDLE = "socializer.providerHandle";
21
22
    /**
23
     * @return \yii\web\Response
24
     * @throws \Throwable
25
     * @throws \craft\errors\MissingComponentException
26
     */
27
    public function actionLogin()
28
    {
29
        $providerHandle = Craft::$app->getRequest()->getParam('provider');
30
        $provider = Socializer::$app->providers->getProviderByHandle($providerHandle);
31
        $redirect = $this->getRedirectUrl();
32
33
        if (is_null($provider)) {
34
            throw new \Exception(Craft::t('enupal-socializer','Provider not found or disabled'));
35
        }
36
37
        $redirectUrl = $redirect ?? Craft::$app->getRequest()->referrer;
38
        Craft::$app->getSession()->set(self::SESSION_REDIRECT_URL, $redirectUrl);
39
        Craft::$app->getSession()->set(self::SESSION_PROVIDER_HANDLE, $providerHandle);
40
        $adapter = $provider->getAdapter();
41
42
        try {
43
            if ($adapter->authenticate()){
44
                if (!Socializer::$app->providers->loginOrRegisterUser($provider)){
0 ignored issues
show
Bug introduced by
It seems like $provider can also be of type array; however, parameter $provider of enupal\socializer\servic...::loginOrRegisterUser() does only seem to accept enupal\socializer\elements\Provider, maybe add an additional type check? ( Ignorable by Annotation )

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

44
                if (!Socializer::$app->providers->loginOrRegisterUser(/** @scrutinizer ignore-type */ $provider)){
Loading history...
45
                    Craft::$app->getSession()->setError(Craft::t('enupal-socializer', "Unable to authenticate user"));
46
                }
47
            }
48
        }
49
        catch (\Exception $e) {
50
            Craft::error($e->getMessage(), __METHOD__);
51
        }
52
53
        return $this->handleRedirect();
54
    }
55
56
    /**
57
     * @return mixed|string
58
     * @throws \yii\base\Exception
59
     */
60
    private function getRedirectUrl()
61
    {
62
        $redirect = Craft::$app->getRequest()->getParam('redirect');
63
64
        if ($redirect){
65
            $redirect = UrlHelper::siteUrl($redirect);
66
        }
67
68
        return $redirect;
69
    }
70
71
    /**
72
     * @return \yii\web\Response
73
     * @throws \Twig\Error\LoaderError
74
     * @throws \Twig\Error\SyntaxError
75
     * @throws \craft\errors\MissingComponentException
76
     */
77
    private function handleRedirect()
78
    {
79
        $redirectUrl = Craft::$app->getSession()->get(self::SESSION_REDIRECT_URL);
80
        $user = Craft::$app->getUser()->getIdentity();
81
82
        if ($user){
0 ignored issues
show
introduced by
$user is of type yii\web\IdentityInterface, thus it always evaluated to true.
Loading history...
83
            $variables['user'] = $user;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$variables was never initialized. Although not strictly required by PHP, it is generally a good practice to add $variables = array(); before regardless.
Loading history...
84
            $redirectUrl = Craft::$app->getView()->renderString($redirectUrl, $variables);
85
        }
86
87
        $this->restoreSession();
88
89
        return $this->redirect($redirectUrl);
90
    }
91
92
    /**
93
     * @return \yii\web\Response
94
     * @throws \Throwable
95
     * @throws \craft\errors\ElementNotFoundException
96
     * @throws \craft\errors\MissingComponentException
97
     * @throws \craft\errors\WrongEditionException
98
     * @throws \yii\base\Exception
99
     */
100
    public function actionCallback()
101
    {
102
        $providerHandle = Craft::$app->getSession()->get(self::SESSION_PROVIDER_HANDLE);
103
        $provider = Socializer::$app->providers->getProviderByHandle($providerHandle);
104
105
        if (is_null($provider)){
106
            throw new \Exception(Craft::t('enupal-socializer','Provider not found or disabled'));
107
        }
108
109
        if (!Socializer::$app->providers->loginOrRegisterUser($provider)){
0 ignored issues
show
Bug introduced by
It seems like $provider can also be of type array; however, parameter $provider of enupal\socializer\servic...::loginOrRegisterUser() does only seem to accept enupal\socializer\elements\Provider, maybe add an additional type check? ( Ignorable by Annotation )

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

109
        if (!Socializer::$app->providers->loginOrRegisterUser(/** @scrutinizer ignore-type */ $provider)){
Loading history...
110
            Craft::$app->getSession()->setError(Craft::t('enupal-socializer', "Unable to authenticate user"));
111
        }
112
113
        return $this->handleRedirect();
114
    }
115
116
    /**
117
     * @throws \craft\errors\MissingComponentException
118
     */
119
    private function restoreSession()
120
    {
121
        Craft::$app->getSession()->remove(self::SESSION_REDIRECT_URL);
122
        Craft::$app->getSession()->remove(self::SESSION_PROVIDER_HANDLE);
123
    }
124
}