forked from oroinc/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteHandler.php
87 lines (78 loc) · 2.37 KB
/
DeleteHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Oro\Bundle\SoapBundle\Handler;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityNotFoundException;
use Oro\Bundle\OrganizationBundle\Ownership\OwnerDeletionManager;
use Oro\Bundle\SecurityBundle\Exception\ForbiddenException;
use Oro\Bundle\SoapBundle\Entity\Manager\ApiEntityManager;
/**
* A class encapsulates a business logic responsible to delete entity
*/
class DeleteHandler implements DeleteHandlerInterface
{
/**
* @var OwnerDeletionManager
*/
protected $ownerDeletionManager;
/**
* Sets an owner deletion manager
*
* @param OwnerDeletionManager $ownerDeletionManager
*/
public function setOwnerDeletionManager(OwnerDeletionManager $ownerDeletionManager)
{
$this->ownerDeletionManager = $ownerDeletionManager;
}
/**
* Handle delete entity object.
*
* @param mixed $id
* @param ApiEntityManager $manager
* @throws EntityNotFoundException if an entity with the given id does not exist
* @throws ForbiddenException if a delete operation is forbidden
*/
public function handleDelete($id, ApiEntityManager $manager)
{
$entity = $manager->find($id);
if (!$entity) {
throw new EntityNotFoundException();
}
$em = $manager->getObjectManager();
$this->processDelete($entity, $em);
}
/**
* Deletes given entity object.
*
* @param object $entity
* @param ObjectManager $em
*/
public function processDelete($entity, ObjectManager $em)
{
$this->checkPermissions($entity, $em);
$this->deleteEntity($entity, $em);
$em->flush();
}
/**
* Checks if a delete operation is allowed
*
* @param object $entity
* @param ObjectManager $em
* @throws ForbiddenException if a delete operation is forbidden
*/
protected function checkPermissions($entity, ObjectManager $em)
{
if ($this->ownerDeletionManager->isOwner($entity) && $this->ownerDeletionManager->hasAssignments($entity)) {
throw new ForbiddenException('has assignments');
}
}
/**
* Deletes the given entity
*
* @param object $entity
* @param ObjectManager $em
*/
protected function deleteEntity($entity, ObjectManager $em)
{
$em->remove($entity);
}
}