Passed
Push — master ( 9511a8...de0418 )
by Benjamin
07:41 queued 03:07
created

SettingsController::actionSaveSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link      https://dukt.net/social/
4
 * @copyright Copyright (c) 2018, Dukt
5
 * @license   https://github.com/dukt/social/blob/v2/LICENSE.md
6
 */
7
8
namespace dukt\social\controllers;
9
10
use Craft;
11
use craft\web\Controller;
12
use dukt\social\Plugin;
13
use yii\web\Response;
14
15
/**
16
 * The SettingsController class is a controller that handles various settings related tasks.
17
 *
18
 * Note that all actions in the controller require an authenticated Craft session via [[allowAnonymous]].
19
 *
20
 * @author  Dukt <[email protected]>
21
 * @since   1.0
22
 */
23
class SettingsController extends Controller
24
{
25
    // Public Methods
26
    // =========================================================================
27
28
    /**
29
     * General settings.
30
     *
31
     * @return Response
32
     * @throws \yii\base\InvalidConfigException
33
     */
34
    public function actionSettings(): Response
35
    {
36
        if (Craft::$app->getEdition() !== Craft::Pro) {
37
            return $this->renderTemplate('social/settings/_pro-requirement');
38
        }
39
40
        $plugin = Craft::$app->getPlugins()->getPlugin('social');
41
        $variables['settings'] = $plugin->getSettings();
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...
42
43
        $accounts = Plugin::getInstance()->getLoginAccounts()->getLoginAccounts();
44
        $variables['totalAccounts'] = count($accounts);
45
46
        return $this->renderTemplate('social/settings/settings', $variables);
47
    }
48
49
    /**
50
     * Saves the settings.
51
     *
52
     * @return null|Response
53
     * @throws \yii\web\BadRequestHttpException
54
     */
55
    public function actionSaveSettings()
56
    {
57
        $this->requirePostRequest();
58
        $settings = Craft::$app->getRequest()->getBodyParam('settings', []);
59
        $plugin = Craft::$app->getPlugins()->getPlugin('social');
60
61
        if ($plugin === null) {
62
            throw new NotFoundHttpException('Plugin not found');
0 ignored issues
show
Bug introduced by
The type dukt\social\controllers\NotFoundHttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
        }
64
65
        if (!Plugin::getInstance()->savePluginSettings($settings, $plugin)) {
0 ignored issues
show
Bug introduced by
It seems like $settings can also be of type string; however, parameter $settings of dukt\social\Plugin::savePluginSettings() does only seem to accept array, 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

65
        if (!Plugin::getInstance()->savePluginSettings(/** @scrutinizer ignore-type */ $settings, $plugin)) {
Loading history...
66
            Craft::$app->getSession()->setError(Craft::t('app', 'Couldn’t save plugin settings.'));
67
68
            // Send the plugin back to the template
69
            Craft::$app->getUrlManager()->setRouteParams([
70
                'plugin' => $plugin
71
            ]);
72
73
            return null;
74
        }
75
76
        Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.'));
77
78
        return $this->redirectToPostedUrl();
79
    }
80
}
81