NotAllowedException   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 48
ccs 17
cts 17
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethods() 0 15 3
A __construct() 0 14 2
1
<?php
2
/**
3
 * Exception with 405 HTTP code
4
 *
5
 * https://tools.ietf.org/html/rfc7231#page-59
6
 *
7
 * @file      UserErrorHandler.php
8
 *
9
 * PHP version 8.0+
10
 *
11
 * @author    Yancharuk Alexander <alex at itvault dot info>
12
 * @copyright © 2012-2021 Alexander Yancharuk
13
 * @date      2015-08-11 21:44
14
 * @license   The BSD 3-Clause License
15
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
16
 */
17
18
namespace Veles\Exceptions\Http;
19
20
use ReflectionClass;
21
use Veles\Controllers\BaseController;
22
23
/**
24
 * Class   NotAllowedException
25
 *
26
 * @author Yancharuk Alexander <alex at itvault dot info>
27
 */
28
class NotAllowedException extends HttpResponseException
29
{
30
	/**
31
	 * Throw exception with 405 HTTP-code
32
	 *
33
	 * According standard http-response "Not Allowed" MUST contain "Allowed"
34
	 * header with available http-methods
35
	 *
36
	 * @param BaseController $controller
37
	 */
38 7
	public function __construct(BaseController $controller)
39
	{
40 7
		parent::__construct();
41 7
		header('HTTP/1.1 405 Method Not Allowed', true, 405);
42
43 7
		$methods = $this->getMethods($controller);
44
45 7
		if ('' !== $methods) {
46 7
			header("Allowed: $methods", true);
47
		}
48
49 7
		$method = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_ENCODED);
50
51 7
		$this->message = "Method $method not allowed";
52
	}
53
54
	/**
55
	 * Get list of allowed HTTP-methods
56
	 *
57
	 * @param BaseController $class
58
	 *
59
	 * @return string
60
	 */
61 7
	protected function getMethods(BaseController $class)
62
	{
63 7
		$reflection = new ReflectionClass($class);
64 7
		$class_name = get_class($class);
65
66 7
		$methods = [];
67 7
		foreach ($reflection->getMethods() as $value) {
68 7
			if ($value->class !== $class_name) {
69 7
				continue;
70
			}
71
72 7
			$methods[] = $value->name;
73
		}
74
75 7
		return strtoupper(implode(', ', $methods));
76
	}
77
}
78