Completed
Push — master ( ea0c7a...87125e )
by Andrii
16:20
created

ThemeManager::getDefaultTheme()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
crap 6
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;
13
14
use hiqdev\thememanager\models\Settings;
15
use Yii;
16
use yii\base\Application;
17
use yii\base\InvalidConfigException;
18
use yii\web\AssetBundle;
19
20
/**
21
 * Theme Manager.
22
 *
23
 * Usage, in config:
24
 * ```
25
 * 'components' => [
26
 *     'themeManager' => [
27
 *         'class' => \hiqdev\thememanager\ThemeManager::class,
28
 *     ],
29
 * ]
30
 * ```
31
 */
32
class ThemeManager extends \hiqdev\yii2\collection\Manager implements \yii\base\BootstrapInterface
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected $_itemClass = Theme::class;
38
39
    /**
40
     * @var array additional dirs to look for views.
41
     */
42
    public $pathDirs = [];
43
44
    /**
45
     * @var string default theme name
46
     */
47
    protected $_defaultTheme;
48
49
    /**
50
     * Sets the default theme name.
51
     *
52
     * @param string $theme default theme name.
53
     */
54
    public function setDefaultTheme($theme)
55
    {
56
        $this->_defaultTheme = $theme;
57
    }
58
59
    /**
60
     * Returns the default theme. Returns the first of available themes by default.
61
     *
62
     * @return string default theme name.
63
     */
64
    public function getDefaultTheme()
65
    {
66
        if (!$this->_defaultTheme) {
67
            $keys = $this->keys(); /// shame to PHP it can't be done in single line :(
68
            $this->_defaultTheme = reset($keys);
69
        }
70
71
        return $this->_defaultTheme;
72
    }
73
74
    protected $_view;
75
76
    /**
77
     * Returns the view object that can be used to render views or view files.
78
     * The [[render()]] and [[renderFile()]] methods will use
79
     * this view object to implement the actual view rendering.
80
     * If not set, it will default to the "view" application component.
81
     *
82
     * @return \yii\web\View the view object that can be used to render views or view files.
83
     */
84
    public function getView()
85
    {
86
        if ($this->_view === null) {
87
            $this->_view = Yii::$app->getView();
88
        }
89
90
        return $this->_view;
91
    }
92
93
    /**
94
     * Sets the view object to be used.
95
     *
96
     * @param View $view the view object that can be used to render views or view files.
97
     */
98
    public function setView($view)
99
    {
100
        $this->_view = $view;
101
    }
102
103
    /**
104
     * @var Theme current theme object
105
     */
106
    protected $_theme;
107
108
    /**
109
     * Changes theme.
110
     *
111
     * @param string theme name
112
     *
113
     * @throws InvalidConfigException
114
     */
115
    public function setTheme($name)
116
    {
117
        if (!$name) {
118
            throw new InvalidConfigException('no theme to set');
119
        }
120
        $this->_theme = $name;
121
    }
122
123
    public function getTheme()
124
    {
125
        if (is_string($this->_theme)) {
126
            if (!$this->has($this->_theme)) {
127
                throw new InvalidConfigException('unknown theme: ' . $this->_theme);
128
            }
129
            $this->_theme      = $this->getItem($this->_theme);
130
            $this->view->theme = $this->_theme;
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<hiqdev\thememanager\ThemeManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
131
        }
132
133
        return $this->_theme;
134
    }
135
136
    public function getSettings()
137
    {
138
        return $this->getTheme()->getSettings();
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    public static function isHomePage()
145
    {
146
        $controller = Yii::$app->controller;
147
        $default_controller = Yii::$app->defaultRoute;
148
        return (($controller->id === $default_controller) && ($controller->action->id === $controller->defaultAction));
149
    }
150
151
    /**
152
     * @var array assets of the application
153
     */
154
    public $assets = [];
155
156
    /**
157
     * Register all the assets.
158
     */
159
    public function registerAssets()
160
    {
161
        foreach (array_merge($this->assets, $this->getTheme()->assets) as $asset) {
162
            /**
163
             * @var AssetBundle
164
             */
165
            $asset::register($this->getView());
166
        }
167
    }
168
169
    /**
170
     * @var bool is already bootstrapped.
171
     */
172
    protected $_isBootstrapped = false;
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function bootstrap($app)
178
    {
179
        if ($this->_isBootstrapped) {
180
            return;
181
        }
182
        $this->_isBootstrapped = true;
183
184
        Yii::trace('Bootstrap themes', get_called_class() . '::bootstrap');
185
186
        $model = new Settings();
187
        $model->load();
188
        $theme = $this->hasItem($model->theme) ? $model->theme : null;
189
        $theme = $theme ?: $this->getDefaultTheme();
190
        $this->setTheme($theme);
191
        $this->getTheme();
192
    }
193
194
    /* public function init()
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
195
    {
196
        parent::init();
197
    } */
198
}
199