1 | <?php |
||
10 | abstract class BaseHandler implements ApiHandlerInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var Manager |
||
14 | */ |
||
15 | private $fractal; |
||
16 | |||
17 | /** |
||
18 | * @var EndpointInterface |
||
19 | */ |
||
20 | private $endpoint; |
||
21 | |||
22 | /** |
||
23 | * @var LinkGenerator |
||
24 | */ |
||
25 | protected $linkGenerator; |
||
26 | |||
27 | 45 | public function __construct() |
|
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | 9 | public function params() |
|
39 | |||
40 | protected function getFractal() |
||
41 | { |
||
42 | if (!$this->fractal) { |
||
43 | throw new InvalidStateException("Fractal manager isnt initialized. Did you call parent::__construct() in your handler constructor?"); |
||
44 | } |
||
45 | return $this->fractal; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 24 | public function setEndpointIdentifier(EndpointInterface $endpoint) |
|
55 | |||
56 | /** |
||
57 | * @return EndpointInterface |
||
58 | */ |
||
59 | 9 | public function getEndpoint() |
|
60 | { |
||
61 | 9 | return $this->endpoint; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Set link generator to handler |
||
66 | * |
||
67 | * @param LinkGenerator $linkGenerator |
||
68 | * |
||
69 | * @return $this |
||
70 | */ |
||
71 | 6 | public function setupLinkGenerator(LinkGenerator $linkGenerator) |
|
72 | { |
||
73 | 6 | $this->linkGenerator = $linkGenerator; |
|
74 | 6 | return $this; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Create link to actual handler endpoint |
||
79 | * |
||
80 | * @param array $params |
||
81 | * |
||
82 | * @return string |
||
83 | * @throws \Nette\Application\UI\InvalidLinkException it handler doesn't have linkgenerator or endpoint |
||
84 | */ |
||
85 | 9 | public function createLink($params) |
|
86 | { |
||
87 | 9 | if (!$this->linkGenerator) { |
|
88 | 3 | throw new InvalidStateException("You have setupLinkGenerator for this handler if you want to generate link in this handler"); |
|
89 | } |
||
90 | 6 | if (!$this->endpoint) { |
|
91 | 3 | throw new InvalidStateException("You have setEndpoint() for this handler if you want to generate link in this handler"); |
|
92 | } |
||
93 | 3 | $params = array_merge([ |
|
94 | 3 | 'version' => $this->endpoint->getVersion(), |
|
95 | 3 | 'package' => $this->endpoint->getPackage(), |
|
96 | 3 | 'apiAction' => $this->endpoint->getApiAction() |
|
97 | 3 | ], $params); |
|
98 | 3 | return $this->linkGenerator->link('Api:Api:default', $params); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | abstract public function handle($params); |
||
105 | } |
||
106 |