Completed
Pull Request — master (#31)
by Tomas
02:19
created

ApiDecider::enableGlobalPreflight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
    /**
15
     * @var ApiHandlerInterface[]
16
     */
17
    private $handlers = [];
18
19
    /**
20
     * @var bool
21
     */
22
    private $globalPreflight = false;
23
24
    /**
25
     * Get api handler that match input method, version, package and apiAction.
26
     * If decider cannot find handler for given handler, returns defaults.
27
     *
28 21
     * @param string   $method
29 2
     * @param integer  $version
30 21
     * @param string   $package
31 18
     * @param string   $apiAction
32 18
     *
33 18
     * @return array
34 18
     */
35 18
    public function getApiHandler($method, $version, $package, $apiAction = '')
36
    {
37 6
        foreach ($this->handlers as $handler) {
38
            $identifier = $handler['endpoint'];
39 3
            if ($method == $identifier->getMethod() && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
40 3
                $endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
41 3
                $handler['handler']->setEndpointIdentifier($endpointIdentifier);
42 2
                return $handler;
43
            }
44
            if ($method == 'OPTIONS'  && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
45
                return [
46
                    'endpoint' => new EndpointIdentifier('OPTION', $version, $package, $apiAction),
47
                    'authorization' => new NoAuthorization(),
48
                    'handler' => new CorsPreflightHandler(new Response()),
49
                ];
50
            }
51
        }
52
        return [
53
            'endpoint' => new EndpointIdentifier($method, $version, $package, $apiAction),
54 21
            'authorization' => new NoAuthorization(),
55
            'handler' => new DefaultHandler($version, $package, $apiAction)
0 ignored issues
show
Unused Code introduced by
The call to DefaultHandler::__construct() has too many arguments starting with $version.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
56 21
        ];
57 21
    }
58 21
59 21
    public function enableGlobalPreflight()
60
    {
61 21
        $this->globalPreflight = true;
62
    }
63
64
    /**
65
     * Register new api handler
66
     *
67
     * @param EndpointInterface         $endpointIdentifier
68
     * @param ApiHandlerInterface       $handler
69 9
     * @param ApiAuthorizationInterface $apiAuthorization
70
     *
71 9
     * @return $this
72
     */
73
    public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization)
74
    {
75
        $this->handlers[] = [
76
            'endpoint' => $endpointIdentifier,
77
            'handler' => $handler,
78
            'authorization' => $apiAuthorization,
79
        ];
80
//        if ($this->globalPreflight) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
//            $this->handlers[] = [
82
//                'endpoint' => new EndpointIdentifier('OPTION', $endpointIdentifier->getVersion(), $endpointIdentifier->getPackage(), $endpointIdentifier->getApiAction()),
83
//                'handler' => new CorsPreflightHandler(new Response()),
84
//                'authorization' => $apiAuthorization,
85
//            ];
86
//        }
87
        return $this;
88
    }
89
90
    /**
91
     * Get all registered handlers
92
     *
93
     * @return Handlers\ApiHandlerInterface[]
94
     */
95
    public function getHandlers()
96
    {
97
        return $this->handlers;
98
    }
99
}
100