Completed
Pull Request — master (#3)
by
unknown
02:28
created

Shutdown::setCatalog()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 1
1
<?php
2
3
namespace Charcoal\App\Handler;
4
5
// Dependencies from PSR-7 (HTTP Messaging)
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
// Dependency from Slim
10
use Slim\Http\Body;
11
12
// Local Dependencies
13
use Charcoal\App\Handler\AbstractHandler;
14
15
/**
16
 * Shutdown Handler
17
 *
18
 * It outputs a simple message in either JSON, XML, or HTML based on the Accept header.
19
 *
20
 * A maintenance mode check is included in the default middleware stack for your application.
21
 * This is a practical feature to "disable" your application while performing an update
22
 * or maintenance.
23
 */
24
class Shutdown extends AbstractHandler
25
{
26
    /**
27
     * HTTP methods allowed by the current request.
28
     *
29
     * @var string $methods
30
     */
31
    protected $methods;
32
33
    /**
34
     * Invoke "Maintenance" Handler
35
     *
36
     * @param  ServerRequestInterface $request  The most recent Request object.
37
     * @param  ResponseInterface      $response The most recent Response object.
38
     * @param  string[]               $methods  Allowed HTTP methods.
39
     * @return ResponseInterface
40
     */
41
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
42
    {
43
        $this->setMethods($methods);
44
45
        if ($request->getMethod() === 'OPTIONS') {
46
            $contentType = 'text/plain';
47
            $output = $this->renderPlainOutput();
48 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
49
            $contentType = $this->determineContentType($request);
50
            switch ($contentType) {
51
                case 'application/json':
52
                    $output = $this->renderJsonOutput();
53
                    break;
54
55
                case 'text/xml':
56
                case 'application/xml':
57
                    $output = $this->renderXmlOutput();
58
                    break;
59
60
                case 'text/html':
61
                default:
62
                    $output = $this->renderHtmlOutput();
63
                    break;
64
            }
65
        }
66
67
        $body = new Body(fopen('php://temp', 'r+'));
68
        $body->write($output);
69
70
        return $response
71
                ->withStatus(503)
72
                ->withHeader('Content-type', $contentType)
73
                ->withBody($body);
74
    }
75
76
    /**
77
     * Set the HTTP methods allowed by the current request.
78
     *
79
     * @param  array $methods Case-sensitive array of methods.
80
     * @return Shutdown Chainable
81
     */
82
    protected function setMethods(array $methods)
83
    {
84
        $this->methods = implode(', ', $methods);
85
86
        return $this;
87
    }
88
89
    /**
90
     * Retrieves the HTTP methods allowed by the current request.
91
     *
92
     * @return string Returns the allowed request methods.
93
     */
94
    public function methods()
95
    {
96
        return $this->methods;
97
    }
98
99
    /**
100
     * Render Plain/Text Error
101
     *
102
     * @return string
103
     */
104
    protected function renderPlainOutput()
105
    {
106
        $message = $this->translator()->translate('Down for maintenance!');
107
108
        return $this->render($message);
109
    }
110
111
    /**
112
     * Render JSON Error
113
     *
114
     * @return string
115
     */
116
    protected function renderJsonOutput()
117
    {
118
        $message = $this->translator()->translate('We are currently unavailable. Check back in 15 minutes.');
119
120
        return $this->render('{"message":"'.$message.'"}');
121
    }
122
123
    /**
124
     * Render XML Error
125
     *
126
     * @return string
127
     */
128
    protected function renderXmlOutput()
129
    {
130
        $message = $this->translator()->translate('We are currently unavailable. Check back in 15 minutes.');
131
132
        return $this->render('<root><message>'.$message.'</message></root>');
133
    }
134
135
    /**
136
     * Render title of error
137
     *
138
     * @return string
139
     */
140
    public function messageTitle()
141
    {
142
        return $this->translator()->translate('Down for maintenance!');
143
    }
144
145
    /**
146
     * Render body of HTML error
147
     *
148
     * @return string
149
     */
150
    public function renderHtmlMessage()
151
    {
152
        $title   = $this->messageTitle();
153
        $notice  = $this->translator()->translate('currently-unavailable');
154
        $message = '<h1>'.$title."</h1>\n\t\t<p>".$notice."</p>\n";
155
156
        return $message;
157
    }
158
}
159