Passed
Push — master ( bebd8f...9bc76b )
by Alexey
02:16
created

FileState   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 17
eloc 35
c 6
b 3
f 0
dl 0
loc 185
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A timestamp() 0 4 1
A getDateFormat() 0 3 1
A disable() 0 6 2
A statusCode() 0 4 1
A isEnabled() 0 3 1
A enable() 0 6 1
A getFilePath() 0 3 1
A getFileStatePath() 0 3 1
A getDefaultTitle() 0 3 1
A getSubscribeOptionsTemplate() 0 6 3
A getSubscribeOptions() 0 3 1
A getDefaultContent() 0 3 1
A getSubscribePath() 0 3 1
1
<?php
2
3
namespace dominus77\maintenance\states;
4
5
use Yii;
6
use Exception;
7
use yii\base\BaseObject;
8
use dominus77\maintenance\interfaces\StateInterface;
9
use dominus77\maintenance\models\FileStateForm;
10
11
/**
12
 * Class FileState
13
 * @package dominus77\maintenance\states
14
 *
15
 * @property array|mixed $subscribeOptionsTemplate
16
 * @property string $fileStatePath
17
 */
18
class FileState extends BaseObject implements StateInterface
19
{
20
    /**
21
     * @var string the filename that will determine if the maintenance mode is enabled
22
     */
23
    public $fileName = 'YII_MAINTENANCE_MODE_ENABLED';
24
25
    /**
26
     * Default title
27
     * @var string
28
     */
29
    public $defaultTitle = 'Maintenance';
30
31
    /**
32
     * Default content
33
     * @var string
34
     */
35
    public $defaultContent = 'The site is undergoing technical work. We apologize for any inconvenience caused.';
36
37
    /**
38
     * @var string name of the file where subscribers will be stored
39
     */
40
    public $fileSubscribe = 'YII_MAINTENANCE_MODE_SUBSCRIBE';
41
42
    /**
43
     * Options SubscribeFormWidget
44
     * @var array
45
     */
46
    public $subscribeOptions = [];
47
48
    /**
49
     * @var string the directory in that the file stated in $fileName above is residing
50
     */
51
    public $directory = '@runtime';
52
53
    /**
54
     * @var string the complete path of the file - populated in init
55
     */
56
    public $path;
57
58
    /**
59
     * @var string the complete path of the file subscribe - populated in init
60
     */
61
    public $subscribePath;
62
63
    /**
64
     * Enter Datetime format
65
     * @var string
66
     */
67
    public $dateFormat = 'd-m-Y H:i:s';
68
69
    /**
70
     * Initialization
71
     */
72
    public function init()
73
    {
74
        $this->path = $this->getFilePath($this->fileName);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getFilePath($this->fileName) can also be of type boolean. However, the property $path is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
        $this->subscribePath = $this->getFilePath($this->fileSubscribe);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getFilePath($this->fileSubscribe) can also be of type boolean. However, the property $subscribePath is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
76
    }
77
78
    /**
79
     * Turn on mode.
80
     *
81
     * @return bool|mixed
82
     */
83
    public function enable()
84
    {
85
        file_put_contents($this->path,
86
            'The maintenance Mode of your Application is enabled if this file exists.');
87
        chmod($this->path, 0765);
88
        return true;
89
    }
90
91
    /**
92
     * Turn off mode.
93
     *
94
     * @return bool|mixed
95
     */
96
    public function disable()
97
    {
98
        if (file_exists($this->path)) {
99
            unlink($this->path);
100
        }
101
        return true;
102
    }
103
104
    /**
105
     * @return bool will return true if the file exists
106
     */
107
    public function isEnabled()
108
    {
109
        return file_exists($this->path);
110
    }
111
112
    /**
113
     * Timestamp
114
     *
115
     * @return int
116
     * @throws Exception
117
     */
118
    public function timestamp()
119
    {
120
        $model = new FileStateForm();
121
        return $model->getTimestamp();
122
    }
123
124
    /**
125
     * Status code
126
     * @return int|string
127
     */
128
    public function statusCode()
129
    {
130
        $model = new FileStateForm();
131
        return $model->getStatusCode();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $model->getStatusCode() also could return the type string which is incompatible with the return type mandated by dominus77\maintenance\in...Interface::statusCode() of integer.
Loading history...
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getDateFormat()
138
    {
139
        return $this->dateFormat;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getDefaultTitle()
146
    {
147
        return $this->defaultTitle;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getDefaultContent()
154
    {
155
        return $this->defaultContent;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getFileStatePath()
162
    {
163
        return $this->path;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getSubscribePath()
170
    {
171
        return $this->subscribePath;
172
    }
173
174
    /**
175
     * @return array
176
     */
177
    public function getSubscribeOptions()
178
    {
179
        return $this->subscribeOptions;
180
    }
181
182
    /**
183
     * @return array|mixed
184
     */
185
    public function getSubscribeOptionsTemplate()
186
    {
187
        return (isset($this->subscribeOptions['template']) && !empty($this->subscribeOptions['template'])) ?
188
            $this->subscribeOptions['template'] : [
189
                'html' => '@dominus77/maintenance/mail/emailNotice-html',
190
                'text' => '@dominus77/maintenance/mail/emailNotice-text'
191
            ];
192
    }
193
194
    /**
195
     * Return file path.
196
     *
197
     * @param $fileName string
198
     * @return bool|string
199
     */
200
    protected function getFilePath($fileName)
201
    {
202
        return Yii::getAlias($this->directory . '/' . $fileName);
203
    }
204
}
205