FrontendBootstrap   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 40.96 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 12
c 4
b 1
f 0
lcom 1
cbo 7
dl 34
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A bootstrap() 0 6 1
A setTime() 10 10 1
A setTheme() 0 17 3
C setModule() 24 24 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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