Code

< 40 %
40-60 %
> 60 %
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)
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