GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SeoModule::redirectSlash()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
namespace app\modules\seo;
3
4
use app\components\BaseModule;
5
use app\modules\seo\handlers\AnalyticsHandler;
6
use app\modules\seo\models\Counter;
7
use app\modules\seo\models\Meta;
8
use Yii;
9
use yii\base\BootstrapInterface;
10
use yii\web\Application;
11
use yii\web\Controller;
12
use yii\web\View;
13
14
class SeoModule extends BaseModule implements BootstrapInterface
15
{
16
    const NO_REDIRECT = 0;
17
    const FROM_WWW = 1;
18
    const FROM_WITHOUT_WWW = 2;
19
    public $cacheConfig = [
20
        'metaCache' => [
21
            'name' => 'metas',
22
            'expire' => 86400,
23
        ],
24
        'counterCache' => [
25
            'name' => 'counters',
26
            'expire' => 86400,
27
        ],
28
        'robotsCache' => [
29
            'name' => 'robots',
30
            'expire' => 86400,
31
        ],
32
    ];
33
    public $include = [];
34
    public $mainPage = '';
35
    /**
36
     * @var int type of redirect from WWW or without WWW
37
     */
38
    public $redirectWWW = self::NO_REDIRECT;
39
    /**
40
     * @var bool if true redirect from url with trailing slash
41
     */
42
    public $redirectTrailingSlash = false;
43
44
    public $analytics = [
45
        'ecGoogle' => [
46
            'active' => 0,
47
            'currency' => AnalyticsHandler::CURRENCY_MAIN,
48
        ],
49
        'ecYandex' => [
50
            'active' => 0,
51
            'currency' => AnalyticsHandler::CURRENCY_MAIN,
52
        ],
53
    ];
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function bootstrap($app)
59
    {
60
        if (is_string($this->include)) {
61
            $this->include = explode(',', $this->include);
62
        }
63
        $app->on(
64
            Application::EVENT_BEFORE_REQUEST,
65
            function () use ($app) {
66
                if ($app->getModule('seo')->redirectWWW != self::NO_REDIRECT) {
67
                    self::redirectWWW();
68
                }
69
                if ($app->getModule('seo')->redirectTrailingSlash == 1) {
70
                    self::redirectSlash();
71
                }
72
73
                $app->getView()->on(View::EVENT_END_PAGE, [Counter::class, 'renderCountersAtHead'], $this->include);
74
                $app->getView()->on(View::EVENT_BEGIN_BODY, [Counter::class, 'renderCountersAtBeginningOfBody'], $this->include);
75
                $app->getView()->on(View::EVENT_END_BODY, [Counter::class, 'renderCountersAtEndOfBody'], $this->include);
76
            }
77
        );
78
79
        // Analytics
80
        $app->on(Application::EVENT_BEFORE_ACTION, [AnalyticsHandler::className(), 'handleBeforeAction']);
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81
        $app->on(Application::EVENT_BEFORE_ACTION, [Meta::className(), 'registrationMeta']);
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function behaviors()
88
    {
89
        return [
90
            'configurableModule' => [
91
                'class' => 'app\modules\config\behaviors\ConfigurableModuleBehavior',
92
                'configurationView' => '@app/modules/seo/views/configurable/_config',
93
                'configurableModel' => 'app\modules\seo\models\ConfigConfigurationModel',
94
            ]
95
        ];
96
    }
97
98
    /**
99
     * @return array prepared to dropdown list in configurable
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
100
     */
101 View Code Duplication
    public static function getRedirectTypes()
102
    {
103
        return [
104
            self::NO_REDIRECT => Yii::t('app', 'No redirect'),
105
            self::FROM_WWW => Yii::t('app', 'Redirect from WWW to without WWW'),
106
            self::FROM_WITHOUT_WWW => Yii::t('app', 'Redirect from without WWW to WWW'),
107
        ];
108
    }
109
110
    /**
111
     * If redirectWWW config make 301 redirect to www or not www domain
112
     */
113
    public static function redirectWWW()
114
    {
115
        $type = Yii::$app->getModule('seo')->redirectWWW;
116
        if ($type != self::NO_REDIRECT) {
117
            $readirArr = [
118 View Code Duplication
                self::FROM_WITHOUT_WWW => function () {
119
                    if (preg_match('#^(http|https):\/\/www\.#i', Yii::$app->request->hostInfo) === 0) {
120
                        Yii::$app->response->redirect(
121
                            str_replace('://', '://www.', Yii::$app->request->absoluteUrl),
122
                            301
123
                        );
124
                        Yii::$app->end();
125
                    }
126
                },
127 View Code Duplication
                self::FROM_WWW => function () {
128
                    if (preg_match('#^(http|https):\/\/www\.#i', Yii::$app->request->hostInfo) === 1) {
129
                        Yii::$app->response->redirect(
130
                            str_replace('://www.', '://', Yii::$app->request->absoluteUrl),
131
                            301
132
                        );
133
                        Yii::$app->end();
134
                    }
135
                },
136
            ];
137
            $readirArr[$type]();
138
        }
139
    }
140
141
    /**
142
     * Make redirect from url with trailing slash
143
     */
144
    public static function redirectSlash()
145
    {
146
        $redirUrl = preg_replace('#^(.*)/$#', '$1', Yii::$app->request->url);
147
        if (!empty($redirUrl) && $redirUrl !== Yii::$app->request->url) {
148
            Yii::$app->response->redirect($redirUrl, 301);
149
            Yii::$app->end();
150
        }
151
    }
152
}
153