Passed
Push — 2.x ( e8dd26...ba8025 )
by Quentin
07:25
created

SettingRepository::getFormFields()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
nc 2
nop 1
dl 0
loc 32
ccs 0
cts 23
cp 0
crap 56
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Repositories;
4
5
use A17\Twill\Models\Setting;
6
use A17\Twill\Repositories\Behaviors\HandleMedias;
7
use Illuminate\Config\Repository as Config;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
11
class SettingRepository extends ModuleRepository
12
{
13
    use HandleMedias;
0 ignored issues
show
introduced by
The trait A17\Twill\Repositories\Behaviors\HandleMedias requires some properties which are not provided by A17\Twill\Repositories\SettingRepository: $medias, $crop_y, $pivot, $metadatas, $mediasParams, $crop_h, $ratio, $crop_x, $crop_w
Loading history...
14
15
    /**
16
     * @var Config
17
     */
18
    protected $config;
19
20
    /**
21
     * @param Setting $model
22
     * @param Config $config
23
     */
24
    public function __construct(Setting $model, Config $config)
25
    {
26
        $this->model = $model;
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * @param string $key
32
     * @param string|null $section
33
     * @return string|null
34
     */
35
    public function byKey($key, $section = null)
36
    {
37
        return $this->model->when($section, function ($query) use ($section) {
38
            $query->where('section', $section);
39
        })->where('key', $key)->exists() ? $this->model->where('key', $key)->with('translations')->first()->value : null;
0 ignored issues
show
Bug introduced by
The property value does not seem to exist on A17\Twill\Models\Model. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
40
    }
41
42
    /**
43
     * @param string|null $section
44
     * @return array
45
     */
46
    public function getFormFields($section = null)
47
    {
48
        $settings = $this->model->when($section, function ($query) use ($section) {
49
            $query->where('section', $section);
50
        })->with('translations', 'medias')->get();
51
52
53
        if (config('twill.media_library.translated_form_fields', false)) {
54
            $medias = $settings->reduce(function ($carry, $setting) {
55
                foreach (getLocales() as $locale) {
56
                    if (!empty(parent::getFormFields($setting)['medias'][$locale]) && !empty(parent::getFormFields($setting)['medias'][$locale][$setting->key]))
57
                    {
58
                        $carry[$locale][$setting->key] = parent::getFormFields($setting)['medias'][$locale][$setting->key];
59
                    }
60
                }
61
                return $carry;
62
            });
63
        } else {
64
            $medias = $settings->mapWithKeys(function ($setting) {
65
                return [$setting->key => parent::getFormFields($setting)['medias'][$setting->key] ?? null];
66
            })->filter()->toArray();
67
        }
68
69
        return $settings->mapWithKeys(function ($setting) {
70
            $settingValue = [];
71
72
            foreach ($setting->translations as $translation) {
73
                $settingValue[$translation->locale] = $translation->value;
74
            }
75
76
            return [$setting->key => count(getLocales()) > 1 ? $settingValue : $setting->value];
77
        })->toArray() + ['medias' => $medias];
78
    }
79
80
    /**
81
     * @param array $settingsFields
82
     * @param string|null $section
83
     * @return void
84
     */
85
    public function saveAll($settingsFields, $section = null)
86
    {
87
        $section = $section ? ['section' => $section] : [];
88
89
        foreach (Collection::make($settingsFields)->except('active_languages', 'medias', 'mediaMeta', 'update') as $key => $value) {
90
            foreach (getLocales() as $locale) {
91
                Arr::set(
92
                    $settingsTranslated,
93
                    $key . '.' . $locale,
94
                    [
95
                        'value' => is_array($value)
96
                        ? (array_key_exists($locale, $value) ? $value[$locale] : $value)
97
                        : $value,
98
                    ] + ['active' => true]
99
                );
100
            }
101
        }
102
103
        if (isset($settingsTranslated) && !empty($settingsTranslated)) {
104
            $settings = [];
105
106
            foreach ($settingsTranslated as $key => $values) {
107
                Arr::set($settings, $key, ['key' => $key] + $section + $values);
108
            }
109
110
            foreach ($settings as $key => $setting) {
111
                $this->model->updateOrCreate(['key' => $key] + $section, $setting);
112
            }
113
        }
114
115
        foreach ($settingsFields['medias'] ?? [] as $role => $mediasList) {
116
            $medias = [];
117
118
            if (config('twill.media_library.translated_form_fields', false)) {
119
                foreach (getLocales() as $locale) {
120
                    $medias["{$role}[{$locale}]"] = Collection::make($settingsFields['medias'][$role][$locale])->map(function ($media) {
121
                        return json_decode($media, true);
122
                    })->filter()->toArray();
123
                }
124
            } else {
125
                $medias =  [
126
                    $role => Collection::make($settingsFields['medias'][$role])->mapWithKeys(function ($media, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

126
                    $role => Collection::make($settingsFields['medias'][$role])->mapWithKeys(function ($media, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
                        return json_decode($media, true);
128
                    })->values()->filter()->toArray(),
129
                ];
130
            }
131
132
133
            $this->updateOrCreate($section + ['key' => $role], $section + [
134
                'key' => $role,
135
                'medias' => $medias,
136
            ]);
137
        }
138
    }
139
140
    /**
141
     * @param string $role
142
     * @return array
143
     */
144
    public function getCrops($role)
145
    {
146
        return $this->config->get('twill.settings.crops')[$role];
147
    }
148
149
}
150