Completed
Pull Request — master (#31)
by Tomas
04:30
created

ApiDecider::getApiHandler()   C

Complexity

Conditions 11
Paths 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 11

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 9
cts 9
cp 1
rs 5.3929
cc 11
eloc 16
nc 4
nop 4
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tomaj\NetteApi;
4
5
use Nette\Http\Response;
6
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
7
use Tomaj\NetteApi\Authorization\NoAuthorization;
8
use Tomaj\NetteApi\Handlers\ApiHandlerInterface;
9
use Tomaj\NetteApi\Handlers\CorsPreflightHandler;
10
use Tomaj\NetteApi\Handlers\DefaultHandler;
11
12
class ApiDecider
13
{
14
    /**
15
     * @var ApiHandlerInterface[]
16
     */
17
    private $handlers = [];
18
19
    /**
20
     * @var bool
21
     */
22
    private $globalPreflight = false;
23
24
    /**
25
     * Get api handler that match input method, version, package and apiAction.
26
     * If decider cannot find handler for given handler, returns defaults.
27
     *
28 21
     * @param string   $method
29 2
     * @param integer  $version
30 21
     * @param string   $package
31 18
     * @param string   $apiAction
32 18
     *
33 18
     * @return array
34 18
     */
35 18
    public function getApiHandler($method, $version, $package, $apiAction = '')
36
    {
37 6
        foreach ($this->handlers as $handler) {
38
            $identifier = $handler['endpoint'];
39 3
            if ($method == $identifier->getMethod() && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
40 3
                $endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
41 3
                $handler['handler']->setEndpointIdentifier($endpointIdentifier);
42 2
                return $handler;
43
            }
44
            if ($method == 'OPTIONS' && $this->globalPreflight && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
45
                return [
46
                    'endpoint' => new EndpointIdentifier('OPTION', $version, $package, $apiAction),
47
                    'authorization' => new NoAuthorization(),
48
                    'handler' => new CorsPreflightHandler(new Response()),
49
                ];
50
            }
51
        }
52
        return [
53
            'endpoint' => new EndpointIdentifier($method, $version, $package, $apiAction),
54 21
            'authorization' => new NoAuthorization(),
55
            'handler' => new DefaultHandler($version, $package, $apiAction)
0 ignored issues
show
Unused Code introduced by
The call to DefaultHandler::__construct() has too many arguments starting with $version.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
56 21
        ];
57 21
    }
58 21
59 21
    public function enableGlobalPreflight()
60
    {
61 21
        $this->globalPreflight = true;
62
    }
63
64
    /**
65
     * Register new api handler
66
     *
67
     * @param EndpointInterface         $endpointIdentifier
68
     * @param ApiHandlerInterface       $handler
69 9
     * @param ApiAuthorizationInterface $apiAuthorization
70
     *
71 9
     * @return $this
72
     */
73
    public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization)
74
    {
75
        $this->handlers[] = [
76
            'endpoint' => $endpointIdentifier,
77
            'handler' => $handler,
78
            'authorization' => $apiAuthorization,
79
        ];
80
        return $this;
81
    }
82
83
    /**
84
     * Get all registered handlers
85
     *
86
     * @return Handlers\ApiHandlerInterface[]
87
     */
88
    public function getHandlers()
89
    {
90
        return $this->handlers;
91
    }
92
}
93