Completed
Push — master ( f71e62...3bdf74 )
by Tomas
02:54
created

ApiDecider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 81
ccs 28
cts 28
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
C getApiHandler() 0 23 11
A enableGlobalPreflight() 0 4 1
A addApiHandler() 0 9 1
A getHandlers() 0 4 1
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
     * @param string   $method
29
     * @param integer  $version
30
     * @param string   $package
31
     * @param string   $apiAction
32
     *
33
     * @return array
34
     */
35 24
    public function getApiHandler($method, $version, $package, $apiAction = '')
36
    {
37 24
        foreach ($this->handlers as $handler) {
38 21
            $identifier = $handler['endpoint'];
39 21
            if ($method == $identifier->getMethod() && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
40 18
                $endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
41 18
                $handler['handler']->setEndpointIdentifier($endpointIdentifier);
42 18
                return $handler;
43
            }
44 9
            if ($method == 'OPTIONS' && $this->globalPreflight && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
45
                return [
46 3
                    'endpoint' => new EndpointIdentifier('OPTION', $version, $package, $apiAction),
47 3
                    'authorization' => new NoAuthorization(),
48 7
                    'handler' => new CorsPreflightHandler(new Response()),
49 1
                ];
50
            }
51 3
        }
52
        return [
53 3
            'endpoint' => new EndpointIdentifier($method, $version, $package, $apiAction),
54 3
            'authorization' => new NoAuthorization(),
55 3
            '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 1
        ];
57
    }
58
59 3
    public function enableGlobalPreflight()
60
    {
61 3
        $this->globalPreflight = true;
62 3
    }
63
64
    /**
65
     * Register new api handler
66
     *
67
     * @param EndpointInterface         $endpointIdentifier
68
     * @param ApiHandlerInterface       $handler
69
     * @param ApiAuthorizationInterface $apiAuthorization
70
     *
71
     * @return $this
72
     */
73 24
    public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization)
74
    {
75 24
        $this->handlers[] = [
76 24
            'endpoint' => $endpointIdentifier,
77 24
            'handler' => $handler,
78 24
            'authorization' => $apiAuthorization,
79
        ];
80 24
        return $this;
81
    }
82
83
    /**
84
     * Get all registered handlers
85
     *
86
     * @return Handlers\ApiHandlerInterface[]
87
     */
88 12
    public function getHandlers()
89
    {
90 12
        return $this->handlers;
91
    }
92
}
93