FeatureManager::findParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
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 5
    public function __construct(EntityManager $em, Cache $cache)
25
    {
26 5
        $this->em = $em;
27 5
        $this->cache = $cache;
28 5
    }
29
30
    /**
31
     * @param string $name   Feature name
32
     * @param string $parent Parent name
33
     *
34
     * @return Feature
35
     */
36 2
    public function find($name, $parent)
37
    {
38 2
        $key = $this->generateCacheKey($parent, $name);
39
40 2
        if ($this->cache->contains($key)) {
41 1
            return $this->cache->fetch($key);
42
        }
43
44 2
        $result = $this->em
45 2
            ->createQuery('SELECT f,p FROM AeFeatureBundle:Feature f JOIN f.parent p WHERE f.name = :name AND p.name = :parent')
46 2
            ->setParameters([
47 2
                'name' => $name,
48 2
                'parent' => $parent,
49
            ])
50 2
            ->getSingleResult();
51
52 2
        $this->cache->save($key, $result, 3600 * 24);
53
54 2
        return $result;
55
    }
56
57
    /**
58
     * @param string $name Feature name
59
     *
60
     * @return Feature
61
     */
62
    public function findParent($name)
63
    {
64
        return $this->em
65
            ->createQuery('SELECT f FROM AeFeatureBundle:Feature f WHERE f.name = :name AND f.parent IS NULL')
66
            ->setParameter('name', $name)
67
            ->getSingleResult();
68
    }
69
70
    /**
71
     * @param string $name   Feature name
72
     * @param string $parent Parent name
73
     *
74
     * @return Feature
75
     */
76
    public function findOrCreate($name, $parent)
77
    {
78
        try {
79
            $feature = $this->find($name, $parent);
80
        } catch (NoResultException $e) {
81
            try {
82
                $parent = $this->findParent($parent);
83
            } catch (NoResultException $e) {
84
                $parent = $this->create($parent);
85
            }
86
            $feature = $this->create($name, $parent);
87
88
            $this->update($feature);
89
        }
90
91
        return $feature;
92
    }
93
94
    /**
95
     * @param string  $name   Feature name
96
     * @param Feature $parent Parent Feature
97
     *
98
     * @return Feature
99
     */
100 5
    public function create($name, Feature $parent = null)
101
    {
102 5
        $feature = new Feature();
103 5
        $feature->setName($name);
104 5
        if ($parent) {
105 5
            $feature->setParent($parent);
106
        }
107
108 5
        return $feature;
109
    }
110
111
    /**
112
     * @param bool $andFlush
113
     */
114 2
    public function update(Feature $feature, $andFlush = true)
115
    {
116 2
        $this->em->persist($feature);
117
118 2
        if ($andFlush) {
119 1
            $this->em->flush();
120 1
            $this->emptyCache($feature->getName(), $feature->getParent()->getName());
121
        }
122 2
    }
123
124
    /**
125
     * @todo Move cache logic to a separate class
126
     *
127
     * @param string $name
128
     * @param string $parent
129
     */
130 1
    public function emptyCache($name, $parent)
131
    {
132 1
        $this->cache->delete($this->generateCacheKey($parent, $name));
133 1
    }
134
135
    /**
136
     * @param string $parentName
137
     * @param string $name
138
     *
139
     * @return string
140
     */
141 3
    private function generateCacheKey($parentName, $name)
142
    {
143 3
        return strtolower(sprintf('feature_%s_%s', $parentName, $name));
144
    }
145
}
146