1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rdehnhardt\MaintenanceMode; |
4
|
|
|
|
5
|
|
|
use Laravel\Lumen\Application; |
6
|
|
|
|
7
|
|
|
class MaintenanceModeService |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* File to verify maintenance mode. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $maintenanceFile = 'framework/down'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Lumen Application instance. |
18
|
|
|
* |
19
|
|
|
* @var \Laravel\Lumen\Application |
20
|
|
|
*/ |
21
|
|
|
protected $app; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* MaintenanceModeService constructor. |
25
|
|
|
* @param Application $app |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Application $app) |
28
|
|
|
{ |
29
|
|
|
$this->app = $app; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Verify if application is in maintenance mode. |
34
|
|
|
* |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function isDownMode() |
38
|
|
|
{ |
39
|
|
|
return $this->maintenanceFileExists(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Indicates if maintenance file exists. |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function maintenanceFileExists() |
48
|
|
|
{ |
49
|
|
|
return file_exists($this->maintenanceFilePath()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Maintenance file path. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function maintenanceFilePath() |
58
|
|
|
{ |
59
|
|
|
return $this->app->storagePath($this->maintenanceFile); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Verify if application is up. |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function isUpMode() |
68
|
|
|
{ |
69
|
|
|
return !$this->maintenanceFileExists(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Put the application in down mode. |
74
|
|
|
* |
75
|
|
|
* @throws Exceptions\FileException |
76
|
|
|
* |
77
|
|
|
* @return bool true if success and false if something fails. |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function setDownMode() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$file = $this->maintenanceFilePath(); |
82
|
|
|
|
83
|
|
|
if (!touch($file)) { |
84
|
|
|
$message = sprintf( |
85
|
|
|
'Something went wrong on trying to create maintenance file %s.', |
86
|
|
|
$file |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
throw new Exceptions\FileException($message); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Put application in up mode. |
97
|
|
|
* |
98
|
|
|
* @throws Exceptions\FileException |
99
|
|
|
* |
100
|
|
|
* @return bool true if success and false if something fails. |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function setUpMode() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$file = $this->maintenanceFilePath(); |
105
|
|
|
|
106
|
|
|
if (file_exists($file) && !unlink($file)) { |
107
|
|
|
$message = sprintf( |
108
|
|
|
'Something went wrong on trying to remove maintenance file %s.', |
109
|
|
|
$file |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
throw new Exceptions\FileException($message); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function checkAllowedIp($ip) |
119
|
|
|
{ |
120
|
|
|
$allowed = explode(',', env('ALLOWED_IPS')); |
121
|
|
|
|
122
|
|
|
return in_array($ip, $allowed); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.