Issues (47)

src/controllers/SettingsController.php (1 issue)

Severity
1
<?php
2
/**
3
 * @link      https://dukt.net/videos/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://github.com/dukt/videos/blob/v2/LICENSE.md
6
 */
7
8
namespace dukt\videos\controllers;
9
10
use Craft;
11
use craft\web\Controller;
12
use dukt\videos\Plugin as Videos;
13
use dukt\videos\web\assets\videos\VideosAsset;
14
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
15
use yii\base\InvalidConfigException;
16
use yii\web\BadRequestHttpException;
17
use yii\web\Response;
18
19
/**
20
 * Settings controller
21
 */
22
class SettingsController extends Controller
23
{
24
    /**
25
     * Settings Index.
26
     *
27
     * @return Response
28
     * @throws InvalidConfigException
29
     */
30
    public function actionIndex(): Response
31
    {
32
        $accounts = [];
33
        $accountErrors = [];
34
35
        $gateways = Videos::$plugin->getGateways()->getGateways(false);
36
37
        foreach ($gateways as $gateway) {
38
            try {
39
                $accounts[$gateway->getHandle()] = $gateway->getAccount();
40
                $accountErrors[$gateway->getHandle()] = false;
41
            } catch (IdentityProviderException $identityProviderException) {
42
                $error = $identityProviderException->getMessage();
43
44
                $data = $identityProviderException->getResponseBody();
45
46
                if (isset($data['error_description'])) {
47
                    $error = $data['error_description'];
48
                }
49
50
                $accounts[$gateway->getHandle()] = false;
51
                $accountErrors[$gateway->getHandle()] = $error;
52
            }
53
        }
54
55
        Craft::$app->getView()->registerAssetBundle(VideosAsset::class);
56
57
        return $this->renderTemplate('videos/settings/_index', [
58
            'gateways' => $gateways,
59
            'accounts' => $accounts,
60
            'accountErrors' => $accountErrors,
61
        ]);
62
    }
63
64
    /**
65
     * Gateway.
66
     *
67
     * @param $gatewayHandle
68
     *
69
     * @return Response
70
     * @throws InvalidConfigException
71
     */
72
    public function actionGateway($gatewayHandle): Response
73
    {
74
        $gateway = Videos::$plugin->getGateways()->getGateway($gatewayHandle, false);
75
        $account = null;
0 ignored issues
show
The assignment to $account is dead and can be removed.
Loading history...
76
77
        try {
78
            $account = $gateway->getAccount();
79
        } catch (IdentityProviderException $identityProviderException) {
80
            $error = $identityProviderException->getMessage();
81
            $data = $identityProviderException->getResponseBody();
82
83
            if (isset($data['error_description'])) {
84
                $error = $data['error_description'];
85
            }
86
        }
87
88
        return $this->renderTemplate('videos/settings/_gateway', [
89
            'gatewayHandle' => $gatewayHandle,
90
            'gateway' => $gateway,
91
            'account' => $account,
92
            'error' => $error ?? null
93
        ]);
94
    }
95
96
    /**
97
     * Gateway OAuth.
98
     *
99
     * @param $gatewayHandle
100
     *
101
     * @return Response
102
     * @throws InvalidConfigException
103
     */
104
    public function actionGatewayOauth($gatewayHandle): Response
105
    {
106
        $gateway = Videos::$plugin->getGateways()->getGateway($gatewayHandle, false);
107
108
        return $this->renderTemplate('videos/settings/_oauth', [
109
            'gatewayHandle' => $gatewayHandle,
110
            'gateway' => $gateway,
111
        ]);
112
    }
113
114
    /**
115
     * Save gateway.
116
     *
117
     * @return Response
118
     * @throws BadRequestHttpException
119
     * @throws InvalidConfigException
120
     * @throws \craft\errors\MissingComponentException
121
     */
122
    public function actionSaveGateway(): Response
123
    {
124
        $this->requireAdmin();
125
126
        $gatewayHandle = Craft::$app->getRequest()->getParam('gatewayHandle');
127
        $gateway = Videos::$plugin->getGateways()->getGateway($gatewayHandle, false);
128
129
        $clientId = Craft::$app->getRequest()->getParam('clientId');
130
        $clientSecret = Craft::$app->getRequest()->getParam('clientSecret');
131
132
        $configData = [
133
            'clientId' => $clientId,
134
            'clientSecret' => $clientSecret,
135
        ];
136
137
        $key = 'plugins.videos.settings.oauthProviderOptions';
138
        $configPath = $key . '.' . $gateway->getHandle();
139
140
        Craft::$app->getProjectConfig()->set($configPath, $configData, sprintf('Save the “%s” integration', $gateway->getHandle()));
141
142
        Craft::$app->getSession()->setNotice(Craft::t('videos', 'Gateway’s OAuth settings saved.'));
143
144
        return $this->redirectToPostedUrl();
145
    }
146
}
147