Shutdown::defaultHandler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Middleware to display temporary 503 maintenance pages.
11
 */
12
class Shutdown
13
{
14
    use Utils\CallableTrait;
15
16
    /**
17
     * @var callable|string The handler used
18
     */
19
    private $handler;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param callable|string|null $handler
25
     */
26
    public function __construct($handler = null)
27
    {
28
        $this->handler = $handler ?: self::class.'::defaultHandler';
29
    }
30
31
    /**
32
     * Execute the middleware.
33
     *
34
     * @param RequestInterface  $request
35
     * @param ResponseInterface $response
36
     * @param callable          $next
37
     *
38
     * @return ResponseInterface
39
     */
40
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
41
    {
42
        $response = $this->executeCallable($this->handler, $request, $response);
43
44
        return $response->withStatus(503);
45
    }
46
47
    public static function defaultHandler()
48
    {
49
        return <<<'EOT'
50
<!DOCTYPE html>
51
<html>
52
<head>
53
    <meta charset="utf-8">
54
    <title>503. Site under maintenance</title>
55
    <style>html{font-family: sans-serif;}</style>
56
    <meta name="viewport" content="width=device-width, initial-scale=1">
57
</head>
58
<body>
59
    <h1>Site under maintenance</h1>
60
</body>
61
</html>
62
EOT;
63
    }
64
}
65