URIFilter::init()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 10 Features 0
Metric Value
eloc 4
c 11
b 10
f 0
dl 0
loc 7
rs 10
cc 3
nc 4
nop 0
1
<?php
2
3
namespace dominus77\maintenance\filters;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use yii\web\Application;
8
use yii\web\NotFoundHttpException;
9
use yii\web\Request;
10
use dominus77\maintenance\Filter;
11
12
/**
13
 * Class URIFilter
14
 * @package dominus77\maintenance\filters
15
 */
16
class URIFilter extends Filter
17
{
18
    /**
19
     * @var array|string
20
     */
21
    public $uri;
22
    /**
23
     * @var Request|null
24
     */
25
    protected $request;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function init()
31
    {
32
        if (Yii::$app instanceof Application) {
33
            $this->request = Yii::$app->request;
34
        }
35
        if (is_string($this->uri)) {
36
            $this->uri = [$this->uri];
37
        }
38
    }
39
40
    /**
41
     * @return bool
42
     * @throws NotFoundHttpException
43
     */
44
    public function isAllowed()
45
    {
46
        if ($this->request && is_array($this->uri) && !empty($this->uri) && $resolve = $this->request->resolve()) {
47
            $this->uri = ArrayHelper::merge($this->uri, ['maintenance/subscribe']);
48
            return (bool)in_array($resolve[0], $this->uri, true);
49
        }
50
        return false;
51
    }
52
}
53