Completed
Push — master ( fcda7e...e0250b )
by Tomas
02:45
created

ApiListingHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

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 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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
    public function __construct(ApiDecider $apiDecider, ApiLink $apiLink)
29
    {
30
        parent::__construct();
31
        $this->apiDecider = $apiDecider;
32
        $this->apiLink = $apiLink;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function handle($params)
39
    {
40
        $version = $this->getEndpoint()->getVersion();
41
        $endpoints = $this->getHandlersList($version);
42
        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
    private function getHandlersList($version)
53
    {
54
        $versionHandlers = array_filter($this->apiDecider->getHandlers(), function ($handler) use ($version) {
55
            return $version == $handler['endpoint']->getVersion();
56
        });
57
58
        return array_map(function ($handler) {
59
            return [
60
                'method' => $handler['endpoint']->getMethod(),
61
                'version' => $handler['endpoint']->getVersion(),
62
                'package' => $handler['endpoint']->getPackage(),
63
                'api_action' => $handler['endpoint']->getApiAction(),
64
                'authorization' => get_class($handler['authorization']),
65
                'url' => $this->apiLink->link($handler['endpoint']),
66
                'params' => $this->createParamsList($handler['handler']),
67
            ];
68
        }, $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
        return array_map(function (InputParam $param) {
81
            $parameter = [
82
                'type' => $param->getType(),
83
                'key' => $param->getKey(),
84
                'is_required' => $param->isRequired(),
85
            ];
86
            if ($param->getAvailableValues()) {
87
                $parameter['available_values'] = $param->getAvailableValues();
88
            }
89
            return $parameter;
90
        }, $handler->params());
91
    }
92
}
93