Completed
Push — master ( 8d6c5f...6f0c85 )
by Martijn van
02:23
created

MaintenanceModeExtension::respond503()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
3
/**
4
 * Class MaintenanceModeExtension
5
 *
6
 * @property Controller $owner
7
 */
8
class MaintenanceModeExtension extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    /**
12
     * @throws SS_HTTPResponse_Exception
13
     */
14
    public function onBeforeInit()
15
    {
16
        if($this->isDownForMaintenance())
17
        {
18
            $this->respond503();
19
        }
20
    }
21
22
    /**
23
     * @return bool
24
     */
25
    public function isDownForMaintenance()
26
    {
27
        return file_exists(BASE_PATH.'/mysite/down');
28
    }
29
30
    /**
31
     * Somehow $this->owner->httpError puts a burden on de Database.
32
     *
33
     * Maybe not very Silverstripe'sch, but at least this works
34
     */
35
    protected function respond503()
36
    {
37
        $message = 'Website is down for maintenance';
38
        $content = '<h1>'.$message.'</h1>';
39
40
        header($message, true, 503);
41
42
        $errorFile = BASE_PATH.'/assets/error-503.html';
43
        if(is_file($errorFile)) {
44
            $content = file_get_contents($errorFile);
45
        }
46
47
        echo $content;
48
        exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method respond503() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
49
    }
50
51
}
52