Completed
Pull Request — master (#3)
by
unknown
02:52
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
// Local Dependencies
10
use Charcoal\App\Handler\AbstractHandler;
11
12
/**
13
 * Not Found Handler
14
 *
15
 * Enhanced version of {@see \Slim\Handlers\NotAllowed}.
16
 *
17
 * It outputs a simple message in either JSON, XML,
18
 * or HTML based on the Accept header.
19
 */
20
class NotFound extends AbstractHandler
21
{
22
    /**
23
     * Invoke "Not Found" Handler
24
     *
25
     * @param  ServerRequestInterface $request  The most recent Request object.
26
     * @param  ResponseInterface      $response The most recent Response object.
27
     * @return ResponseInterface
28
     */
29
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
30
    {
31
        $contentType = $this->determineContentType($request);
32
        switch ($contentType) {
33
            case 'application/json':
34
                $output = $this->renderJsonOutput();
35
                break;
36
37
            case 'text/xml':
38
            case 'application/xml':
39
                $output = $this->renderXmlOutput();
40
                break;
41
42
            case 'text/html':
43
            default:
44
                $output = $this->renderHtmlOutput();
45
        }
46
47
        $response->getBody()->write($output);
48
49
        return $response->withStatus(404)
50
                        ->withHeader('Content-Type', $contentType);
51
    }
52
53
    /**
54
     * Render Plain/Text Error
55
     *
56
     * @return string
57
     */
58
    protected function renderPlainOutput()
59
    {
60
        $message = $this->translator()->translate('Not Found');
61
62
        return $this->render($message);
63
    }
64
65
    /**
66
     * Render JSON Error
67
     *
68
     * @return string
69
     */
70
    protected function renderJsonOutput()
71
    {
72
        $message = $this->translator()->translate('Not Found');
73
74
        return $this->render('{"message":"'.$message.'"}');
75
    }
76
77
    /**
78
     * Render XML Error
79
     *
80
     * @return string
81
     */
82
    protected function renderXmlOutput()
83
    {
84
        $message = $this->translator()->translate('Not Found');
85
86
        return $this->render('<root><message>'.$message.'</message></root>');
87
    }
88
89
    /**
90
     * Render title of error
91
     *
92
     * @return string
93
     */
94
    public function messageTitle()
95
    {
96
        return $this->translator()->translate('Page Not Found');
97
    }
98
99
    /**
100
     * Render body of HTML error
101
     *
102
     * @return string
103
     */
104
    public function renderHtmlMessage()
105
    {
106
        $title = $this->messageTitle();
107
        $link  = sprintf(
108
            '<a href="%1$s">%2$s</a>',
109
            $this->baseUrl(),
110
            $this->translator()->translate('Visit the Home Page')
111
        );
112
        $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.');
113
        $message = '<h1>'.$title."</h1>\n\t\t<p>".$notice."</p>\n\t\t<p>".$link."</p>\n";
114
115
        return $message;
116
    }
117
}
118