1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\MenuBundle\Menu; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\MenuBundle\Entity\Item; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Alexis BUSSIERES <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Menu |
12
|
|
|
{ |
13
|
|
|
const DELETE_STRATEGY_MOVE_CHILDREN = 'delete.strategy.move_children'; |
14
|
|
|
|
15
|
|
|
protected $entityManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Menu constructor. |
19
|
|
|
* @param EntityManager $entityManager |
20
|
|
|
*/ |
21
|
|
|
public function __construct(EntityManager $entityManager) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$this->entityManager = $entityManager; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array $item An array of object Item |
28
|
|
|
* @param string $strategy Strategy to use |
29
|
|
|
*/ |
30
|
|
|
public function deleteItem($item, $strategy) |
31
|
|
|
{ |
32
|
|
|
$this->deleteItems([$item], $strategy); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This method delete an array of Item by different strategy. |
37
|
|
|
* |
38
|
|
|
* Strategies available: |
39
|
|
|
* self::DELETE_STRATEGY_MOVE_CHILDREN Remove Item and set children to the same level of the deleted Item |
40
|
|
|
* |
41
|
|
|
* @param $items |
42
|
|
|
* @param string $strategy Strategy to use |
43
|
|
|
*/ |
44
|
|
|
public function deleteItems($items, $strategy = self::DELETE_STRATEGY_MOVE_CHILDREN) |
45
|
|
|
{ |
46
|
|
|
if (!is_array($items)) { |
47
|
|
|
throw new \InvalidArgumentException('The "$items" parameters is not an array.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (empty($items)) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
switch ($strategy) { |
55
|
|
|
case self::DELETE_STRATEGY_MOVE_CHILDREN: |
56
|
|
|
$this->deleteItemsMoveChildren($items); |
57
|
|
|
break; |
58
|
|
|
default: |
59
|
|
|
throw new \InvalidArgumentException('The "$stategy" parameter must be a non empty string.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->entityManager->flush(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* This method delete Item object and manage his children by self::DELETE_STRATEGY_MOVE_CHILDREN strategy |
67
|
|
|
* |
68
|
|
|
* @param array $items An array of object Item |
69
|
|
|
*/ |
70
|
|
|
private function deleteItemsMoveChildren($items) |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
foreach ($items as $item) { |
74
|
|
|
if (!is_object($item) || !$item instanceof Item) { |
75
|
|
|
throw new \InvalidArgumentException(sprintf( |
76
|
|
|
'An error occurred during the operation, |
77
|
|
|
the value must be an instance object of %s' |
78
|
|
|
), Item::class); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->entityManager->remove($item); |
82
|
|
|
|
83
|
|
|
$children = $item->getChildren(); |
84
|
|
|
$itemParent = $item->getParent(); |
85
|
|
|
|
86
|
|
|
if (empty($children) || empty($itemParent)) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$itemParentChildren = $itemParent->getChildren(); |
91
|
|
|
foreach ($children as $child) { |
92
|
|
|
$child->setParent($itemParent); |
93
|
|
|
$itemParentChildren->add($child); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->entityManager->persist($itemParent); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array An array of available delete strategies |
102
|
|
|
*/ |
103
|
|
|
public static function getDeleteStrategiesAvailable() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
self::DELETE_STRATEGY_MOVE_CHILDREN, |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.