Completed
Push — master ( e0250b...fc23d8 )
by Tomas
02:44
created

ApiListingHandler::createParamsList()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 10
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Tomaj\NetteApi\Handlers;
4
5
use Tomaj\NetteApi\ApiDecider;
6
use Tomaj\NetteApi\Link\ApiLink;
7
use Tomaj\NetteApi\Params\InputParam;
8
use Tomaj\NetteApi\Response\JsonApiResponse;
9
10
class ApiListingHandler extends BaseHandler
11
{
12
    /**
13
     * @var ApiDecider
14
     */
15
    private $apiDecider;
16
17
    /**
18
     * @var ApiLink
19
     */
20
    private $apiLink;
21
22
    /**
23
     * ApiListingHandler constructor.
24
     *
25
     * @param ApiDecider  $apiDecider
26
     * @param ApiLink     $apiLink
27
     */
28 6
    public function __construct(ApiDecider $apiDecider, ApiLink $apiLink)
29
    {
30 6
        parent::__construct();
31 6
        $this->apiDecider = $apiDecider;
32 6
        $this->apiLink = $apiLink;
33 6
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 6
    public function handle($params)
39
    {
40 6
        $version = $this->getEndpoint()->getVersion();
41 6
        $endpoints = $this->getHandlersList($version);
42 6
        return new JsonApiResponse(200, ['endpoints' => $endpoints]);
43
    }
44
45
    /**
46
     * Create handler list for specified version
47
     *
48
     * @param integer $version
49
     *
50
     * @return array
51
     */
52 6
    private function getHandlersList($version)
53
    {
54
        $versionHandlers = array_filter($this->apiDecider->getHandlers(), function ($handler) use ($version) {
55 6
            return $version == $handler['endpoint']->getVersion();
56 6
        });
57
58
        return array_map(function ($handler) {
59
            return [
60 6
                'method' => $handler['endpoint']->getMethod(),
61 6
                'version' => $handler['endpoint']->getVersion(),
62 6
                'package' => $handler['endpoint']->getPackage(),
63 6
                'api_action' => $handler['endpoint']->getApiAction(),
64 6
                'authorization' => get_class($handler['authorization']),
65 6
                'url' => $this->apiLink->link($handler['endpoint']),
66 6
                'params' => $this->createParamsList($handler['handler']),
67 6
            ];
68 6
        }, $versionHandlers);
69
    }
70
71
    /**
72
     * Create array with params for specified handler
73
     *
74
     * @param ApiHandlerInterface $handler
75
     *
76
     * @return array
77
     */
78
    private function createParamsList(ApiHandlerInterface $handler)
79
    {
80 6
        return array_map(function (InputParam $param) {
81
            $parameter = [
82 3
                'type' => $param->getType(),
83 3
                'key' => $param->getKey(),
84 3
                'is_required' => $param->isRequired(),
85 3
            ];
86 3
            if ($param->getAvailableValues()) {
87 3
                $parameter['available_values'] = $param->getAvailableValues();
88 3
            }
89 3
            return $parameter;
90 6
        }, $handler->params());
91
    }
92
}
93