1 | <?php |
||
12 | class FeatureManager |
||
13 | { |
||
14 | /** |
||
15 | * @var EntityManager |
||
16 | */ |
||
17 | protected $em; |
||
18 | |||
19 | 2 | public static function generateCacheKey($parentName, $name) |
|
20 | { |
||
21 | 2 | return strtolower(sprintf('feature_%s_%s', $parentName, $name)); |
|
22 | } |
||
23 | |||
24 | /** |
||
25 | * @param EntityManager $em |
||
26 | */ |
||
27 | 4 | public function __construct(EntityManager $em) |
|
28 | { |
||
29 | 4 | $this->em = $em; |
|
30 | 4 | } |
|
31 | |||
32 | /** |
||
33 | * @param string $name Feature name |
||
34 | * @param string $parent Parent name |
||
35 | * |
||
36 | * @return Feature |
||
37 | */ |
||
38 | public function find($name, $parent) |
||
49 | |||
50 | /** |
||
51 | * @param string $name Feature name |
||
52 | * |
||
53 | * @return Feature |
||
54 | */ |
||
55 | public function findParent($name) |
||
56 | { |
||
57 | return $this->em |
||
58 | ->createQuery('SELECT f FROM AeFeatureBundle:Feature f WHERE f.name = :name AND f.parent IS NULL') |
||
59 | ->setParameter('name', $name) |
||
60 | ->getSingleResult(); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param string $name Feature name |
||
65 | * @param string $parent Parent name |
||
66 | * |
||
67 | * @return Feature |
||
68 | */ |
||
69 | public function findOrCreate($name, $parent) |
||
70 | { |
||
71 | try { |
||
72 | $feature = $this->find($name, $parent); |
||
73 | } catch (NoResultException $e) { |
||
74 | try { |
||
75 | $parent = $this->findParent($parent); |
||
76 | } catch (NoResultException $e) { |
||
77 | $parent = $this->create($parent); |
||
78 | } |
||
79 | $feature = $this->create($name, $parent); |
||
80 | |||
81 | $this->update($feature); |
||
82 | } |
||
83 | |||
84 | return $feature; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param string $name Feature name |
||
89 | * @param Feature $parent Parent Feature |
||
90 | * |
||
91 | * @return Feature |
||
92 | */ |
||
93 | 1 | public function create($name, Feature $parent = null) |
|
103 | |||
104 | /** |
||
105 | * @param Feature $feature |
||
106 | * @param bool $andFlush |
||
107 | */ |
||
108 | 2 | public function update(Feature $feature, $andFlush = true) |
|
116 | |||
117 | /** |
||
118 | * @todo Move cache logic to a separate class |
||
119 | * |
||
120 | * @param string $name |
||
121 | * @param string $parent |
||
122 | */ |
||
123 | 2 | public function emptyCache($name, $parent) |
|
133 | } |
||
134 |