Completed
Push — master ( 914e3f...2b201f )
by Timur
02:21
created

CustomizationsManager::saveSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Giftd\Editor;
4
5
class CustomizationsManager
6
{
7
    /**
8
     * @var Customization
9
     */
10
    protected $customization;
11
12
    /**
13
     * CustomizationsManager constructor.
14
     *
15
     * @param Customization   $customization
16
     * @param SettingsManager $settings
17
     * @param array           $previewData   = []
18
     */
19
    public function __construct(Customization $customization, SettingsManager $settings, array $previewData = [])
20
    {
21
        $customization->setSettings($settings)->setPreviewData($previewData);
22
23
        $this->customization = $customization;
24
    }
25
26
    /**
27
     * Parses Customization's editables and generates final editables array.
28
     *
29
     * @return array
30
     */
31
    public function parseEditables()
32
    {
33
        $editables = $this->customization->editables();
34
35
        if (!is_array($editables)) {
36
            return [];
37
        }
38
39
        return $this->groupEditableSettings($editables);
40
    }
41
42
    /**
43
     * Builds customization data.
44
     *
45
     * @return Customization
46
     */
47
    public function buildData()
48
    {
49
        $this->customization->build();
50
51
        return $this->customization;
52
    }
53
54
    /**
55
     * Saves customization settings.
56
     *
57
     * @return $this
58
     */
59
    public function saveSettings()
60
    {
61
        $this->customization->save();
62
63
        return $this;
64
    }
65
66
    /**
67
     * Performs editable settings groupping.
68
     *
69
     * @param array $editables
70
     *
71
     * @return array
72
     */
73
    protected function groupEditableSettings(array $editables)
74
    {
75
        foreach ($editables as &$group) {
76
            if (!count($group['settings'])) {
77
                continue;
78
            }
79
80
            $settings = [];
81
82
            foreach ($group['settings'] as $setting) {
83
                /**
84
                 * @var Editable $setting
85
                 */
86
                $attributes = $setting->attributes();
87
88
                if (empty($attributes['name'])) {
89
                    continue;
90
                }
91
92
                $settings[$attributes['name']] = $attributes;
93
                unset($settings[$attributes['name']]['name']);
94
            }
95
96
            $group['settings'] = $settings;
97
        }
98
99
        return $editables;
100
    }
101
}
102