Completed
Pull Request — master (#61)
by Tomas
02:14
created

ApiDecider::getApiHandler()   B

Complexity

Conditions 11
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 7.3166
c 0
b 0
f 0
cc 11
nc 4
nop 4
crap 11

1 Method

Rating   Name   Duplication   Size   Complexity  
A ApiDecider::enableGlobalPreflight() 0 7 2

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 27
    public function getApi(string $method, int $version, string $package, ?string $apiAction = null)
32
    {
33 27
        $method = strtoupper($method);
34 27
        $apiAction = $apiAction === '' ? null : $apiAction;
35
36 27
        foreach ($this->apis as $api) {
37 24
            $identifier = $api->getEndpoint();
38 24
            if ($method === $identifier->getMethod() && $identifier->getVersion() === $version && $identifier->getPackage() === $package && $identifier->getApiAction() === $apiAction) {
39 21
                $endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
40 21
                $api->getHandler()->setEndpointIdentifier($endpointIdentifier);
41 21
                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
     * Register new api handler
60
     *
61
     * @param EndpointInterface $endpointIdentifier
62
     * @param ApiHandlerInterface $handler
63
     * @param ApiAuthorizationInterface $apiAuthorization
64
     * @return self
65
     */
66 27
    public function addApi(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization): self
67
    {
68 27
        $this->apis[] = new Api($endpointIdentifier, $handler, $apiAuthorization);
69 27
        return $this;
70
    }
71
72
    /**
73
     * Get all registered apis
74
     *
75
     * @return Api[]
76
     */
77 12
    public function getApis(): array
78
    {
79 12
        return $this->apis;
80
    }
81
}
82