Passed
Push — master ( 609fbb...fb764c )
by Alexey
02:15
created

FileState::getSubscribeOptionsTemplate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 4
c 1
b 1
f 0
dl 0
loc 6
rs 10
cc 3
nc 4
nop 0
1
<?php
2
3
namespace dominus77\maintenance\states;
4
5
use Yii;
6
use Exception;
7
use RuntimeException;
8
use yii\base\BaseObject;
9
use dominus77\maintenance\interfaces\StateInterface;
10
use dominus77\maintenance\models\FileStateForm;
11
12
/**
13
 * Class FileState
14
 * @package dominus77\maintenance\states
15
 *
16
 * @property array|mixed $subscribeOptionsTemplate
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
        try {
86
            file_put_contents($this->path,
87
                'The maintenance Mode of your Application is enabled if this file exists.');
88
            chmod($this->path, 0765);
89
            return true;
90
        } catch (RuntimeException $e) {
91
            throw new RuntimeException(
92
                "Attention: the maintenance mode could not be enabled because {$this->path} could not be created."
93
            );
94
        }
95
    }
96
97
    /**
98
     * Turn off mode.
99
     *
100
     * @return bool|mixed
101
     */
102
    public function disable()
103
    {
104
        try {
105
            if (file_exists($this->path)) {
106
                unlink($this->path);
107
            }
108
            return true;
109
        } catch (RuntimeException $e) {
110
            throw new RuntimeException(
111
                "Attention: the maintenance mode could not be disabled because {$this->path} could not be removed."
112
            );
113
        }
114
    }
115
116
    /**
117
     * @return bool will return true if the file exists
118
     */
119
    public function isEnabled()
120
    {
121
        return file_exists($this->path);
122
    }
123
124
    /**
125
     * Timestamp
126
     *
127
     * @return int
128
     * @throws Exception
129
     */
130
    public function timestamp()
131
    {
132
        $model = new FileStateForm();
133
        return $model->getTimestamp();
134
    }
135
136
    /**
137
     * Status code
138
     * @return int
139
     */
140
    public function statusCode()
141
    {
142
        $model = new FileStateForm();
143
        return $model->getStatusCode();
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getDateFormat()
150
    {
151
        return $this->dateFormat;
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public function getSubscribePath()
158
    {
159
        return $this->subscribePath;
160
    }
161
162
    /**
163
     * @return array|mixed
164
     */
165
    public function getSubscribeOptionsTemplate()
166
    {
167
        return (isset($this->subscribeOptions['template']) && !empty($this->subscribeOptions['template'])) ?
168
            $this->subscribeOptions['template'] : [
169
                'html' => '@dominus77/maintenance/mail/emailNotice-html',
170
                'text' => '@dominus77/maintenance/mail/emailNotice-text'
171
            ];
172
    }
173
174
    /**
175
     * Return file path.
176
     *
177
     * @param $fileName string
178
     * @return bool|string
179
     */
180
    protected function getFilePath($fileName)
181
    {
182
        return Yii::getAlias($this->directory . '/' . $fileName);
183
    }
184
}
185