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