Helper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 31.25 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 11
c 5
b 0
f 3
lcom 1
cbo 5
dl 20
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkApplication() 0 8 2
A checkUserSetup() 10 10 2
A checkPagesSetup() 10 10 2
B checkPassword() 0 24 5

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
namespace app\components;
4
5
/**
6
 * @link http://www.diemeisterei.de/
7
 *
8
 * @copyright Copyright (c) 2015 diemeisterei GmbH, Stuttgart
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
use dektrium\user\models\User as UserModel;
15
use yii\base\Component;
16
use yii\helpers\Html;
17
18
class Helper extends Component
19
{
20
    public static function checkApplication()
21
    {
22
        if (\Yii::$app->user->can('Admin')) {
23
            self::checkPassword(getenv('APP_ADMIN_PASSWORD'));
24
            self::checkUserSetup();
25
            self::checkPagesSetup();
26
        }
27
    }
28
29 View Code Duplication
    private static function checkUserSetup()
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...
30
    {
31
        if (UserModel::find()->where('id != 1')->count() == 0) {
32
            $link = Html::a('user module', ['/user/admin/create']);
33
            \Yii::$app->session->addFlash(
34
                'warning',
35
                "There is no additional user registered, visit {$link} to create an editor."
36
            );
37
        }
38
    }
39
40 View Code Duplication
    private static function checkPagesSetup()
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...
41
    {
42
        if (!\Yii::$app->getModule('pages')->getLocalizedRootNode()) {
43
            $link = Html::a('pages module', ['/pages']);
44
            \Yii::$app->session->addFlash(
45
                'warning',
46
                "There is no navigation root node, visit {$link} to create a root node."
47
            );
48
        }
49
    }
50
51
    /**
52
     * Password check
53
     * Based upon http://stackoverflow.com/a/10753064
54
     *
55
     * @param $pwd
56
     */
57
    private static function checkPassword($pwd)
58
    {
59
        $errors = [];
60
61
        if (strlen($pwd) < 8) {
62
            $errors[] = "Password too short!";
63
        }
64
65
        if (!preg_match("#[0-9]+#", $pwd)) {
66
            $errors[] = "Password must include at least one number!";
67
        }
68
69
        if (!preg_match("#[a-zA-Z]+#", $pwd)) {
70
            $errors[] = "Password must include at least one letter!";
71
        }
72
73
        if (count($errors) > 0) {
74
            $msg = implode('<br/>', $errors);
75
            \Yii::$app->session->addFlash(
76
                'danger',
77
                "Application admin password from environment setting is not strong enough.<br/><i>{$msg}</i>"
78
            );
79
        }
80
    }
81
}
82