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