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

NotAllowed::setCatalog()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
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 Allowed 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 NotAllowed extends AbstractHandler
24
{
25
    /**
26
     * HTTP methods allowed by the current request.
27
     *
28
     * @var string $methods
29
     */
30
    protected $methods;
31
32
    /**
33
     * Invoke "Not Allowed" Handler
34
     *
35
     * @param  ServerRequestInterface $request  The most recent Request object.
36
     * @param  ResponseInterface      $response The most recent Response object.
37
     * @param  string[]               $methods  Allowed HTTP methods.
38
     * @return ResponseInterface
39
     */
40
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
41
    {
42
        $this->setMethods($methods);
43
44
        if ($request->getMethod() === 'OPTIONS') {
45
            $status = 200;
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
            $status = 405;
50
            $contentType = $this->determineContentType($request);
51
            switch ($contentType) {
52
                case 'application/json':
53
                    $output = $this->renderJsonOutput();
54
                    break;
55
56
                case 'text/xml':
57
                case 'application/xml':
58
                    $output = $this->renderXmlOutput();
59
                    break;
60
61
                case 'text/html':
62
                default:
63
                    $output = $this->renderHtmlOutput();
64
                    break;
65
            }
66
        }
67
68
        $body = new Body(fopen('php://temp', 'r+'));
69
        $body->write($output);
70
        $allow = implode(', ', $methods);
71
72
        return $response
73
                ->withStatus($status)
74
                ->withHeader('Content-type', $contentType)
75
                ->withHeader('Allow', $allow)
76
                ->withBody($body);
77
    }
78
79
    /**
80
     * Set the HTTP methods allowed by the current request.
81
     *
82
     * @param  array $methods Case-sensitive array of methods.
83
     * @return NotAllowed Chainable
84
     */
85
    protected function setMethods(array $methods)
86
    {
87
        $this->methods = implode(', ', $methods);
88
89
        return $this;
90
    }
91
92
    /**
93
     * Retrieves the HTTP methods allowed by the current request.
94
     *
95
     * @return string Returns the allowed request methods.
96
     */
97
    public function methods()
98
    {
99
        return $this->methods;
100
    }
101
102
    /**
103
     * Render Plain/Text Error
104
     *
105
     * @return string
106
     */
107
    protected function renderPlainOutput()
108
    {
109
        $message = $this->translator()->translate('Allowed methods:').' '.$this->methods();
110
111
        return $this->render($message);
112
    }
113
114
    /**
115
     * Render JSON Error
116
     *
117
     * @return string
118
     */
119
    protected function renderJsonOutput()
120
    {
121
        $message = $this->translator()->translate('Method not allowed. Must be one of:').' '.$this->methods();
122
123
        return $this->render('{"message":"'.$message.'"}');
124
    }
125
126
    /**
127
     * Render XML Error
128
     *
129
     * @return string
130
     */
131
    protected function renderXmlOutput()
132
    {
133
        $message = $this->translator()->translate('Method not allowed. Must be one of:').' '.$this->methods();
134
135
        return $this->render('<root><message>'.$message.'</message></root>');
136
    }
137
138
    /**
139
     * Render title of error
140
     *
141
     * @return string
142
     */
143
    public function messageTitle()
144
    {
145
        return $this->translator()->translate('Method not allowed.');
146
    }
147
148
    /**
149
     * Render body of HTML error
150
     *
151
     * @return string
152
     */
153
    public function renderHtmlMessage()
154
    {
155
        $title   = $this->messageTitle();
156
        $notice  = $this->translator()->entry('Method not allowed. Must be one of:');
0 ignored issues
show
Bug introduced by
The method entry() does not seem to exist on object<Charcoal\Translator\Translator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
        $methods = $this->methods();
158
        $message = '<h1>'.$title."</h1>\n\t\t<p>".$notice.' <strong>'.$methods."</strong></p>\n";
159
160
        return $message;
161
    }
162
}
163