1
|
|
|
<?php namespace Pz\LaravelDoctrine\Rest; |
2
|
|
|
|
3
|
|
|
use Doctrine\ORM\EntityManager; |
4
|
|
|
use Illuminate\Http\Response; |
5
|
|
|
use Illuminate\Routing\Controller; |
6
|
|
|
use League\Fractal\TransformerAbstract; |
7
|
|
|
|
8
|
|
|
use pmill\Doctrine\Hydrator\ArrayHydrator; |
9
|
|
|
use Pz\Doctrine\Rest\Action\DeleteAction; |
10
|
|
|
use Pz\Doctrine\Rest\Action\IndexAction; |
11
|
|
|
use Pz\Doctrine\Rest\Action\ShowAction; |
12
|
|
|
use Pz\Doctrine\Rest\Action\CreateAction; |
13
|
|
|
use Pz\Doctrine\Rest\Action\UpdateAction; |
14
|
|
|
|
15
|
|
|
use Pz\Doctrine\Rest\RestResponseFactory; |
16
|
|
|
use Pz\LaravelDoctrine\Rest\Request\CreateRestRequest; |
17
|
|
|
use Pz\LaravelDoctrine\Rest\Request\UpdateRestRequest; |
18
|
|
|
|
19
|
|
|
abstract class RestController extends Controller |
20
|
|
|
{ |
21
|
|
|
use IndexAction; |
22
|
|
|
use ShowAction; |
23
|
|
|
use DeleteAction; |
24
|
|
|
use CreateAction; |
25
|
|
|
use UpdateAction; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EntityManager |
29
|
|
|
*/ |
30
|
|
|
protected $em; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RestResponse|RestResponseFactory |
34
|
|
|
*/ |
35
|
|
|
protected $response; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return TransformerAbstract |
39
|
|
|
*/ |
40
|
|
|
abstract public function transformer(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* UserController constructor. |
44
|
|
|
* |
45
|
|
|
* @param EntityManager $em |
46
|
|
|
* @param RestResponse $response |
47
|
|
|
*/ |
48
|
5 |
|
public function __construct(EntityManager $em, RestResponse $response) |
49
|
|
|
{ |
50
|
5 |
|
$this->em = $em; |
51
|
5 |
|
$this->response = $response; |
52
|
|
|
|
53
|
5 |
|
$this->middleware(function($request, \Closure $next) { |
54
|
|
|
try { |
55
|
|
|
/** @var Response $response */ |
56
|
5 |
|
$response = $next($request); |
57
|
|
|
|
58
|
5 |
|
if (isset($response->exception) && $response->exception instanceof \Exception) { |
59
|
5 |
|
return $this->response()->exception($response->exception); |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
} catch (\Exception $e) { |
63
|
5 |
|
return $this->response()->exception($e); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
return $response; |
67
|
5 |
|
}); |
68
|
5 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param CreateRestRequest $request |
72
|
|
|
* |
73
|
|
|
* @return object |
74
|
|
|
* @throws \Exception |
75
|
|
|
*/ |
76
|
1 |
|
public function createEntity($request) |
77
|
|
|
{ |
78
|
1 |
|
return $this->hydrator()->hydrate( |
79
|
1 |
|
$this->repository()->getClassName(), |
80
|
1 |
|
$request->validated() |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param UpdateRestRequest $request |
86
|
|
|
* @param $entity |
87
|
|
|
* |
88
|
|
|
* @return object |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
1 |
|
public function updateEntity($request, $entity) |
92
|
|
|
{ |
93
|
1 |
|
return $this->hydrator()->hydrate( |
|
|
|
|
94
|
1 |
|
$entity, |
95
|
1 |
|
$request->validated() |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return RestResponse |
101
|
|
|
*/ |
102
|
5 |
|
public function response() |
103
|
|
|
{ |
104
|
5 |
|
return $this->response->transformer($this->transformer()); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return ArrayHydrator |
109
|
|
|
*/ |
110
|
2 |
|
public function hydrator() |
111
|
|
|
{ |
112
|
2 |
|
return new ArrayHydrator($this->em); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.