Completed
Pull Request — master (#25)
by Michal
15:14 queued 12:43
created

BaseHandler::getEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 45
    public function __construct()
28
    {
29 45
        $this->fractal = new Manager();
30 45
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function description()
36
    {
37
        return '';
38
    }
39
    
40
    /**
41
     * {@inheritdoc}
42
     */
43 9
    public function params()
44
    {
45 9
        return [];
46
    }
47
    
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function tags()
52
    {
53
        return [];
54
    }
55
56
    protected function getFractal()
57
    {
58
        if (!$this->fractal) {
59
            throw new InvalidStateException("Fractal manager isnt initialized. Did you call parent::__construct() in your handler constructor?");
60
        }
61
        return $this->fractal;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 24
    public function setEndpointIdentifier(EndpointInterface $endpoint)
68
    {
69 24
        $this->endpoint = $endpoint;
70 24
    }
71
72
    /**
73
     * @return EndpointInterface
74
     */
75 9
    public function getEndpoint()
76
    {
77 9
        return $this->endpoint;
78
    }
79
80
    /**
81
     * Set link generator to handler
82
     *
83
     * @param LinkGenerator $linkGenerator
84
     *
85
     * @return $this
86
     */
87 6
    public function setupLinkGenerator(LinkGenerator $linkGenerator)
88
    {
89 6
        $this->linkGenerator = $linkGenerator;
90 6
        return $this;
91
    }
92
93
    /**
94
     * Create link to actual handler endpoint
95
     *
96
     * @param array   $params
97
     *
98
     * @return string
99
     * @throws \Nette\Application\UI\InvalidLinkException it handler doesn't have linkgenerator or endpoint
100
     */
101 9
    public function createLink($params)
102
    {
103 9
        if (!$this->linkGenerator) {
104 3
            throw new InvalidStateException("You have setupLinkGenerator for this handler if you want to generate link in this handler");
105
        }
106 6
        if (!$this->endpoint) {
107 3
            throw new InvalidStateException("You have setEndpoint() for this handler if you want to generate link in this handler");
108
        }
109 3
        $params = array_merge([
110 3
            'version' => $this->endpoint->getVersion(),
111 3
            'package' => $this->endpoint->getPackage(),
112 3
            'apiAction' => $this->endpoint->getApiAction()
113 3
        ], $params);
114 3
        return $this->linkGenerator->link('Api:Api:default', $params);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    abstract public function handle($params);
121
}
122