DoctrineORMStorage::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/*
3
 * This file is part of the Adlogix package.
4
 *
5
 * (c) Allan Segebarth <[email protected]>
6
 * (c) Jean-Jacques Courtens <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Adlogix\Zf2Rollout\Storage\Doctrine;
13
14
use Adlogix\Zf2Rollout\Entity\FeatureInterface;
15
use Doctrine\ORM\EntityRepository;
16
use Opensoft\Rollout\Storage\StorageInterface;
17
18
/**
19
 * @author Richard Fullmer <[email protected]>
20
 */
21
class DoctrineORMStorage extends EntityRepository implements StorageInterface
22
{
23
    /**
24
     * @param  string $key
25
     *
26
     * @return mixed|null Null if the value is not found
27
     */
28
    public function get($key)
29
    {
30
        /** @var FeatureInterface $feature */
31
        $feature = $this->findOneBy(array('name' => $key));
32
        if (!$feature) {
33
            return null;
34
        }
35
36
        return $feature->getSettings();
37
    }
38
39
    /**
40
     * @param string $key
41
     * @param mixed  $value
42
     */
43
    public function set($key, $value)
44
    {
45
        /** @var FeatureInterface $feature */
46
        $feature = $this->findOneBy(array('name' => $key));
47
        if (!$feature) {
48
            $className = $this->getClassName();
49
            $feature = new $className();
50
        }
51
        $feature->setName($key);
52
        $feature->setSettings($value);
53
        $this->_em->persist($feature);
54
        $this->_em->flush($feature);
55
    }
56
57
    /**
58
     * @param string $key
59
     */
60
    public function remove($key)
61
    {
62
        $feature = $this->findOneBy(array('name' => $key));
63
        if ($feature) {
64
            $this->_em->remove($feature);
65
            $this->_em->flush($feature);
66
        }
67
    }
68
}