Completed
Pull Request — master (#61)
by Michal
04:05
created

ApiDecider::getApi()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 6.9666
c 0
b 0
f 0
cc 12
nc 8
nop 4
crap 12

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
    /** @var Api[] */
15
    private $apis = [];
16
17
    /** @var ApiHandlerInterface|null */
18
    private $globalPreflightHandler = null;
19
20
    /**
21
     * Get api handler that match input method, version, package and apiAction.
22
     * If decider cannot find handler for given handler, returns defaults.
23
     *
24
     * @param string   $method
25
     * @param integer  $version
26
     * @param string   $package
27
     * @param string   $apiAction
28
     *
29
     * @return Api
30
     */
31 24
    public function getApi(string $method, int $version, string $package, ?string $apiAction = null)
32
    {
33 24
        $method = strtoupper($method);
34 24
        $apiAction = $apiAction === '' ? null : $apiAction;
35
36 24
        foreach ($this->apis as $api) {
37 21
            $identifier = $api->getEndpoint();
38 21
            if ($method === $identifier->getMethod() && $identifier->getVersion() === $version && $identifier->getPackage() === $package && $identifier->getApiAction() === $apiAction) {
39 18
                $endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
40 18
                $api->getHandler()->setEndpointIdentifier($endpointIdentifier);
41 18
                return $api;
42
            }
43 9
            if ($method === 'OPTIONS' && $this->globalPreflightHandler && $identifier->getVersion() === $version && $identifier->getPackage() === $package && $identifier->getApiAction() === $apiAction) {
44 5
                return new Api(new EndpointIdentifier('OPTIONS', $version, $package, $apiAction), $this->globalPreflightHandler, new NoAuthorization());
45
            }
46
        }
47 3
        return new Api(new EndpointIdentifier($method, $version, $package, $apiAction), new DefaultHandler(), new NoAuthorization());
48
    }
49
50 3
    public function enableGlobalPreflight(ApiHandlerInterface $corsHandler = null)
51
    {
52 3
        if (!$corsHandler) {
53 3
            $corsHandler = new CorsPreflightHandler(new Response());
54
        }
55 3
        $this->globalPreflightHandler = $corsHandler;
56 3
    }
57
58
    /**
59
     * @deprecated use addApi instead
60
     */
61
    public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization)
62
    {
63
        return $this->addApi($endpointIdentifier, $handler, $apiAuthorization);
64
    }
65
66
    /**
67
     * Register new api handler
68
     *
69
     * @param EndpointInterface $endpointIdentifier
70
     * @param ApiHandlerInterface $handler
71
     * @param ApiAuthorizationInterface $apiAuthorization
72
     * @return self
73
     */
74 24
    public function addApi(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization): self
75
    {
76 24
        $this->apis[] = new Api($endpointIdentifier, $handler, $apiAuthorization);
77 24
        return $this;
78
    }
79
80
    /**
81
     * Get all registered apis
82
     *
83
     * @return Api[]
84
     */
85 12
    public function getApis(): array
86
    {
87 12
        return $this->apis;
88
    }
89
}
90