Completed
Push — master ( eb239b...de9420 )
by Timur
02:11
created

CustomizationsManager::customization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
     * Get original customization.
56
     *
57
     * @return Customization
58
     */
59
    public function customization()
60
    {
61
        return $this->customization;
62
    }
63
64
    /**
65
     * Saves customization settings.
66
     *
67
     * @return $this
68
     */
69
    public function saveSettings()
70
    {
71
        $this->customization->save();
72
73
        return $this;
74
    }
75
76
    /**
77
     * Performs editable settings groupping.
78
     *
79
     * @param array $editables
80
     *
81
     * @return array
82
     */
83
    protected function groupEditableSettings(array $editables)
84
    {
85
        foreach ($editables as &$group) {
86
            if (!count($group['settings'])) {
87
                continue;
88
            }
89
90
            $settings = [];
91
92
            foreach ($group['settings'] as $setting) {
93
                /**
94
                 * @var Editable $setting
95
                 */
96
                $attributes = $setting->attributes();
97
98
                if (empty($attributes['name'])) {
99
                    continue;
100
                }
101
102
                $settings[$attributes['name']] = $attributes;
103
                unset($settings[$attributes['name']]['name']);
104
            }
105
106
            $group['settings'] = $settings;
107
        }
108
109
        return $editables;
110
    }
111
}
112