1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Opensoft\RolloutBundle\Storage\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Opensoft\Rollout\Storage\StorageInterface; |
7
|
|
|
use Opensoft\RolloutBundle\Entity\Feature; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Richard Fullmer <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class DoctrineORMStorage implements StorageInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Doctrine\ORM\EntityManagerInterface |
16
|
|
|
*/ |
17
|
|
|
protected $em; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Doctrine\ORM\EntityRepository |
21
|
|
|
*/ |
22
|
|
|
protected $repository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $class; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param EntityManagerInterface $em |
31
|
|
|
* @param string $class |
32
|
|
|
*/ |
33
|
|
|
public function __construct(EntityManagerInterface $em, $class) |
34
|
|
|
{ |
35
|
|
|
$this->em = $em; |
36
|
|
|
$this->repository = $em->getRepository($class); |
37
|
|
|
$this->class = $class; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string key |
42
|
|
|
* |
43
|
|
|
* @return mixed|null Null if the value is not found |
44
|
|
|
*/ |
45
|
|
|
public function get($key) |
46
|
|
|
{ |
47
|
|
|
/** @var Feature $feature */ |
48
|
|
|
$feature = $this->repository->findOneBy(array('name' => $key)); |
49
|
|
|
if (!$feature) { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $feature->getSettings(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $key |
58
|
|
|
* @param mixed $value |
59
|
|
|
*/ |
60
|
|
|
public function set($key, $value) |
61
|
|
|
{ |
62
|
|
|
/** @var Feature $feature */ |
63
|
|
|
$feature = $this->repository->findOneBy(array('name' => $key)); |
64
|
|
|
if (!$feature) { |
65
|
|
|
$feature = new Feature(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$feature->setName($key); |
69
|
|
|
$feature->setSettings($value); |
70
|
|
|
|
71
|
|
|
$this->em->persist($feature); |
72
|
|
|
$this->em->flush($feature); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $key |
77
|
|
|
*/ |
78
|
|
|
public function remove($key) |
79
|
|
|
{ |
80
|
|
|
$feature = $this->repository->findOneBy(array('name' => $key)); |
81
|
|
|
if ($feature) { |
82
|
|
|
$this->em->remove($feature); |
83
|
|
|
$this->em->flush($feature); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|