Completed
Push — master ( ff2b0b...f369f8 )
by Alexey
05:58
created

Maintenance   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
eloc 34
c 1
b 0
f 1
dl 0
loc 96
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A bootstrap() 0 25 6
A filtersExcepted() 0 17 6
1
<?php
2
3
namespace common\components\maintenance;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\web\Application;
8
use yii\base\BaseObject;
9
use yii\base\BootstrapInterface;
10
use common\components\maintenance\interfaces\StateInterface;
11
12
/**
13
 * Class Maintenance
14
 * @package common\components\maintenance
15
 */
16
class Maintenance extends BaseObject implements BootstrapInterface
17
{
18
    /**
19
     * Value of "OK" status code.
20
     */
21
    const STATUS_CODE_OK = 200;
22
23
    /**
24
     * Route to maintenance action.
25
     * @var string
26
     */
27
    public $route;
28
    /**
29
     * @var array
30
     */
31
    public $filters;
32
    /**
33
     * Default status code to send on maintenance
34
     * 503 = Service Unavailable
35
     * @var integer
36
     */
37
    public $statusCode = 503;
38
    /**
39
     * Retry-After header
40
     * @var bool|string
41
     */
42
    public $retryAfter = false;
43
    /**
44
     * @var StateInterface
45
     */
46
    protected $state;
47
48
    /**
49
     * Maintenance constructor.
50
     * @param StateInterface $state
51
     * @param array $config
52
     */
53
    public function __construct(StateInterface $state, array $config = [])
54
    {
55
        $this->state = $state;
56
        parent::__construct($config);
57
    }
58
59
    /**
60
     * @param Application $app
61
     * @throws InvalidConfigException
62
     */
63
    public function bootstrap($app)
64
    {
65
        if (YII_DEBUG) {
66
            $urlManager = $app->urlManager;
67
            $urlManager->addRules(
68
                [
69
                    '<_m:debug>/<_c:\w+>/<_a:\w+>' => '<_m>/<_c>/<_a>',
70
                ]
71
            );
72
        }
73
74
        $response = $app->response;
75
        if ($app->request->isAjax) {
76
            $response->statusCode = self::STATUS_CODE_OK;
77
        } else {
78
            $response->statusCode = $this->statusCode;
79
            if ($this->retryAfter) {
80
                $response->headers->set('Retry-After', $this->retryAfter);
0 ignored issues
show
Bug introduced by
It seems like $this->retryAfter can also be of type true; however, parameter $value of yii\web\HeaderCollection::set() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                $response->headers->set('Retry-After', /** @scrutinizer ignore-type */ $this->retryAfter);
Loading history...
81
            }
82
        }
83
84
        if ($this->state->isEnabled() && !$this->filtersExcepted()) {
85
            $app->catchAll = [$this->route];
86
        } else {
87
            $response->statusCode = self::STATUS_CODE_OK;
88
        }
89
    }
90
91
    /**
92
     * @return bool
93
     * @throws InvalidConfigException
94
     */
95
    protected function filtersExcepted()
96
    {
97
        if (!is_array($this->filters) || empty($this->filters)) {
0 ignored issues
show
introduced by
The condition is_array($this->filters) is always true.
Loading history...
98
            return false;
99
        }
100
        foreach ($this->filters as $config) {
101
            $filter = Yii::createObject($config);
102
            if (!($filter instanceof Filter)) {
103
                throw new InvalidConfigException(
104
                    'Class "' . get_class($filter) . '" must instance of "' . Filter::class . '".'
105
                );
106
            }
107
            if ($filter->isAllowed()) {
108
                return true;
109
            }
110
        }
111
        return false;
112
    }
113
}