Completed
Pull Request — master (#61)
by Michal
03:22
created

ApiListingHandler::getApiList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

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