Completed
Push — master ( 736f3d...81c44f )
by Toni
03:39
created

DoctrineORMStorage::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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\Feature;
15
use Doctrine\ORM\EntityManager;
16
use Opensoft\Rollout\Storage\StorageInterface;
17
/**
18
 * @author Richard Fullmer <[email protected]>
19
 */
20
class DoctrineORMStorage implements StorageInterface
21
{
22
    /**
23
     * @var \Doctrine\ORM\EntityManager
24
     */
25
    protected $em;
26
    /**
27
     * @var \Doctrine\ORM\EntityRepository
28
     */
29
    protected $repository;
30
    /**
31
     * @var string
32
     */
33
    protected $class;
34
    /**
35
     * @param EntityManager $em
36
     * @param string        $class
37
     */
38
    public function __construct(EntityManager $em, $class)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
39
    {
40
        $this->em = $em;
41
        $this->repository = $em->getRepository($class);
42
        $this->class = $class;
43
    }
44
    /**
45
     * @param  string     $key
46
     * @return mixed|null Null if the value is not found
47
     */
48
    public function get($key)
49
    {
50
        /** @var Feature $feature */
51
        $feature = $this->repository->findOneBy(array('name' => $key));
52
        if (!$feature) {
53
            return null;
54
        }
55
        return $feature->getSettings();
56
    }
57
    /**
58
     * @param string $key
59
     * @param mixed  $value
60
     */
61
    public function set($key, $value)
62
    {
63
        /** @var Feature $feature */
64
        $feature = $this->repository->findOneBy(array('name' => $key));
65
        if (!$feature) {
66
            $feature = new Feature();
67
        }
68
        $feature->setName($key);
69
        $feature->setSettings($value);
70
        $this->em->persist($feature);
71
        $this->em->flush($feature);
72
    }
73
    /**
74
     * @param string $key
75
     */
76
    public function remove($key)
77
    {
78
        $feature = $this->repository->findOneBy(array('name' => $key));
79
        if ($feature) {
80
            $this->em->remove($feature);
81
            $this->em->flush($feature);
82
        }
83
    }
84
}