Completed
Push — master ( 592e78...324c87 )
by Martijn van
02:18
created

MaintenanceModeExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 7
c 5
b 0
f 1
lcom 1
cbo 3
dl 0
loc 48
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onBeforeInit() 0 7 2
A isDownForMaintenance() 0 4 1
A throw503() 0 8 2
A get503File() 0 6 2
1
<?php
2
3
/**
4
 * Class MaintenanceModeExtension.
5
 *
6
 * throw a 503 if mysite/down exists
7
 *
8
 * @property Controller $owner
9
 */
10
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...
11
{
12
13
    /**
14
     * @throws SS_HTTPResponse_Exception
15
     */
16
    public function onBeforeInit()
17
    {
18
        if($this->isDownForMaintenance())
19
        {
20
            $this->throw503();
21
        }
22
    }
23
24
    /**
25
     * @return bool
26
     */
27
    public function isDownForMaintenance()
28
    {
29
        return file_exists(BASE_PATH.'/mysite/down');
30
    }
31
32
    /**
33
     * Somehow $this->owner->httpError keeps the website spinning.
34
     *
35
     * Maybe not very Silverstripe'sch, but at least this works
36
     *
37
     * @throws SS_HTTPResponse_Exception
38
     */
39
    protected function throw503()
40
    {
41
        $message   = 'Website is down for maintenance';
42
        $errorFile = $this->get503File();
43
        $content   = is_file($errorFile) ? file_get_contents($errorFile) : '<h1>'.$message.'</h1>';
44
45
        throw new SS_HTTPResponse_Exception(new SS_HTTPResponse($content, 503, $message));
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    protected function get503File()
52
    {
53
        $custom = BASE_PATH.(string)Config::inst()->get('MaintenanceMode', 'file');
54
55
        return is_file($custom) ? $custom : BASE_PATH.'/assets/error-503.html';
56
    }
57
}
58