Passed
Push — master ( 4d325a...bebd8f )
by Alexey
02:23
created

FileState::getFileStatePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
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() returns the type string which is incompatible with the documented return type 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 getFileStatePath()
146
    {
147
        return $this->path;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getSubscribePath()
154
    {
155
        return $this->subscribePath;
156
    }
157
158
    /**
159
     * @return array
160
     */
161
    public function getSubscribeOptions()
162
    {
163
        return $this->subscribeOptions;
164
    }
165
166
    /**
167
     * @return array|mixed
168
     */
169
    public function getSubscribeOptionsTemplate()
170
    {
171
        return (isset($this->subscribeOptions['template']) && !empty($this->subscribeOptions['template'])) ?
172
            $this->subscribeOptions['template'] : [
173
                'html' => '@dominus77/maintenance/mail/emailNotice-html',
174
                'text' => '@dominus77/maintenance/mail/emailNotice-text'
175
            ];
176
    }
177
178
    /**
179
     * Return file path.
180
     *
181
     * @param $fileName string
182
     * @return bool|string
183
     */
184
    protected function getFilePath($fileName)
185
    {
186
        return Yii::getAlias($this->directory . '/' . $fileName);
187
    }
188
}
189