|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Ticket; |
|
6
|
|
|
use AppBundle\Exception\Ticket\PlaceArrangementException; |
|
7
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
8
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
9
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
10
|
|
|
|
|
11
|
|
|
class EntityDeleteListener |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var TokenStorageInterface |
|
15
|
|
|
*/ |
|
16
|
49 |
|
private $tokenStorage; |
|
17
|
|
|
|
|
18
|
49 |
|
public function __construct(TokenStorageInterface $tokenStorage) |
|
19
|
49 |
|
{ |
|
20
|
|
|
$this->tokenStorage = $tokenStorage; |
|
21
|
7 |
|
} |
|
22
|
|
|
|
|
23
|
7 |
|
public function preSoftDelete(LifecycleEventArgs $args) |
|
24
|
7 |
|
{ |
|
25
|
7 |
|
$token = $this->tokenStorage->getToken(); |
|
26
|
7 |
|
|
|
27
|
|
|
if (null == $token) { |
|
28
|
7 |
|
throw new AccessDeniedException('Only authorized users can delete entities'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$object = $args->getEntity(); |
|
32
|
7 |
|
|
|
33
|
|
|
$this->checkObject($object); |
|
34
|
|
|
|
|
35
|
|
|
if (!method_exists($object, 'setDeletedBy')) { |
|
36
|
7 |
|
return; |
|
37
|
7 |
|
} |
|
38
|
7 |
|
|
|
39
|
7 |
|
$om = $args->getEntityManager(); |
|
40
|
|
|
$uow = $om->getUnitOfWork(); |
|
41
|
7 |
|
$meta = $om->getClassMetadata(get_class($object)); |
|
42
|
7 |
|
$reflProp = $meta->getReflectionProperty('deletedBy'); |
|
43
|
7 |
|
$oldValue = $reflProp->getValue($object); |
|
44
|
7 |
|
$reflProp->setValue($object, $token->getUser()->getUsername()); |
|
45
|
|
|
|
|
46
|
7 |
|
$om->persist($object); |
|
47
|
|
|
$uow->propertyChanged($object, 'deletedBy', $oldValue, $token->getUser()->getUsername()); |
|
48
|
|
|
$uow->scheduleExtraUpdate($object, array( |
|
49
|
|
|
'deletedBy' => array($oldValue, $token->getUser()->getUsername()), |
|
50
|
|
|
)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function checkObject($object) |
|
54
|
|
|
{ |
|
55
|
|
|
switch (true) { |
|
56
|
|
|
case $object instanceof Ticket: |
|
57
|
|
|
if (!$object->isRemovable()) { |
|
58
|
|
|
throw new PlaceArrangementException( |
|
59
|
|
|
sprintf( |
|
60
|
|
|
'Impossible to remove ticket: %s. It has status: %s.', |
|
61
|
|
|
$object->getId(), |
|
62
|
|
|
$object->getStatus() |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
break; |
|
67
|
|
|
default: |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|