Completed
Push — master ( 7c6110...b863b8 )
by Tomas
23s queued 11s
created

ApiListingControl::getTemplateFilePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
    private $templateFilePath;
23
24
    public function __construct(ApiDecider $apiDecider)
25
    {
26
        $this->apiDecider = $apiDecider;
27
    }
28
29
    public function render(): void
30
    {
31
        $apis = $this->apiDecider->getApis();
32
33
        /** @var Template $template */
34
        $template = $this->getTemplate();
35
        $template->add('apis', $this->groupApis($apis));
36
        $template->setFile($this->getTemplateFilePath());
37
        $template->render();
38
    }
39
40
    public function handleSelect(string $method, int $version, string $package, ?string $apiAction = null): void
41
    {
42
        $this->onClick($method, $version, $package, $apiAction);
43
    }
44
45
    /**
46
     * @param Api[] $handlers
47
     * @return array
48
     */
49
    private function groupApis(array $handlers): array
50
    {
51
        $versionHandlers = [];
52
        foreach ($handlers as $handler) {
53
            $endPoint = $handler->getEndpoint();
54
            if (!isset($versionHandlers[$endPoint->getVersion()])) {
55
                $versionHandlers[$endPoint->getVersion()] = [];
56
            }
57
            $versionHandlers[$endPoint->getVersion()][] = $handler;
58
        }
59
        return $versionHandlers;
60
    }
61
62
    public function setTemplateFilePath(string $templateFilePath): void
63
    {
64
        $this->templateFilePath = $templateFilePath;
65
    }
66
67
    private function getTemplateFilePath(): string
68
    {
69
        return $this->templateFilePath ?: __DIR__ . '/api_listing.latte';
70
    }
71
}
72