FrontendBootstrap::setTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace common\components;
9
10
use common\models\Module;
11
use common\models\Option;
12
use Yii;
13
use yii\base\Application;
14
use yii\base\BootstrapInterface;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * Class FrontendBootstrap
19
 *
20
 * @author Agiel K. Saputra <[email protected]>
21
 * @since 0.1.0
22
 */
23
class FrontendBootstrap implements BootstrapInterface
24
{
25
    /**
26
     * Bootstrap method to be called during application bootstrap stage.
27
     *
28
     * @param Application $app the application currently running
29
     */
30
    public function bootstrap($app)
31
    {
32
        $this->setTime($app);
33
        $this->setTheme($app);
34
        $this->setModule($app);
35
    }
36
37
    /**
38
     * Set time base on Option.
39
     *
40
     * @param Application $app the application currently running
41
     */
42 View Code Duplication
    protected function setTime($app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        /* TIME ZONE */
45
        $app->timeZone = Option::get('time_zone');
46
47
        /* DATE TIME */
48
        $app->formatter->dateFormat = 'php:' . Option::get('date_format');
49
        $app->formatter->timeFormat = 'php:' . Option::get('time_format');
50
        $app->formatter->datetimeFormat = 'php:' . Option::get('date_format') . ' ' . Option::get('time_format');
51
    }
52
53
    /**
54
     * Set theme params
55
     *
56
     * @param Application $app the application currently running
57
     */
58
    protected function setTheme($app)
59
    {
60
        $app->view->theme->basePath = '@themes/' . Option::get('theme');
61
        $app->view->theme->baseUrl = '@web/themes/' . Option::get('theme');
62
        $app->view->theme->pathMap = [
63
            '@app/views' => '@themes/' . Option::get('theme'),
64
            '@app/views/post' => '@themes/' . Option::get('theme') . '/post',
65
        ];
66
        $paramsPath = Yii::getAlias('@themes/') . Option::get('theme') . '/config/params.php';
67
68
        if (is_file($paramsPath)) {
69
            $params = require($paramsPath);
70
            if ($frontendParams = ArrayHelper::getValue($params, 'frontend')) {
71
                $app->params = ArrayHelper::merge($app->params, $frontendParams);
72
            }
73
        }
74
    }
75
76
    /**
77
     * Set modules.
78
     *
79
     * @param Application $app the application currently running
80
     */
81 View Code Duplication
    protected function setModule($app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        foreach (Module::getActiveModules() as $module) {
84
            // Get module backend config.
85
            if ($config = $module->getFrontendConfig()) {
86
                // Set module.
87
                $app->setModules([$module->name => $config]);
88
                // Merge application params with exist module params.
89
                if (is_file($module->getParamPath())) {
90
                    $params = require($module->getParamPath());
91
                    if ($frontendParams = ArrayHelper::getValue($params, 'frontend')) {
92
                        $app->params = ArrayHelper::merge($app->params, $frontendParams);
93
                    }
94
                }
95
                // Bootstrap injection.
96
                if ($module->frontend_bootstrap) {
97
                    $component = $app->getModule($module->name);
98
                    if ($component instanceof BootstrapInterface) {
99
                        $component->bootstrap($app);
100
                    }
101
                }
102
            }
103
        }
104
    }
105
}
106