Completed
Pull Request — master (#61)
by Michal
02:15
created

BaseHandler::outputs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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