Completed
Pull Request — master (#61)
by Michal
02:15
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
declare(strict_types=1);
4
5
namespace Tomaj\NetteApi\Component;
6
7
use Nette\Application\UI\Control;
8
use Nette\Bridges\ApplicationLatte\Template;
9
use Tomaj\NetteApi\ApiDecider;
10
use Tomaj\NetteApi\Api;
11
12
/**
13
 * @method void onClick(string $method, int $version, string $package, ?string $apiAction)
14
 */
15
class ApiListingControl extends Control
16
{
17
    /** @var ApiDecider */
18
    private $apiDecider;
19
20
    public $onClick = [];
21
22
    public function __construct(ApiDecider $apiDecider)
23
    {
24
        $this->apiDecider = $apiDecider;
25
    }
26
27
    public function render(): void
28
    {
29
        $apis = $this->apiDecider->getApis();
30
31
        /** @var Template $template */
32
        $template = $this->getTemplate();
33
        $template->add('apis', $this->groupApis($apis));
34
        $template->setFile(__DIR__ . '/api_listing.latte');
35
        $template->render();
36
    }
37
38
    public function handleSelect(string $method, int $version, string $package, ?string $apiAction = null): void
39
    {
40
        $this->onClick($method, $version, $package, $apiAction);
41
    }
42
43
    /**
44
     * @param Api[] $handlers
45
     * @return array
46
     */
47
    private function groupApis(array $handlers): array
48
    {
49
        $versionHandlers = [];
50
        foreach ($handlers as $handler) {
51
            $endPoint = $handler->getEndpoint();
52
            if (!isset($versionHandlers[$endPoint->getVersion()])) {
53
                $versionHandlers[$endPoint->getVersion()] = [];
54
            }
55
            $versionHandlers[$endPoint->getVersion()][] = $handler;
56
        }
57
        return $versionHandlers;
58
    }
59
}
60