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

BaseHandler::setupLinkGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tomaj\NetteApi\Handlers;
4
5
use League\Fractal\Manager;
6
use League\Fractal\ScopeFactoryInterface;
7
use Nette\Application\LinkGenerator;
8
use Nette\Application\UI\InvalidLinkException;
9
use Nette\InvalidStateException;
10
use Tomaj\NetteApi\EndpointInterface;
11
use Tomaj\NetteApi\Response\ResponseInterface;
12
13
abstract class BaseHandler implements ApiHandlerInterface
14
{
15
    /**
16
     * @var Manager|null
17
     */
18
    private $fractal;
19
20
    /**
21
     * @var EndpointInterface|null
22
     */
23
    private $endpoint;
24
25
    /**
26
     * @var LinkGenerator|null
27
     */
28
    protected $linkGenerator;
29
30 63
    public function __construct(ScopeFactoryInterface $scopeFactory = null)
31
    {
32 63
        $this->fractal = new Manager($scopeFactory);
33 63
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function summary(): string
39
    {
40 3
        return '';
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    public function description(): string
47
    {
48 3
        return '';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 9
    public function params(): array
55
    {
56 9
        return [];
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 3
    public function tags(): array
63
    {
64 3
        return [];
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function deprecated(): bool
71
    {
72 3
        return false;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 3
    public function outputs(): array
79
    {
80 3
        return [];
81
    }
82
83 3
    protected function getFractal(): Manager
84
    {
85 3
        if (!$this->fractal) {
86 3
            throw new InvalidStateException("Fractal manager isn't initialized. Did you call parent::__construct() in your handler constructor?");
87
        }
88
        return $this->fractal;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 33
    final public function setEndpointIdentifier(EndpointInterface $endpoint): void
95
    {
96 33
        $this->endpoint = $endpoint;
97 33
    }
98
99 12
    final public function getEndpoint(): ?EndpointInterface
100
    {
101 12
        return $this->endpoint;
102
    }
103
104
    /**
105
     * Set link generator to handler
106
     *
107
     * @param LinkGenerator $linkGenerator
108
     *
109
     * @return self
110
     */
111 12
    final public function setupLinkGenerator(LinkGenerator $linkGenerator): self
112
    {
113 12
        $this->linkGenerator = $linkGenerator;
114 12
        return $this;
115
    }
116
117
    /**
118
     * Create link to actual handler endpoint
119
     *
120
     * @param array   $params
121
     *
122
     * @return string
123
     * @throws InvalidLinkException if handler doesn't have linkgenerator or endpoint
124
     */
125 18
    final public function createLink(array $params = []): string
126
    {
127 18
        if (!$this->linkGenerator) {
128 6
            throw new InvalidStateException("You have setupLinkGenerator for this handler if you want to generate link in this handler");
129
        }
130 12
        if (!$this->endpoint) {
131 6
            throw new InvalidStateException("You have setEndpoint() for this handler if you want to generate link in this handler");
132
        }
133 6
        $params = array_merge([
134 6
            'version' => $this->endpoint->getVersion(),
135 6
            'package' => $this->endpoint->getPackage(),
136 6
            'apiAction' => $this->endpoint->getApiAction()
137 4
        ], $params);
138 6
        return $this->linkGenerator->link('Api:Api:default', $params);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    abstract public function handle(array $params): ResponseInterface;
145
}
146