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