Completed
Push — master ( 424357...d1d6a1 )
by Nate
24:35 queued 09:39
created

Authorize   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 96
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 6 1
A run() 0 13 2
A performAction() 0 20 2
A runInternal() 0 16 2
A getScopes() 0 8 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron\actions\authorization;
10
11
use Craft;
12
use flipbox\craft\ember\actions\LookupTrait;
13
use flipbox\patron\Patron;
14
use flipbox\patron\queries\ProviderQuery;
15
use League\OAuth2\Client\Provider\AbstractProvider;
16
use yii\web\Controller;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 *
22
 * @property Controller $controller
23
 */
24
class Authorize extends Action
25
{
26
    use LookupTrait;
27
28
    /**
29
     * @param int $id
30
     * @return AbstractProvider
31
     * @throws \yii\base\InvalidConfigException
32
     */
33
    protected function find(int $id)
34
    {
35
        return (new ProviderQuery())
36
            ->id($id)
37
            ->one();
38
    }
39
40
    /**
41
     * @inheritdoc
42
     * @throws \yii\base\InvalidConfigException
43
     */
44
    public function run(int $id)
45
    {
46
        if (!$object = $this->find($id)) {
47
            return $this->handleNotFoundResponse();
48
        }
49
50
        // Save provider to session
51
        Patron::getInstance()->getSession()->setProvider(
52
            $id
53
        );
54
55
        return $this->runInternal($object, $this->getScopes());
56
    }
57
58
    /**
59
     * @param AbstractProvider $provider
60
     * @param array $scopes
61
     * @return mixed|\yii\web\Response
62
     * @throws \yii\web\HttpException
63
     */
64
    protected function runInternal(AbstractProvider $provider, array $scopes)
65
    {
66
        $request = Craft::$app->getRequest();
67
68
        // Set redirect
69
        Patron::getInstance()->getSession()->setRedirectUrl(
70
            $request->getParam('redirect', $request->getReferrer())
71
        );
72
73
        // Check access
74
        if (($access = $this->checkAccess($provider, $scopes)) !== true) {
75
            return $access;
76
        }
77
78
        return $this->performAction($provider, $scopes);
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    protected function getScopes(): array
85
    {
86
        if (!$scopes = Craft::$app->getRequest()->getParam('scope')) {
87
            return [];
88
        }
89
90
        return $scopes;
91
    }
92
93
    /**
94
     * @param AbstractProvider $provider
95
     * @param array $scopes
96
     * @return mixed
97
     * @throws \yii\web\HttpException
98
     */
99
    protected function performAction(AbstractProvider $provider, array $scopes)
100
    {
101
        return $this->handleExceptions(function () use ($provider, $scopes) {
102
            $options = [];
103
104
            if (!empty($scopes)) {
105
                $options['scope'] = $scopes;
106
            }
107
108
            // Get url (this ensure all params ... such as state)
109
            $authorizationUrl = $provider->getAuthorizationUrl($options);
110
111
            // Save state to session
112
            Patron::getInstance()->getSession()->setState(
113
                $provider->getState()
114
            );
115
116
            return $this->controller->redirect($authorizationUrl);
117
        });
118
    }
119
}
120