Completed
Pull Request — master (#61)
by Tomas
03:17
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 24
    public function getApi(string $method, int $version, string $package, ?string $apiAction = null)
32
    {
33 24
        foreach ($this->apis as $api) {
34 21
            $identifier = $api->getEndpoint();
35 21
            if ($method == $identifier->getMethod() && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
36 18
                $endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
37 18
                $api->getHandler()->setEndpointIdentifier($endpointIdentifier);
38 18
                return $api;
39
            }
40 9
            if ($method == 'OPTIONS' && $this->globalPreflightHandler && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
41 5
                return new Api(new EndpointIdentifier('OPTION', $version, $package, $apiAction), $this->globalPreflightHandler, new NoAuthorization());
42
            }
43
        }
44 3
        return new Api(new EndpointIdentifier($method, $version, $package, $apiAction), new DefaultHandler(), new NoAuthorization());
45
    }
46
47 3
    public function enableGlobalPreflight(ApiHandlerInterface $corsHandler = null)
48
    {
49 3
        if (!$corsHandler) {
50 3
            $corsHandler = new CorsPreflightHandler(new Response());
51
        }
52 3
        $this->globalPreflightHandler = $corsHandler;
53 3
    }
54
55
    /**
56
     * @deprecated use addApi instead
57
     */
58
    public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization)
59
    {
60
        return $this->addApi($endpointIdentifier, $handler, $apiAuthorization);
61
    }
62
63
    /**
64
     * Register new api handler
65
     *
66
     * @param EndpointInterface $endpointIdentifier
67
     * @param ApiHandlerInterface $handler
68
     * @param ApiAuthorizationInterface $apiAuthorization
69
     * @return self
70
     */
71 24
    public function addApi(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization): self
72
    {
73 24
        $this->apis[] = new Api($endpointIdentifier, $handler, $apiAuthorization);
74 24
        return $this;
75
    }
76
77
    /**
78
     * Get all registered apis
79
     *
80
     * @return Api[]
81
     */
82 12
    public function getApis(): array
83
    {
84 12
        return $this->apis;
85
    }
86
}
87