Completed
Push — master ( 8efb3c...7a5433 )
by Tomas
04:16
created

ApiDecider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tomaj\NetteApi;
4
5
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
6
use Tomaj\NetteApi\Authorization\NoAuthorization;
7
use Tomaj\NetteApi\Handlers\ApiHandlerInterface;
8
use Tomaj\NetteApi\Handlers\DefaultHandler;
9
10
class ApiDecider
11
{
12
    /**
13
     * @var ApiHandlerInterface[]
14
     */
15
    private $handlers = [];
16
17
    /**
18
     * Get api handler that match input method, version, package and apiAction.
19
     * If decider cannot find handler for given handler, returns defaults.
20
     *
21
     * @param string   $method
22
     * @param integer  $version
23
     * @param string   $package
24
     * @param string   $apiAction
25
     *
26
     * @return array
27
     */
28 12
    public function getApiHandler($method, $version, $package, $apiAction = '')
29 3
    {
30 12
        foreach ($this->handlers as $handler) {
31 9
            $identifier = $handler['endpoint'];
32 9
            if ($method == $identifier->getMethod() && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
33 9
                $endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
34 9
                $handler['handler']->setEndpointIdentifier($endpointIdentifier);
35 9
                return $handler;
36
            }
37 9
        }
38
        return [
39 3
            'endpoint' => new EndpointIdentifier($method, $version, $package, $apiAction),
40 3
            'authorization' => new NoAuthorization(),
41 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...
42 3
        ];
43
    }
44
45
    /**
46
     * Register new api handler
47
     *
48
     * @param EndpointInterface         $endpointIdentifier
49
     * @param ApiHandlerInterface       $handler
50
     * @param ApiAuthorizationInterface $apiAuthorization
51
     *
52
     * @return $this
53
     */
54 12
    public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization)
55
    {
56 12
        $this->handlers[] = [
57 12
            'endpoint' => $endpointIdentifier,
58 12
            'handler' => $handler,
59 12
            'authorization' => $apiAuthorization,
60
        ];
61 12
        return $this;
62
    }
63
64
    /**
65
     * Get all registered handlers
66
     *
67
     * @return Handlers\ApiHandlerInterface[]
68
     */
69 9
    public function getHandlers()
70
    {
71 9
        return $this->handlers;
72
    }
73
}
74