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

NotFound::setCatalog()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 29
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
 * Not Found Handler
17
 *
18
 * Enhanced version of {@see \Slim\Handlers\NotAllowed}.
19
 *
20
 * It outputs a simple message in either JSON, XML,
21
 * or HTML based on the Accept header.
22
 */
23
class NotFound extends AbstractHandler
24
{
25
    /**
26
     * Invoke "Not Found" Handler
27
     *
28
     * @param  ServerRequestInterface $request  The most recent Request object.
29
     * @param  ResponseInterface      $response The most recent Response object.
30
     * @return ResponseInterface
31
     */
32
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
33
    {
34
        $contentType = $this->determineContentType($request);
35
        switch ($contentType) {
36
            case 'application/json':
37
                $output = $this->renderJsonOutput();
38
                break;
39
40
            case 'text/xml':
41
            case 'application/xml':
42
                $output = $this->renderXmlOutput();
43
                break;
44
45
            case 'text/html':
46
            default:
47
                $output = $this->renderHtmlOutput();
48
        }
49
50
        $body = new Body(fopen('php://temp', 'r+'));
51
        $body->write($output);
52
53
        return $response->withStatus(404)
54
                        ->withHeader('Content-Type', $contentType)
55
                        ->withBody($body);
56
    }
57
58
    /**
59
     * Render Plain/Text Error
60
     *
61
     * @return string
62
     */
63
    protected function renderPlainOutput()
64
    {
65
        $message = $this->translator()->translate('Not Found');
66
67
        return $this->render($message);
68
    }
69
70
    /**
71
     * Render JSON Error
72
     *
73
     * @return string
74
     */
75
    protected function renderJsonOutput()
76
    {
77
        $message = $this->translator()->translate('Not Found');
78
79
        return $this->render('{"message":"'.$message.'"}');
80
    }
81
82
    /**
83
     * Render XML Error
84
     *
85
     * @return string
86
     */
87
    protected function renderXmlOutput()
88
    {
89
        $message = $this->translator()->translate('Not Found');
90
91
        return $this->render('<root><message>'.$message.'</message></root>');
92
    }
93
94
    /**
95
     * Render title of error
96
     *
97
     * @return string
98
     */
99
    public function messageTitle()
100
    {
101
        return $this->translator()->translate('Page Not Found');
102
    }
103
104
    /**
105
     * Render body of HTML error
106
     *
107
     * @return string
108
     */
109
    public function renderHtmlMessage()
110
    {
111
        $title = $this->messageTitle();
112
        $link  = sprintf(
113
            '<a href="%1$s">%2$s</a>',
114
            $this->baseUrl(),
115
            $this->translator()->translate('Visit the Home Page')
116
        );
117
        $notice  = $this->translator()->translate('The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.');
118
        $message = '<h1>'.$title."</h1>\n\t\t<p>".$notice."</p>\n\t\t<p>".$link."</p>\n";
119
120
        return $message;
121
    }
122
}
123