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

ApiListingControl::groupApis()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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