Completed
Push — master ( fcda7e...e0250b )
by Tomas
02:45
created

BaseHandler::params()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Tomaj\NetteApi\Handlers;
4
5
use League\Fractal\Manager;
6
use Nette\Application\LinkGenerator;
7
use Nette\InvalidStateException;
8
use Tomaj\NetteApi\EndpointInterface;
9
10
abstract class BaseHandler implements ApiHandlerInterface
11
{
12
    /**
13
     * @var Manager
14
     */
15
    private $fractal;
16
17
    /**
18
     * @var EndpointInterface
19
     */
20
    private $endpoint;
21
22
    /**
23
     * @var  LinkGenerator
24
     */
25
    protected $linkGenerator;
26
27 30
    public function __construct()
28
    {
29 30
        $this->fractal = new Manager();
30 30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function params()
36
    {
37
        return [];
38
    }
39
40
    protected function getFractal()
41
    {
42
        if (!$this->fractal) {
43
            throw new InvalidStateException("Fractal manager isnt initialized. Did you call parent::__construct() in your handler constructor?");
44
        }
45
        return $this->fractal;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 15
    public function setEndpointIdentifier(EndpointInterface $endpoint)
52
    {
53 15
        $this->endpoint = $endpoint;
54 15
    }
55
56
    /**
57
     * @return EndpointInterface
58
     */
59 3
    public function getEndpoint()
60
    {
61 3
        return $this->endpoint;
62
    }
63
64
    /**
65
     * Set link generator to handler
66
     *
67
     * @param LinkGenerator $linkGenerator
68
     *
69
     * @return $this
70
     */
71 6
    public function setupLinkGenerator(LinkGenerator $linkGenerator)
72
    {
73 6
        $this->linkGenerator = $linkGenerator;
74 6
        return $this;
75
    }
76
77
    /**
78
     * Create link to actual handler endpoint
79
     *
80
     * @param array   $params
81
     *
82
     * @return string
83
     * @throws \Nette\Application\UI\InvalidLinkException it handler doesn't have linkgenerator or endpoint
84
     */
85 9
    public function createLink($params)
86
    {
87 9
        if (!$this->linkGenerator) {
88 3
            throw new InvalidStateException("You have setupLinkGenerator for this handler if you want to generate link in this handler");
89
        }
90 6
        if (!$this->endpoint) {
91 3
            throw new InvalidStateException("You have setEndpoint() for this handler if you want to generate link in this handler");
92
        }
93 3
        $params = array_merge([
94 3
            'version' => $this->endpoint->getVersion(),
95 3
            'package' => $this->endpoint->getPackage(),
96 3
            'apiAction' => $this->endpoint->getApiAction()
97 3
        ], $params);
98 3
        return $this->linkGenerator->link('Api:Api:default', $params);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    abstract public function handle($params);
105
}
106