SettingsManager   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 88.71%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 186
ccs 55
cts 62
cp 0.8871
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A initialize() 0 7 2
A clear() 0 4 1
A add() 0 15 3
A all() 0 12 3
A get() 0 9 2
A set() 0 23 2
A has() 0 7 1
A resolve() 0 4 1
A resolveValue() 0 4 1
A escapeValue() 0 4 1
A unescapeValue() 0 4 1
A remove() 0 4 1
1
<?php
2
3
namespace SumoCoders\FrameworkSettingsBundle;
4
5
use Doctrine\ORM\EntityManager;
6
use SumoCoders\FrameworkSettingsBundle\Entity\Setting;
7
use SumoCoders\FrameworkSettingsBundle\Entity\SettingRepository;
8
use SumoCoders\FrameworkSettingsBundle\Exception\DontUseException;
9
use SumoCoders\FrameworkSettingsBundle\Exception\InvalidInstanceException;
10
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11
12
class SettingsManager implements ParameterBagInterface
13
{
14
    /**
15
     * @var EntityManager
16
     */
17
    protected $entityManager;
18
19
    /**
20
     * @var SettingRepository
21
     */
22
    protected $repository;
23
24
    /**
25
     * Parameter storage.
26
     *
27
     * @var array
28
     */
29
    protected $settings;
30
31
    /**
32
     * Is the class initialized
33
     *
34
     * @var bool
35
     */
36
    protected $isInitialized = false;
37
38
    /**
39
     * @param EntityManager     $entityManager
40
     * @param SettingRepository $repository
41
     */
42 11
    public function __construct($entityManager, $repository)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. 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...
43
    {
44 11
        $this->entityManager = $entityManager;
45 11
        $this->repository = $repository;
46 11
    }
47
48
    /**
49
     * Initialize the settings
50
     */
51 5
    protected function initialize()
52
    {
53 5
        if (!$this->isInitialized) {
54 5
            $this->all();
55 5
        }
56 5
        $this->isInitialized = true;
57 5
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function clear()
63
    {
64 1
        throw new DontUseException();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function add(array $parameters)
71
    {
72 3
        $this->isInitialized = false;
73 3
        foreach ($parameters as $setting) {
74 3
            if (!($setting instanceof Setting)) {
75 1
                throw new InvalidInstanceException('This is not an instance of the Setting-class.');
76
            }
77
78 2
            $this->set(
79 2
                $setting->getName(),
80 2
                $setting->getValue(),
81 2
                $setting->isEditable()
82 2
            );
83 2
        }
84 2
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 5
    public function all()
90
    {
91 5
        $settings = $this->repository->findAll();
92
93 5
        if (!empty($settings)) {
94
            foreach ($settings as $setting) {
95
                $this->settings[$setting->getName()] = $setting;
96
            }
97
        }
98
99 5
        return $this->settings;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 2
    public function get($name, $defaultValue = null)
106
    {
107 2
        $this->initialize();
108 2
        if (!isset($this->settings[$name])) {
109
            return $defaultValue;
110
        }
111
112 2
        return $this->settings[$name]->getValue();
113
    }
114
115
    /**
116
     * Store a setting
117
     *
118
     * @param string $name
119
     * @param mixed  $value
120
     * @param bool   $isEditable
121
     * @return $this
122
     */
123 4
    public function set($name, $value, $isEditable = false)
124
    {
125 4
        $this->isInitialized = false;
126
127 4
        if ($this->has($name)) {
128
            $setting = $this->settings[$name];
129
        } else {
130 4
            $setting = new Setting();
131 4
            $setting->setName($name);
132
        }
133
134 4
        $setting->setValue($value);
135 4
        $setting->setEditable($isEditable);
136
137
        // store and flush
138 4
        $this->entityManager->persist($setting);
139 4
        $this->entityManager->flush();
140
141
        // reset it
142 4
        $this->settings[$name] = $setting;
143
144 4
        return $this;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 5
    public function has($name)
151
    {
152 5
        $this->isInitialized = false;
153 5
        $this->initialize();
154
155 5
        return isset($this->settings[$name]);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1
    public function resolve()
162
    {
163 1
        throw new DontUseException();
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 1
    public function resolveValue($value)
170
    {
171 1
        throw new DontUseException();
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 1
    public function escapeValue($value)
178
    {
179 1
        throw new DontUseException();
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 1
    public function unescapeValue($value)
186
    {
187 1
        throw new DontUseException();
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function remove($name)
194
    {
195
        throw new DontUseException();
196
    }
197
}
198