Completed
Pull Request — master (#98)
by Michal
01:35
created

BaseHandler::description()   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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
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
    public function additionalData(): array
86
    {
87 3
        return [];
88 3
    }
89
90
    protected function getFractal(): Manager
91
    {
92
        if (!$this->fractal) {
93
            throw new InvalidStateException("Fractal manager isn't initialized. Did you call parent::__construct() in your handler constructor?");
94
        }
95
        return $this->fractal;
96 33
    }
97
98 33
    /**
99 33
     * {@inheritdoc}
100
     */
101 12
    final public function setEndpointIdentifier(EndpointInterface $endpoint): void
102
    {
103 12
        $this->endpoint = $endpoint;
104
    }
105
106
    final public function getEndpoint(): ?EndpointInterface
107
    {
108
        return $this->endpoint;
109
    }
110
111
    /**
112
     * Set link generator to handler
113 12
     *
114
     * @param LinkGenerator $linkGenerator
115 12
     *
116 12
     * @return self
117
     */
118
    final public function setupLinkGenerator(LinkGenerator $linkGenerator): self
119
    {
120
        $this->linkGenerator = $linkGenerator;
121
        return $this;
122
    }
123
124
    /**
125
     * Create link to actual handler endpoint
126
     *
127 18
     * @param array   $params
128
     *
129 18
     * @return string
130 6
     * @throws InvalidLinkException if handler doesn't have linkgenerator or endpoint
131
     */
132 12
    final public function createLink(array $params = []): string
133 6
    {
134
        if (!$this->linkGenerator) {
135 6
            throw new InvalidStateException("You have setupLinkGenerator for this handler if you want to generate link in this handler");
136 6
        }
137 6
        if (!$this->endpoint) {
138 6
            throw new InvalidStateException("You have setEndpoint() for this handler if you want to generate link in this handler");
139 6
        }
140 6
        $params = array_merge([
141
            'version' => $this->endpoint->getVersion(),
142
            'package' => $this->endpoint->getPackage(),
143
            'apiAction' => $this->endpoint->getApiAction()
144
        ], $params);
145
        return $this->linkGenerator->link('Api:Api:default', $params);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    abstract public function handle(array $params): ResponseInterface;
152
}
153