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

ApiListingControl   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 42
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A render() 0 7 1
A handleSelect() 0 4 1
A groupApis() 0 12 3
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