Completed
Pull Request — master (#167)
by
unknown
29:50 queued 26:02
created

SettingsManager::createSetting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
rs 9.4285
cc 2
eloc 9
nc 2
nop 3
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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 ONGR\SettingsBundle\Settings\General;
13
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\Translation\TranslatorInterface;
16
use ONGR\ElasticsearchBundle\Service\Repository;
17
use ONGR\ElasticsearchBundle\Service\Manager;
18
use ONGR\SettingsBundle\Document\Setting;
19
use ONGR\SettingsBundle\Event\SettingChangeEvent;
20
21
/**
22
 * Class SettingsManager responsible for managing settings actions.
23
 */
24
class SettingsManager
25
{
26
    /**
27
     * @var TranslatorInterface
28
     */
29
    protected $translator;
30
31
    /**
32
     * @var EventDispatcherInterface
33
     */
34
    protected $eventDispatcher;
35
36
    /**
37
     * @var Manager
38
     */
39
    protected $manager;
40
41
    /**
42
     * @var Repository
43
     */
44
    protected $repo;
45
46
    /**
47
     * @param TranslatorInterface      $translator
48
     * @param EventDispatcherInterface $eventDispatcher
49
     * @param Manager                  $manager
50
     */
51
    public function __construct(
52
        TranslatorInterface $translator,
53
        EventDispatcherInterface $eventDispatcher,
54
        Manager $manager
55
    ) {
56
        $this->translator = $translator;
57
        $this->eventDispatcher = $eventDispatcher;
58
        $this->manager = $manager;
59
        $this->repo = $this->manager->getRepository('ONGRSettingsBundle:Setting');
60
    }
61
62
    /**
63
     * Overwrites setting with given name.
64
     *
65
     * @param string       $name
66
     * @param string       $description
67
     * @param string       $type
68
     * @param string|array $value
69
     * @param string       $profile
70
     *
71
     * @throws \LogicException
72
     */
73
    public function set($name, $description, $type, $value, $profile = 'default')
74
    {
75
        $setting = new Setting();
76
        $setting->setId($profile . '_' . $name);
77
        $setting->setName($name);
78
        $setting->setDescription($description);
79
        $setting->setData((object)['value' => $value]);
80
        $setting->setType($type);
81
        $setting->setProfile($profile);
82
83
        $this->manager->persist($setting);
84
        $this->manager->commit();
85
        $this->manager->flush();
86
87
        $this->eventDispatcher->dispatch('ongr_settings.setting_change', new SettingChangeEvent('save'));
0 ignored issues
show
Documentation introduced by
'save' is of type string, but the function expects a object<ONGR\SettingsBundle\Event\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
    }
89
90
    /**
91
     * Saves setting.
92
     *
93
     * @param Setting $setting
94
     */
95
    public function save(Setting $setting)
96
    {
97
        $this->manager->persist($setting);
98
        $this->manager->commit();
99
100
        $this->eventDispatcher->dispatch('ongr_settings.setting_change', new SettingChangeEvent('save'));
0 ignored issues
show
Documentation introduced by
'save' is of type string, but the function expects a object<ONGR\SettingsBundle\Event\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
    }
102
103
    /**
104
     * Removes a setting.
105
     *
106
     * @param Setting $setting
107
     */
108
    public function remove(Setting $setting)
109
    {
110
        $this->repo->remove($setting->getId());
111
        $this->manager->flush();
112
        $this->manager->refresh();
113
114
        $this->eventDispatcher->dispatch('ongr_settings.setting_change', new SettingChangeEvent('delete'));
0 ignored issues
show
Documentation introduced by
'delete' is of type string, but the function expects a object<ONGR\SettingsBundle\Event\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
    }
116
117
    /**
118
     * Copy a setting to the new profile.
119
     *
120
     * @param Setting $setting
121
     * @param string  $newProfile
122
     */
123
    public function duplicate(Setting $setting, $newProfile)
124
    {
125
        $newSetting = clone $setting;
126
127
        $newSetting->setId($newProfile . '_' . $setting->getName());
128
        $newSetting->setProfile($newProfile);
129
130
        $this->save($newSetting);
131
    }
132
133
    /**
134
     * Returns setting model by name and profile or creates new if $mustExist is set to FALSE.
135
     *
136
     * @param string $name
137
     * @param string $profile
138
     * @param bool   $mustExist
139
     * @param string $type
140
     *
141
     * @throws \UnexpectedValueException
142
     *
143
     * @return Setting
144
     */
145
    public function get($name, $profile = 'default', $mustExist = true, $type = 'string')
146
    {
147
        $setting = $this->repo->find($profile . '_' . $name);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->repo->find($profile . '_' . $name); of type null|ReflectionClass adds the type ReflectionClass to the return on line 155 which is incompatible with the return type documented by ONGR\SettingsBundle\Sett...al\SettingsManager::get of type ONGR\SettingsBundle\Document\Setting.
Loading history...
148
        if ($setting === null) {
149
            if ($mustExist === true) {
150
                throw new \UnexpectedValueException();
151
            }
152
            $setting = $this->createSetting($name, $profile, $type);
153
        }
154
155
        return $setting;
156
    }
157
158
    /**
159
     * Returns setting model by id
160
     *
161
     * @param string $id
162
     *
163
     * @throws \UnexpectedValueException
164
     *
165
     * @return Setting
166
     */
167
    public function getById($id)
168
    {
169
        $setting = $this->repo->find($id);
170
        if ($setting === null) {
171
            throw new \UnexpectedValueException();
172
        }
173
174
        return $setting;
175
    }
176
177
    /**
178
     * Creates new setting object.
179
     *
180
     * @param string $name
181
     * @param string $profile
182
     * @param string $type
183
     *
184
     * @return Setting
185
     */
186
    protected function createSetting($name, $profile, $type)
187
    {
188
        $setting = new Setting();
189
        $setting->setId($profile . '_' . $name);
190
        $setting->setName($name);
191
        $setting->setProfile($profile);
192
        $setting->setType($type);
193
194
        if ($type == 'array') {
195
            $setting->setData(['value' => null]);
0 ignored issues
show
Documentation introduced by
array('value' => null) is of type array<string,null,{"value":"null"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
196
        }
197
198
        return $setting;
199
    }
200
}
201