1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ae\FeatureBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\NoResultException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Carlo Forghieri <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class FeatureManager |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var EntityManager |
15
|
|
|
*/ |
16
|
|
|
protected $em; |
17
|
|
|
|
18
|
1 |
|
public static function generateCacheKey($parentName, $name) |
19
|
|
|
{ |
20
|
1 |
|
return strtolower(sprintf('feature_%s_%s', $parentName, $name)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param EntityManager $em |
25
|
|
|
*/ |
26
|
3 |
|
public function __construct(EntityManager $em) |
27
|
|
|
{ |
28
|
3 |
|
$this->em = $em; |
29
|
3 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $name Feature name |
33
|
|
|
* @param string $parent Parent name |
34
|
|
|
* |
35
|
|
|
* @return Feature |
36
|
|
|
*/ |
37
|
|
|
public function find($name, $parent) |
38
|
|
|
{ |
39
|
|
|
return $this->em |
40
|
|
|
->createQuery('SELECT f,p FROM AeFeatureBundle:Feature f JOIN f.parent p WHERE f.name = :name AND p.name = :parent') |
41
|
|
|
->setParameters([ |
42
|
|
|
'name' => $name, |
43
|
|
|
'parent' => $parent, |
44
|
|
|
]) |
45
|
|
|
->useResultCache(true, 3600 * 24, self::generateCacheKey($parent, $name)) |
46
|
|
|
->getSingleResult(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $name Feature name |
51
|
|
|
* |
52
|
|
|
* @return Feature |
53
|
|
|
*/ |
54
|
|
|
public function findParent($name) |
55
|
|
|
{ |
56
|
|
|
return $this->em |
57
|
|
|
->createQuery('SELECT f FROM AeFeatureBundle:Feature f WHERE f.name = :name AND f.parent IS NULL') |
58
|
|
|
->setParameter('name', $name) |
59
|
|
|
->getSingleResult(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $name Feature name |
64
|
|
|
* @param string $parent Parent name |
65
|
|
|
* |
66
|
|
|
* @return Feature |
67
|
|
|
*/ |
68
|
|
|
public function findOrCreate($name, $parent) |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
|
|
$feature = $this->find($name, $parent); |
72
|
|
|
} catch (NoResultException $e) { |
73
|
|
|
try { |
74
|
|
|
$parent = $this->findParent($parent); |
75
|
|
|
} catch (NoResultException $e) { |
76
|
|
|
$parent = $this->create($parent); |
77
|
|
|
} |
78
|
|
|
$feature = $this->create($name, $parent); |
79
|
|
|
|
80
|
|
|
$this->update($feature); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $feature; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $name Feature name |
88
|
|
|
* @param Feature $parent Parent Feature |
89
|
|
|
* |
90
|
|
|
* @return Feature |
91
|
|
|
*/ |
92
|
1 |
|
public function create($name, Feature $parent = null) |
93
|
|
|
{ |
94
|
1 |
|
$feature = new Feature(); |
95
|
1 |
|
$feature->setName($name); |
96
|
1 |
|
if ($parent) { |
97
|
1 |
|
$feature->setParent($parent); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
1 |
|
return $feature; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Feature $feature |
105
|
|
|
* @param bool $andFlush |
106
|
|
|
*/ |
107
|
1 |
|
public function update(Feature $feature, $andFlush = true) |
108
|
|
|
{ |
109
|
1 |
|
$this->em->persist($feature); |
110
|
1 |
|
if ($andFlush) { |
111
|
1 |
|
$this->em->flush(); |
112
|
1 |
|
$this->emptyCache($feature->getName(), $feature->getParent()->getName()); |
113
|
1 |
|
} |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @todo Move cache logic to a separate class |
118
|
|
|
* |
119
|
|
|
* @param string $name |
120
|
|
|
* @param string $parent |
121
|
|
|
*/ |
122
|
|
|
public function emptyCache($name, $parent) |
123
|
|
|
{ |
124
|
|
|
$cache = $this->em->getConfiguration()->getResultCacheImpl(); |
125
|
|
|
$cache->delete(self::generateCacheKey($parent, $name)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|