Completed
Push — master ( 9e89d5...89e339 )
by Tomas
11:24
created

ApiDecider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
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
use Tomaj\NetteApi\Link\ApiLink;
10
11
// TODO - refactor - remove array with triplet, replace with some class
12
13
class ApiDecider
14
{
15
    /**
16
     * @var ApiHandlerInterface[]
17
     */
18
    private $handlers = [];
19
20
    /**
21
     * @var ApiLink
22
     */
23
    private $apiLink;
24
25
    /**
26
     * ApiDecider constructor.
27
     *
28
     * @param ApiLink $apiLink
29
     */
30 12
    public function __construct(ApiLink $apiLink)
31
    {
32 12
        $this->apiLink = $apiLink;
33 12
    }
34
35
    /**
36
     * Get api handler that match input method, version, package and apiAction.
37
     * If decider cannot find handler for given handler, returns defaults.
38
     *
39
     * @param string   $method
40
     * @param integer  $version
41
     * @param string   $package
42
     * @param string   $apiAction
43
     *
44
     * @return array
45
     */
46 9
    public function getApiHandler($method, $version, $package, $apiAction = '')
47
    {
48 9
        foreach ($this->handlers as $handler) {
49 6
            $identifier = $handler['endpoint'];
50 6
            if ($method == $identifier->getMethod() && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
51 6
                $endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
52 6
                $handler['handler']->setEndpointIdentifier($endpointIdentifier);
53 6
                return $handler;
54
            }
55 6
        }
56
        return [
57 3
            'endpoint' => new EndpointIdentifier($method, $version, $package, $apiAction),
58 3
            'authorization' => new NoAuthorization(),
59 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...
60 3
        ];
61
    }
62
63
    /**
64
     * Register new api handler
65
     *
66
     * @param EndpointInterface         $endpointIdentifier
67
     * @param ApiHandlerInterface       $handler
68
     * @param ApiAuthorizationInterface $apiAuthorization
69
     *
70
     * @return $this
71
     */
72 9
    public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization)
73
    {
74 9
        $this->handlers[] = [
75 9
            'endpoint' => $endpointIdentifier,
76 9
            'handler' => $handler,
77 9
            'authorization' => $apiAuthorization,
78
        ];
79 9
        return $this;
80
    }
81
82
    /**
83
     * Get all registered handlers
84
     *
85
     * @return Handlers\ApiHandlerInterface[]
86
     */
87 6
    public function getHandlers()
88
    {
89 6
        return $this->handlers;
90
    }
91
92
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
    public function getHandlersList($version)
94
    {
95
        $list = [];
96
        foreach ($this->getHandlers() as $handler) {
97
            $endpoint = $handler['endpoint'];
98
            if ($version && $version != $endpoint->getVersion()) {
99
                continue;
100
            }
101
            $item = [
102
                'method' => $endpoint->getMethod(),
103
                'version' => $endpoint->getVersion(),
104
                'package' => $endpoint->getPackage(),
105
                'api_action' => $endpoint->getApiAction(),
106
                'url' => $this->apiLink->link($endpoint),
107
            ];
108
            $params = $this->createParamsList($handler);
109
            if (count($params) > 0) {
110
                $item['params'] = $params;
111
            }
112
            $list[] = $item;
113
        }
114
        return $list;
115
    }
116
    
117
    private function createParamsList($handler)
118
    {
119
        $paramsList = $handler['handler']->params();
120
        $params = [];
121
        foreach ($paramsList as $param) {
122
            $parameter = [
123
                'type' => $param->getType(),
124
                'key' => $param->getKey(),
125
                'is_required' => $param->isRequired(),
126
            ];
127
            if ($param->getAvailableValues()) {
128
                $parameter['available_values'] = $param->getAvailableValues();
129
            }
130
            $params[] = $parameter;
131
        }
132
        return $params;
133
    }
134
    */
135
}
136