Completed
Push — settings-management ( 58b7ea )
by Fèvre
06:34 queued 06:34
created

SettingRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 10 5
A create() 0 9 5
1
<?php
2
namespace Xetaravel\Models\Repositories;
3
4
use Illuminate\Support\Str;
5
use Xetaravel\Models\Setting;
6
7
class SettingRepository
8
{
9
    /**
10
     * Create a new setting instance.
11
     *
12
     * @param array $data The data used to create the setting.
13
     *
14
     * @return \Xetaravel\Models\Setting
15
     */
16
    public static function create(array $data): Setting
17
    {
18
        return Setting::create([
19
            'name' => Str::slug($data['name'], '.'),
20
            'value_int' => $data['type'] ==  'value_int' ? (int) $data['value'] : null,
21
            'value_str' => $data['type'] ==  'value_str' ? (string) $data['value'] : null,
22
            'value_bool' => $data['type'] ==  'value_bool' ? (bool) $data['value'] : null,
23
            'description' => $data['description'],
24
            'is_deletable' => isset($data['is_deletable']) ? true : false,
25
        ]);
26
    }
27
28
    /**
29
     * Update the Setting informations after a valid update request.
30
     *
31
     * @param array $data The data used to update the setting.
32
     * @param \Xetaravel\Models\Setting $setting The setting to update.
33
     *
34
     * @return bool
35
     */
36
    public static function update(array $data, Setting $setting): bool
37
    {
38
        $setting->name = Str::slug($data['name'], '.');
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Xetaravel\Models\Setting. Since you implemented __set, consider adding a @property annotation.
Loading history...
39
        $setting->value_int = $data['type'] ==  'value_int' ? (int) $data['value'] : null;
0 ignored issues
show
Bug introduced by
The property value_int does not seem to exist on Xetaravel\Models\Setting. 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
        $setting->value_str = $data['type'] ==  'value_str' ? (string) $data['value'] : null;
0 ignored issues
show
Bug introduced by
The property value_str does not seem to exist on Xetaravel\Models\Setting. 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...
41
        $setting->value_bool = $data['type'] ==  'value_bool' ? (bool) $data['value'] : null;
0 ignored issues
show
Bug introduced by
The property value_bool does not seem to exist on Xetaravel\Models\Setting. 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...
42
        $setting->description = $data['description'];
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist on Xetaravel\Models\Setting. Since you implemented __set, consider adding a @property annotation.
Loading history...
43
        $setting->is_deletable = isset($data['is_deletable']) ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
The property is_deletable does not exist on Xetaravel\Models\Setting. Since you implemented __set, consider adding a @property annotation.
Loading history...
44
45
        return $setting->save();
46
    }
47
}
48