Completed
Push — master ( 6aa8c0...1510da )
by Tomas
10s
created

src/Component/ApiListingControl.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Tomaj\NetteApi\Component;
4
5
use Nette\Application\UI\Control;
6
use Nette\ComponentModel\IContainer;
7
use Tomaj\NetteApi\ApiDecider;
8
use Closure;
9
use Exception;
10
11
class ApiListingControl extends Control
12
{
13
    /**
14
     * @var ApiDecider
15
     */
16
    private $apiDecider;
17
18
    private $clickCallback;
19
20
    public function __construct(IContainer $parent, $name, ApiDecider $apiDecider)
0 ignored issues
show
The parameter $parent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
    {
22
        parent::__construct(null, $name);
23
        $this->apiDecider = $apiDecider;
24
    }
25
26
27
    public function onClick(Closure $callback)
28
    {
29
        $this->clickCallback = $callback;
30
    }
31
32
    public function render()
33
    {
34
        $handlers = $this->apiDecider->getHandlers();
35
        $this->getTemplate()->add('handlers', $this->sortHandlers($handlers));
36
        $this->getTemplate()->setFile(__DIR__ . '/api_listing.latte');
37
        $this->getTemplate()->render();
38
    }
39
40
    public function handleSelect($method, $version, $package, $apiAction)
41
    {
42
        if (!$this->clickCallback) {
43
            throw new Exception('You have to set onClick callback to component!');
44
        }
45
46
        $this->clickCallback->__invoke($method, $version, $package, $apiAction);
47
    }
48
49
    private function sortHandlers($handlers)
50
    {
51
        $versionHandlers = [];
52
        foreach ($handlers as $handler) {
53
            $endPoint = $handler['endpoint'];
54
            if (!isset($versionHandlers[$endPoint->getVersion()])) {
55
                $versionHandlers[$endPoint->getVersion()] = [];
56
            }
57
            $versionHandlers[$endPoint->getVersion()][] = $handler;
58
        }
59
        return $versionHandlers;
60
    }
61
}
62