|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Slim Framework (https://slimframework.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/slimphp/Slim |
|
6
|
|
|
* @copyright Copyright (c) 2011-2017 Josh Lockhart |
|
7
|
|
|
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Slim\Handlers; |
|
10
|
|
|
|
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use Slim\Http\Body; |
|
14
|
|
|
use UnexpectedValueException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Default Slim application not allowed handler |
|
18
|
|
|
* |
|
19
|
|
|
* It outputs a simple message in either JSON, XML or HTML based on the |
|
20
|
|
|
* Accept header. |
|
21
|
|
|
*/ |
|
22
|
|
|
class NotAllowed extends AbstractHandler |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Invoke error handler |
|
26
|
|
|
* |
|
27
|
|
|
* @param ServerRequestInterface $request The most recent Request object |
|
28
|
|
|
* @param ResponseInterface $response The most recent Response object |
|
29
|
|
|
* @param string[] $methods Allowed HTTP methods |
|
30
|
|
|
* |
|
31
|
|
|
* @return ResponseInterface |
|
32
|
|
|
* @throws UnexpectedValueException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($request->getMethod() === 'OPTIONS') { |
|
37
|
|
|
$status = 200; |
|
38
|
|
|
$contentType = 'text/plain'; |
|
39
|
|
|
$output = Render::make('PlainOptionsMessage', $methods); |
|
40
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
41
|
|
|
$status = 405; |
|
42
|
|
|
$contentType = $this->determineContentType($request); |
|
43
|
|
|
switch ($contentType) { |
|
44
|
|
|
case 'application/json': |
|
45
|
|
|
$output = Render::make('JsonNotAllowedMessage', $methods); |
|
46
|
|
|
break; |
|
47
|
|
|
|
|
48
|
|
|
case 'text/xml': |
|
49
|
|
|
case 'application/xml': |
|
50
|
|
|
$output = Render::make('XmlNotAllowedMessage', $methods); |
|
51
|
|
|
break; |
|
52
|
|
|
|
|
53
|
|
|
case 'text/html': |
|
54
|
|
|
$output = Render::make('HtmlNotAllowedMessage', $methods); |
|
55
|
|
|
break; |
|
56
|
|
|
default: |
|
57
|
|
|
throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$body = new Body(fopen('php://temp', 'r+')); |
|
62
|
|
|
$body->write($output); |
|
63
|
|
|
$allow = implode(', ', $methods); |
|
64
|
|
|
|
|
65
|
|
|
return $response |
|
66
|
|
|
->withStatus($status) |
|
67
|
|
|
->withHeader('Content-type', $contentType) |
|
68
|
|
|
->withHeader('Allow', $allow) |
|
69
|
|
|
->withBody($body); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
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.