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

ApiDecider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 0
loc 70
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C getApi() 0 18 12
A enableGlobalPreflight() 0 7 2
A addApi() 0 5 1
A getApis() 0 4 1
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