SettingsController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 77
rs 10
c 2
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 26 3
A actionOauth() 0 7 1
A actionSaveOauthSettings() 0 15 1
1
<?php
2
/**
3
 * @link      https://dukt.net/twitter/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://github.com/dukt/twitter/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\twitter\controllers;
9
10
use Craft;
11
use craft\web\Controller;
12
use dukt\twitter\Plugin;
13
use yii\web\Response;
14
use Exception;
15
16
/**
17
 * Settings controller
18
 *
19
 * @author Dukt <[email protected]>
20
 * @since  3.0
21
 */
22
class SettingsController extends Controller
23
{
24
    // Public Methods
25
    // =========================================================================
26
27
    /**
28
     * Settings index.
29
     *
30
     * @return Response
31
     * @throws \craft\errors\SiteNotFoundException
32
     */
33
    public function actionIndex(): Response
34
    {
35
        $tokenExists = false;
36
        $resourceOwner = null;
37
38
        try {
39
            $token = Plugin::getInstance()->getOauth()->getToken();
40
41
            if ($token) {
42
                $tokenExists = true;
43
44
                // Retrieve resource owner’s details
45
                $provider = Plugin::getInstance()->getOauth()->getOauthProvider();
46
                $resourceOwner = $provider->getUserDetails($token);
47
            }
48
        } catch (Exception $e) {
49
            Craft::error('Couldn’t retrieve twitter account: '.$e->getTraceAsString(), __METHOD__);
50
            $error = $e->getMessage();
51
        }
52
53
        return $this->renderTemplate('twitter/settings', [
54
            'error' => $error ?? null,
55
            'tokenExists' => $tokenExists,
56
            'resourceOwner' => $resourceOwner,
57
            'javascriptOrigin' => Plugin::getInstance()->getOauth()->getJavascriptOrigin(),
58
            'redirectUri' => Plugin::getInstance()->getOauth()->getRedirectUri(),
59
        ]);
60
    }
61
62
    /**
63
     * OAuth settings.
64
     *
65
     * @return Response
66
     * @throws \craft\errors\SiteNotFoundException
67
     */
68
    public function actionOauth(): Response
69
    {
70
        return $this->renderTemplate('twitter/settings/oauth', [
71
            'javascriptOrigin' => Plugin::getInstance()->getOauth()->getJavascriptOrigin(),
72
            'redirectUri' => Plugin::getInstance()->getOauth()->getRedirectUri(),
73
            'oauthConsumerKey' => Plugin::$plugin->getConsumerKey(false),
74
            'oauthConsumerSecret' => Plugin::$plugin->getConsumerSecret(false),
75
        ]);
76
    }
77
78
    /**
79
     * Save OAuth settings.
80
     *
81
     * @return Response
82
     * @throws \yii\web\BadRequestHttpException
83
     */
84
    public function actionSaveOauthSettings(): Response
85
    {
86
        $plugin = Craft::$app->getPlugins()->getPlugin('twitter');
87
        $settings = $plugin->getSettings();
88
89
        $postSettings = Craft::$app->getRequest()->getBodyParam('settings');
90
91
        $settings['oauthConsumerKey'] = $postSettings['oauthConsumerKey'];
92
        $settings['oauthConsumerSecret'] = $postSettings['oauthConsumerSecret'];
93
94
        Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->getAttributes());
95
96
        Craft::$app->getSession()->setNotice(Craft::t('twitter', 'OAuth settings saved.'));
97
98
        return $this->redirectToPostedUrl();
99
    }
100
}
101