1
|
|
|
<?php namespace Pz\Doctrine\Rest; |
2
|
|
|
|
3
|
|
|
use Doctrine\ORM\EntityNotFoundException; |
4
|
|
|
use League\Fractal\TransformerAbstract; |
5
|
|
|
use Pz\Doctrine\Rest\Contracts\JsonApiResource; |
6
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
7
|
|
|
|
8
|
|
|
abstract class RestAction |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var RestRepository |
12
|
|
|
*/ |
13
|
|
|
protected $repository; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var RestResponseFactory |
17
|
|
|
*/ |
18
|
|
|
protected $response; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var TransformerAbstract|\Closure |
22
|
|
|
*/ |
23
|
|
|
protected $transformer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param RestRequestContract $request |
27
|
|
|
* |
28
|
|
|
* @return RestResponse |
29
|
|
|
*/ |
30
|
|
|
abstract protected function handle(RestRequestContract $request); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* RestActionAbstract constructor. |
34
|
|
|
* |
35
|
|
|
* @param RestRepository $repository |
36
|
|
|
* @param TransformerAbstract|\Closure $transformer |
37
|
|
|
*/ |
38
|
15 |
|
public function __construct(RestRepository $repository, $transformer) |
39
|
|
|
{ |
40
|
15 |
|
$this->repository = $repository; |
41
|
15 |
|
$this->transformer = $transformer; |
42
|
15 |
|
$this->response = new RestResponseFactory(); |
43
|
15 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param RestRequestContract $request |
47
|
|
|
* |
48
|
|
|
* @return RestResponse |
49
|
|
|
*/ |
50
|
15 |
|
public function dispatch(RestRequestContract $request) |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
|
|
|
54
|
15 |
|
return $this->handle($request); |
55
|
|
|
|
56
|
2 |
|
} catch (EntityNotFoundException $e) { |
57
|
1 |
|
return RestResponse::notFound($e->getMessage()); |
58
|
1 |
|
} catch (RestException $e) { |
59
|
1 |
|
return RestResponse::exception($e); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return RestRepository |
65
|
|
|
*/ |
66
|
15 |
|
public function repository() |
67
|
|
|
{ |
68
|
15 |
|
return $this->repository; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return RestResponseFactory |
73
|
|
|
*/ |
74
|
13 |
|
public function response() |
75
|
|
|
{ |
76
|
13 |
|
return $this->response; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return TransformerAbstract|\Closure |
81
|
|
|
*/ |
82
|
13 |
|
public function transformer() |
83
|
|
|
{ |
84
|
13 |
|
return $this->transformer; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Authorize rest request. |
89
|
|
|
* Entity will be object for get,update,delete actions. |
90
|
|
|
* Entity will be string for index,create action. |
91
|
|
|
* |
92
|
|
|
* @param RestRequestContract $request |
93
|
|
|
* @param object|string $entity |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
14 |
|
public function authorize(/** @scrutinizer ignore-unused */$request, /** @scrutinizer ignore-unused */$entity) |
98
|
|
|
{ |
99
|
14 |
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|