Completed
Push — master ( 9442e1...116254 )
by Tomas
02:50
created

ApiListingHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 97.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 92
ccs 42
cts 43
cp 0.9767
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 6 1
B getHandlersList() 0 24 5
A createParamsList() 0 17 3
1
<?php
2
3
namespace Tomaj\NetteApi\Handlers;
4
5
use Tomaj\NetteApi\ApiDecider;
6
use Tomaj\NetteApi\ApiResponse;
7
use Tomaj\NetteApi\Link\ApiLink;
8
9
class ApiListingHandler extends BaseHandler
10
{
11
    /**
12
     * @var ApiDecider
13
     */
14
    private $apiDecider;
15
16
    /**
17
     * @var ApiLink
18
     */
19
    private $apiLink;
20
21
    /**
22
     * ApiListingHandler constructor.
23
     *
24
     * @param ApiDecider  $apiDecider
25
     * @param ApiLink     $apiLink
26
     */
27 6
    public function __construct(ApiDecider $apiDecider, ApiLink $apiLink)
28
    {
29 6
        parent::__construct();
30 6
        $this->apiDecider = $apiDecider;
31 6
        $this->apiLink = $apiLink;
32 6
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 6
    public function handle($params)
38
    {
39 6
        $version = $this->getEndpoint()->getVersion();
40 6
        $endpoints = $this->getHandlersList($version);
41 6
        return new ApiResponse(200, ['endpoints' => $endpoints]);
42
    }
43
44
    /**
45
     * Create handler list for specified version
46
     *
47
     * @param integer $version
48
     *
49
     * @return array
50
     */
51 6
    private function getHandlersList($version)
52
    {
53 6
        $list = [];
54 6
        foreach ($this->apiDecider->getHandlers() as $handler) {
55 6
            $endpoint = $handler['endpoint'];
56 6
            if ($version && $version != $endpoint->getVersion()) {
57
                continue;
58
            }
59
            $item = [
60 6
                'method' => $endpoint->getMethod(),
61 6
                'version' => $endpoint->getVersion(),
62 6
                'package' => $endpoint->getPackage(),
63 6
                'api_action' => $endpoint->getApiAction(),
64 6
                'authorization' => get_class($handler['authorization']),
65 6
                'url' => $this->apiLink->link($endpoint),
66 6
            ];
67 6
            $params = $this->createParamsList($handler['handler']);
68 6
            if (count($params) > 0) {
69 3
                $item['params'] = $params;
70 3
            }
71 6
            $list[] = $item;
72 6
        }
73 6
        return $list;
74
    }
75
76
    /**
77
     * Create array with params for specified handler
78
     *
79
     * @param ApiHandlerInterface $handler
80
     *
81
     * @return array
82
     */
83 6
    private function createParamsList(ApiHandlerInterface $handler)
84
    {
85 6
        $paramsList = $handler->params();
86 6
        $params = [];
87 6
        foreach ($paramsList as $param) {
88
            $parameter = [
89 3
                'type' => $param->getType(),
90 3
                'key' => $param->getKey(),
91 3
                'is_required' => $param->isRequired(),
92 3
            ];
93 3
            if ($param->getAvailableValues()) {
94 3
                $parameter['available_values'] = $param->getAvailableValues();
95 3
            }
96 3
            $params[] = $parameter;
97 6
        }
98 6
        return $params;
99
    }
100
}
101