1
|
|
|
<?php namespace Pz\Doctrine\Rest\Action; |
2
|
|
|
|
3
|
|
|
use Pz\Doctrine\Rest\Resource\Item; |
4
|
|
|
use Pz\Doctrine\Rest\RestResponseFactory; |
5
|
|
|
use Pz\Doctrine\Rest\Traits\CanHydrate; |
6
|
|
|
use Pz\Doctrine\Rest\RestAction; |
7
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
8
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
9
|
|
|
use Pz\Doctrine\Rest\Traits\CanValidate; |
10
|
|
|
|
11
|
|
|
class UpdateAction extends RestAction |
12
|
|
|
{ |
13
|
|
|
use CanHydrate; |
|
|
|
|
14
|
|
|
use CanValidate; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
protected $beforeUpdate = []; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
protected $afterUpdate = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param RestRequestContract $request |
24
|
|
|
* @return RestResponse |
25
|
|
|
* |
26
|
|
|
* @throws \Doctrine\ORM\ORMException |
27
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
28
|
|
|
* @throws \Pz\Doctrine\Rest\Exceptions\RestException |
29
|
|
|
*/ |
30
|
1 |
|
public function handle($request) |
31
|
|
|
{ |
32
|
1 |
|
$entity = $this->repository()->findById($request->getId()); |
33
|
|
|
|
34
|
1 |
|
$this->authorize($request, $entity); |
35
|
1 |
|
$this->hydrateEntity($entity, $request->getData()); |
36
|
1 |
|
$this->validateEntity($entity); |
37
|
|
|
|
38
|
1 |
|
if ($this->hasEvents()) { |
39
|
1 |
|
$this->repository() |
40
|
1 |
|
->getEntityManager() |
41
|
1 |
|
->getUnitOfWork() |
42
|
1 |
|
->computeChangeSets(); |
43
|
|
|
|
44
|
1 |
|
$changeSet = $this->repository() |
45
|
1 |
|
->getEntityManager() |
46
|
1 |
|
->getUnitOfWork() |
47
|
1 |
|
->getEntityChangeSet($entity); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
if (isset($changeSet)) { |
51
|
1 |
|
$this->callBeforeUpdate($entity, $changeSet); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$this->repository() |
55
|
1 |
|
->getEntityManager() |
56
|
1 |
|
->flush(); |
57
|
|
|
|
58
|
1 |
|
if (isset($changeSet)) { |
59
|
1 |
|
$this->callAfterUpdate($entity, $changeSet); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return RestResponseFactory::resource($request, |
63
|
1 |
|
new Item($entity, $this->transformer()) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param callable $cb |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
1 |
|
public function beforeUpdate(callable $cb) |
72
|
|
|
{ |
73
|
1 |
|
$this->beforeUpdate[] = $cb; |
74
|
1 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param callable $cb |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
1 |
|
public function afterUpdate(callable $cb) |
82
|
|
|
{ |
83
|
1 |
|
$this->afterUpdate[] = $cb; |
84
|
1 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
1 |
|
protected function hasEvents() |
91
|
|
|
{ |
92
|
1 |
|
return !empty($this->beforeUpdate) || !empty($this->afterUpdate); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param object $entity |
97
|
|
|
* @param array $changeSet |
98
|
|
|
*/ |
99
|
1 |
|
protected function callBeforeUpdate($entity, array $changeSet) |
100
|
|
|
{ |
101
|
1 |
|
foreach ($this->beforeUpdate as $beforeUpdate) { |
102
|
1 |
|
$beforeUpdate($entity, $changeSet); |
103
|
|
|
} |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param object $entity |
108
|
|
|
* @param array $changeSet |
109
|
|
|
*/ |
110
|
1 |
|
protected function callAfterUpdate($entity, array $changeSet) |
111
|
|
|
{ |
112
|
1 |
|
foreach ($this->afterUpdate as $afterUpdate) { |
113
|
1 |
|
$afterUpdate($entity, $changeSet); |
114
|
|
|
} |
115
|
1 |
|
} |
116
|
|
|
} |
117
|
|
|
|