Completed
Pull Request — master (#168)
by
unknown
14:57
created

SettingsManager::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 18
rs 9.4285
cc 2
eloc 13
nc 2
nop 5
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       $type
67
     * @param string       $description
68
     * @param string|array $value
69
     * @param array        $profiles
70
     *
71
     * @throws \LogicException
72
     */
73
    public function set($name, $type, $description, $value, $profiles)
74
    {
75
        foreach ($profiles as $profile) {
76
            $setting = new Setting();
77
            $setting->setId($profile . '_' . $name);
78
            $setting->setName($name);
79
            $setting->setDescription($description);
80
            $setting->setData((object)['value' => $value]);
81
            $setting->setType($type);
82
            $setting->setProfile($profile);
83
84
            $this->manager->persist($setting);
85
        }
86
        $this->manager->commit();
87
        $this->manager->flush();
88
89
        $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...
90
    }
91
92
    /**
93
     * Saves setting.
94
     *
95
     * @param Setting $setting
96
     */
97
    public function save(Setting $setting)
98
    {
99
        $this->manager->persist($setting);
100
        $this->manager->commit();
101
102
        $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...
103
    }
104
105
    /**
106
     * Removes a setting.
107
     *
108
     * @param Setting $setting
109
     */
110
    public function remove(Setting $setting)
111
    {
112
        $this->repo->remove($setting->getId());
113
        $this->manager->flush();
114
        $this->manager->refresh();
115
116
        $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...
117
    }
118
119
    /**
120
     * Copy a setting to the new profile.
121
     *
122
     * @param Setting $setting
123
     * @param string  $newProfile
124
     */
125
    public function duplicate(Setting $setting, $newProfile)
126
    {
127
        $newSetting = clone $setting;
128
129
        $newSetting->setId($newProfile . '_' . $setting->getName());
130
        $newSetting->setProfile($newProfile);
131
132
        $this->save($newSetting);
133
    }
134
135
    /**
136
     * Returns setting model by name and profile or creates new if $mustExist is set to FALSE.
137
     *
138
     * @param string $name
139
     * @param string $profile
140
     * @param bool   $mustExist
141
     * @param string $type
142
     *
143
     * @throws \UnexpectedValueException
144
     *
145
     * @return Setting
146
     */
147
    public function get($name, $profile = 'default', $mustExist = true, $type = 'string')
148
    {
149
        $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 157 which is incompatible with the return type documented by ONGR\SettingsBundle\Sett...al\SettingsManager::get of type ONGR\SettingsBundle\Document\Setting.
Loading history...
150
        if ($setting === null) {
151
            if ($mustExist === true) {
152
                throw new \UnexpectedValueException();
153
            }
154
            $setting = $this->createSetting($name, $profile, $type);
155
        }
156
157
        return $setting;
158
    }
159
160
    /**
161
     * Creates new setting object.
162
     *
163
     * @param string $name
164
     * @param string $profile
165
     * @param string $type
166
     *
167
     * @return Setting
168
     */
169
    protected function createSetting($name, $profile, $type)
170
    {
171
        $setting = new Setting();
172
        $setting->setId($profile . '_' . $name);
173
        $setting->setName($name);
174
        $setting->setProfile($profile);
175
        $setting->setType($type);
176
177
        if ($type == 'array') {
178
            $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...
179
        }
180
181
        return $setting;
182
    }
183
}
184