Action   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 37
c 5
b 0
f 1
dl 0
loc 77
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPattern() 0 3 1
A calculateCallable() 0 25 5
A getCallable() 0 7 2
A __construct() 0 12 2
A getMethod() 0 3 1
1
<?php
2
3
namespace SlimX\Controllers;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Slim\Http\Response;
8
9
class Action
10
{
11
    private $allowedMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
12
    protected $method;
13
    protected $pattern;
14
    protected $callable;
15
    protected $errorCallable;
16
17
    /**
18
     * Organize the action.
19
     *
20
     * @param string $method An HTTP method supported by Slim.
21
     * @param string $pattern URI to route the request.
22
     * @param mixed $callable Either an anonymous function or an array with keys
23
     *  matching the HTTP_ACCEPT and values being the anonymous function to be
24
     *  called.
25
     * @param ?callable $errorCallable Optionally, a callback function may be given, to be
26
     *  called when ACCEPT header doesn't match any of the expected.
27
     */
28
    public function __construct(string $method, string $pattern, $callable, ?callable $errorCallable = null)
29
    {
30
        if (!in_array($method, $this->allowedMethods)) {
31
            throw new \Exception(
32
                "Http method $method is not allowed. List of allowed methods: " .
33
                implode(', ', $this->allowedMethods)
34
            );
35
        }
36
        $this->method = $method;
37
        $this->pattern = $pattern;
38
        $this->callable = $callable;
39
        $this->errorCallable = $errorCallable;
40
    }
41
42
    public function getMethod()
43
    {
44
        return $this->method;
45
    }
46
47
    public function getPattern()
48
    {
49
        return $this->pattern;
50
    }
51
52
    public function getCallable()
53
    {
54
        if (!is_callable($this->callable)) {
55
            $this->calculateCallable();
56
        }
57
58
        return $this->callable;
59
    }
60
61
    private function calculateCallable()
62
    {
63
        $callable = $this->callable;
64
        $errorCallable = $this->errorCallable;
65
        $this->callable = function (
66
            RequestInterface $request,
67
            Response $response,
68
            array $args
69
        ) use (
70
            $callable,
71
            $errorCallable
72
        ) {
73
            $headers = $request->getHeaders();
74
            if (isset($headers['HTTP_ACCEPT'])) {
75
                foreach ($headers['HTTP_ACCEPT'] as $accept) {
76
                    if (isset($callable[$accept])) {
77
                        return $callable[$accept]($request, $response, $args);
78
                    }
79
                }
80
            }
81
82
            return null !== $errorCallable ?
83
                $errorCallable($request, $response) :
84
                $response->withStatus(406)
85
                    ->write('API version not present or not accepted');
86
        };
87
    }
88
}
89