Context::removeSetting()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace LCI\Blend\Blendable;
4
use LCI\Blend\Blender;
5
6
/**
7
 * Class Context
8
 * @package LCI\Blend\Blendable
9
 */
10
class Context extends Blendable
11
{
12
    use DescriptionGetterAndSetter;
13
14
    /** @var string  */
15
    protected $opt_cache_key = 'contexts';
16
17
    /** @var string ex: modContext */
18
    protected $xpdo_simple_object_class = 'modContext';
19
20
    /** @var string  */
21
    protected $unique_key_column = 'key';
22
23
    /** @var array ~ this should match data to be inserted via xPDO, ex [column_name => value, ...] */
24
    protected $blendable_xpdo_simple_object_data = [
25
        'description' => '',
26
        'key' => '',
27
        'name' => '',
28
        'rank' => 0,
29
    ];
30
31
    /** @var array  */
32
    protected $portable_settings = [];
33
34
    protected $remove_settings = [];
35
36
    /**
37
     * Resource constructor.
38
     *
39
     * @param \modx $modx
40
     * @param Blender $blender
41
     * @param string $key ~ the context key
42
     */
43
    public function __construct(\modx $modx, Blender $blender, $key = '')
0 ignored issues
show
Bug introduced by
The type modx was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
    {
45
        $this->setFieldKey($key);
46
        parent::__construct($modx, $blender, $key);
47
48
        $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.mediaSources'));
49
        if (count($additional) > 0) {
50
            foreach ($additional as $tv_name) {
51
                $this->portable_settings[$tv_name] = 'media_source';
52
            }
53
        }
54
55
        $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.resources'));
56
        if (count($additional) > 0) {
57
            foreach ($additional as $tv_name) {
58
                $this->portable_settings[$tv_name] = 'resource';
59
            }
60
        }
61
62
        $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.templates'));
63
        if (count($additional) > 0) {
64
            foreach ($additional as $tv_name) {
65
                $this->portable_settings[$tv_name] = 'template';
66
            }
67
        }
68
    }
69
70
    /**
71
     * @param string $type ~ seed or revert
72
     * @return string
73
     */
74
    public function getSeedKey($type = 'seed')
75
    {
76
        $name = $this->getFieldKey();
77
        $key = $this->blender->getSeedKeyFromName($name);
78
79
        switch ($type) {
80
            case 'revert':
81
                $seed_key = 'revert-'.$key;
82
                break;
83
84
            case 'seed':
85
                // no break
86
            default:
87
                $seed_key = $key;
88
        }
89
90
        return $seed_key;
91
    }
92
93
    // Column Getters:
94
    /**
95
     * @return string
96
     */
97
    public function getFieldKey()
98
    {
99
        return $this->blendable_xpdo_simple_object_data['key'];
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getFieldName()
106
    {
107
        return $this->blendable_xpdo_simple_object_data['name'];
108
    }
109
110
    /**
111
     * @return int
112
     */
113
    public function getFieldRank()
114
    {
115
        return $this->blendable_xpdo_simple_object_data['rank'];
116
    }
117
118
    // Column Setters:
119
120
    /**
121
     * @param string $value  max characters: 100
122
     * @return $this
123
     */
124
    public function setFieldKey($value)
125
    {
126
        $this->blendable_xpdo_simple_object_data['key'] = $value;
127
        return $this;
128
    }
129
130
    /**
131
     * @param string $value  max characters: 191
132
     * @return $this
133
     */
134
    public function setFieldName($value)
135
    {
136
        $this->blendable_xpdo_simple_object_data['name'] = $value;
137
        return $this;
138
    }
139
140
    /**
141
     * @param int $value
142
     * @return $this
143
     */
144
    public function setFieldRank($value)
145
    {
146
        $this->blendable_xpdo_simple_object_data['rank'] = $value;
147
        return $this;
148
    }
149
150
    /**
151
     * @param string $key
152
     * @param mixed $value
153
     * @param string $xtype
154
     * @param string $area
155
     * @param string $namespace
156
     * @return $this
157
     */
158
    public function addSetting($key, $value, $xtype = 'textfield', $area = '', $namespace = 'core')
159
    {
160
        $this->related_data['settings'][] = [
161
            'area' => $area,
162
            'namespace' => $namespace,
163
            'key' => $key,
164
            'value' => $value,
165
            'xtype' => $xtype,
166
        ];
167
        return $this;
168
    }
169
170
    /**
171
     * @param string $key
172
     * @return $this
173
     */
174
    public function removeSetting($key)
175
    {
176
        $this->remove_settings[] = $key;
177
        return $this;
178
    }
179
180
    /**
181
     * @return Blendable
182
     */
183
    public function getCurrentVersion()
184
    {
185
        /** @var \LCI\Blend\Blendable\Resource $resource */
186
        $resource = new self($this->modx, $this->blender, $this->getFieldKey());
187
        return $resource
188
            ->setSeedsDir($this->getSeedsDir());
189
    }
190
191
    /**
192
     * Override in child classes
193
     */
194
    protected function loadRelatedData()
195
    {
196
        $settings = [];
197
        $contextSettings = [];
198
        if (is_object($this->xPDOSimpleObject)) {
199
            /** @var array of \modContextSetting $contextSettings */
200
            $contextSettings = $this->xPDOSimpleObject->getMany('ContextSettings');
201
        }
202
        /** @var \modContextSetting $setting */
203
        foreach ($contextSettings as $setting) {
204
            $settings[] = $this->makePortableData($setting->toArray());
205
        }
206
207
        $this->related_data = [
208
            'settings' => $settings
209
        ];
210
211
        // Calls on the event: OnBlendLoadRelatedData
212
        parent::loadRelatedData();
213
    }
214
215
216
    /**
217
     * @param array $setting
218
     * @return bool|string
219
     */
220
    protected function getPortableType($setting)
221
    {
222
        $type = false;
223
        switch ($setting['xtype']) {
224
            case 'modx-combo-template':
225
                $type = 'template';
226
                break;
227
228
            case 'modx-combo-source':
229
                $type = 'media-source';
230
                break;
231
232
            default:
233
                if (isset($this->portable_settings[$setting['key']])) {
234
                    $type = $this->portable_settings[$setting['key']];
235
                }
236
        }
237
        return $type;
238
    }
239
240
    /**
241
     * @param array $setting
242
     * @return array
243
     */
244
    protected function makePortableData($setting)
245
    {
246
        $type = $this->getPortableType($setting);
247
248
        switch ($type) {
249
            case 'media_source':
250
                $mediaSource = $this->modx->getObject('modMediaSource', $setting['value']);
251
                if (is_object($mediaSource)) {
252
                    $setting['portable_type'] = 'media_source';
253
                    $setting['portable_value'] = $mediaSource->get('name');
254
                }
255
                break;
256
257
            case 'resource':
258
                $setting['portable_type'] = 'resource';
259
                $setting['portable_value'] = $this->blender->getResourceSeedKeyFromID($setting['value']);
260
                break;
261
262
            case 'template':
263
                $template = $this->modx->getObject('modTemplate', $setting['value']);
264
                if (is_object($template)) {
265
                    $setting['portable_type'] = 'media_source';
266
                    $setting['portable_value'] = $template->get('templatename');
267
                }
268
                break;
269
        }
270
271
        return $setting;
272
    }
273
274
    /**
275
     * Create convert methods for any portable data column that needs to be converted to an int for a related primary key
276
     */
277
278
    /**
279
     * @param array $setting
280
     * @return string|int|mixed $value
281
     */
282
    protected function convertToLocalData($setting)
283
    {
284
        $value = $setting['value'];
285
        if (is_array($setting) && isset($setting['portable_type']) && isset($setting['portable_value'])) {
286
            switch ($setting['portable_type']) {
287
                case 'media_source':
288
                    $mediaSource = $this->modx->getObject('modMediaSource', ['name' => $setting['portable_value']]);
289
                    if (is_object($mediaSource)) {
290
                        $value = $mediaSource->get('id');
291
                    }
292
                    break;
293
294
                case 'resource':
295
                    $value = $this->blender->getResourceIDFromSeedKey($setting['portable_value']['seed_key'], $setting['portable_value']['context']);
296
                    break;
297
298
                case 'template':
299
                    $template = $this->modx->getObject('modTemplate', ['templatename' => $setting['portable_value']]);
300
                    if (is_object($template)) {
301
                        $value = $template->get('id');
302
                    }
303
                    break;
304
            }
305
        }
306
307
        return $value;
308
    }
309
310
    /**
311
     * This method is called just after a successful blend/save()
312
     */
313
    protected function attachRelatedPiecesAfterSave()
314
    {
315
        if (isset($this->related_data['settings']) && is_array($this->related_data['settings']) && count($this->related_data['settings']) > 0) {
316
            foreach ($this->related_data['settings'] as $setting) {
317
318
                $contextSetting = $this->modx->getObject('modContextSetting', ['context_key' => $this->getFieldKey(), 'key' => $setting['key']]);
319
                if (!is_object($contextSetting)) {
320
                    $contextSetting = $this->modx->newObject('modContextSetting');
321
                }
322
                $contextSetting->fromArray($setting);
323
                $contextSetting->set('context_key', $this->getFieldKey());
324
                $contextSetting->set('key', $setting['key']);
325
                $contextSetting->set('value', $this->convertToLocalData($setting));
326
                $contextSetting->save();
327
            }
328
        }
329
330
        foreach ($this->remove_settings as $setting_key) {
331
332
            $contextSetting = $this->modx->getObject('modContextSetting', ['context_key' => $this->getFieldKey(), 'key' => $setting_key]);
333
            if (is_object($contextSetting)) {
334
                $contextSetting->remove();
335
            }
336
        }
337
    }
338
}
339