Completed
Pull Request — master (#61)
by Michal
04:05
created

BaseHandler::handle()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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