Completed
Pull Request — master (#61)
by Tomas
02:27
created

ApiListingControl::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tomaj\NetteApi\Component;
6
7
use Nette\Application\UI\Control;
8
use Tomaj\NetteApi\ApiDecider;
9
use Tomaj\NetteApi\Api;
10
11
class ApiListingControl extends Control
12
{
13
    /** @var ApiDecider */
14
    private $apiDecider;
15
16
    public $onClick = [];
17
18
    public function __construct(ApiDecider $apiDecider)
19
    {
20
        $this->apiDecider = $apiDecider;
21
    }
22
23
    public function render(): void
24
    {
25
        $apis = $this->apiDecider->getApis();
26
        $this->getTemplate()->add('apis', $this->groupApis($apis));
27
        $this->getTemplate()->setFile(__DIR__ . '/api_listing.latte');
28
        $this->getTemplate()->render();
29
    }
30
31
    public function handleSelect(string $method, int $version, string $package, ?string $apiAction = null): void
32
    {
33
        $this->onClick($method, $version, $package, $apiAction);
34
    }
35
36
    /**
37
     * @param Api[] $handlers
38
     * @return array
39
     */
40
    private function groupApis(array $handlers): array
41
    {
42
        $versionHandlers = [];
43
        foreach ($handlers as $handler) {
44
            $endPoint = $handler->getEndpoint();
45
            if (!isset($versionHandlers[$endPoint->getVersion()])) {
46
                $versionHandlers[$endPoint->getVersion()] = [];
47
            }
48
            $versionHandlers[$endPoint->getVersion()][] = $handler;
49
        }
50
        return $versionHandlers;
51
    }
52
}
53