Completed
Pull Request — master (#25)
by Michal
15:14 queued 12:43
created

ApiDecider::setTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Tomaj\NetteApi;
4
5
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
6
use Tomaj\NetteApi\Authorization\NoAuthorization;
7
use Tomaj\NetteApi\Handlers\ApiHandlerInterface;
8
use Tomaj\NetteApi\Handlers\DefaultHandler;
9
10
class ApiDecider
11
{
12
    /** @var ApiHandlerInterface[] */
13
    private $handlers = [];
14
15
    /** @var string */
16
    private $title;
17
    
18
    /** @var string */
19
    private $description;
20
    
21
    /**
22
     * Get api handler that match input method, version, package and apiAction.
23
     * If decider cannot find handler for given handler, returns defaults.
24
     *
25
     * @param string   $method
26
     * @param integer  $version
27
     * @param string   $package
28
     * @param string   $apiAction
29
     *
30
     * @return array
31
     */
32 21
    public function getApiHandler($method, $version, $package, $apiAction = '')
33
    {
34 21
        foreach ($this->handlers as $handler) {
35 18
            $identifier = $handler['endpoint'];
36 18
            if ($method == $identifier->getMethod() && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
37 18
                $endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
38 18
                $handler['handler']->setEndpointIdentifier($endpointIdentifier);
39 18
                return $handler;
40
            }
41 9
        }
42
        return [
43 3
            'endpoint' => new EndpointIdentifier($method, $version, $package, $apiAction),
44 3
            'authorization' => new NoAuthorization(),
45 3
            '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...
46 3
        ];
47
    }
48
49
    /**
50
     * Register new api handler
51
     *
52
     * @param EndpointInterface         $endpointIdentifier
53
     * @param ApiHandlerInterface       $handler
54
     * @param ApiAuthorizationInterface $apiAuthorization
55
     *
56
     * @return $this
57
     */
58 21
    public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization)
59
    {
60 21
        $this->handlers[] = [
61 21
            'endpoint' => $endpointIdentifier,
62 21
            'handler' => $handler,
63 21
            'authorization' => $apiAuthorization,
64
        ];
65 21
        return $this;
66
    }
67
68
    /**
69
     * Get all registered handlers
70
     *
71
     * @return ApiHandlerInterface[]
72
     */
73 9
    public function getHandlers()
74
    {
75 9
        return $this->handlers;
76
    }
77
    
78
    /**
79
     * @param string $title
80
     * @return ApiDecider
81
     */
82
    public function setTitle($title)
83
    {
84
        $this->title = $title;
85
        return $this;
86
    }
87
    
88
    /**
89
     * @return string
90
     */
91
    public function getTitle()
92
    {
93
        return $this->title;
94
    }
95
96
    /**
97
     * @param string $description
98
     * @return ApiDecider
99
     */
100
    public function setDescription($description)
101
    {
102
        $this->description = $description;
103
        return $this;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getDescription()
110
    {
111
        return $this->description;
112
    }
113
}
114