Completed
Push — master ( bed82d...5b1b17 )
by Dmitry
02:54
created

SettingsController::getSettingsStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Theme Manager for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-thememanager
7
 * @package   yii2-thememanager
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\thememanager\controllers;
13
14
use hiqdev\thememanager\models\Settings;
15
use hiqdev\thememanager\Module;
16
use hiqdev\thememanager\storage\SettingsStorageInterface;
17
use hiqdev\thememanager\ThemeManager;
18
use Yii;
19
use yii\base\DynamicModel;
20
use yii\web\Response;
21
22
class SettingsController extends \yii\web\Controller
23
{
24
    /**
25
     * @var Module
26
     */
27
    public $module;
28
29
    /**
30
     * @return Settings
31
     */
32
    public function getModel()
33
    {
34
        return $this->module->getManager()->getSettings();
35
    }
36
37
    /**
38
     * @return SettingsStorageInterface
39
     */
40
    public function getSettingsStorage()
41
    {
42
        return Yii::$app->get('themeSettingsStorage');
43
    }
44
45
    /**
46
     * Settings form
47
     *
48
     * @return Response
49
     */
50
    public function actionIndex()
51
    {
52
        $model = $this->getModel();
53
54
        $data = Yii::$app->request->post($model->formName());
55
        if (empty($data)) {
56
            $data = $this->getSettingsStorage()->get();
57
        }
58
59
        if (Yii::$app->request->getIsPost() && $model->load($data) && $model->validate()) {
60
            $this->getSettingsStorage()->set($model);
61
            Yii::$app->session->setFlash('success', 'Layout settings saved.');
62
        }
63
64
        if (Yii::$app->request->isAjax) {
65
            return $this->renderAjax('//settings/_form', compact('model'));
66
        }
67
68
        return $this->render('index', compact('model'));
69
    }
70
71
    /**
72
     * @param $theme
73
     * @return Response
74
     */
75
    public function actionChangeTheme($theme)
76
    {
77
        if ($this->module->getManager()->has($theme)) {
78
            $model = $this->getModel();
79
            $model->theme = $theme;
80
            $this->getSettingsStorage()->set($model);
81
            Yii::$app->session->setFlash('success', 'Theme changed');
82
        }
83
84
        return $this->redirect(['index']);
85
    }
86
87
    public function actionCollapsedSidebar()
88
    {
89
        if (Yii::$app->request->isAjax) {
90
            $model = $this->getModel();
91
            $model->collapsed_sidebar = Yii::$app->request->post('collapsed_sidebar');
0 ignored issues
show
Documentation introduced by
The property collapsed_sidebar does not exist on object<hiqdev\thememanager\models\Settings>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
            $this->getSettingsStorage()->set($model);
93
        }
94
    }
95
}
96