Test Failed
Push — master ( 4c546d...964b0c )
by Maximo
11:32 queued 04:39
created

CustomFields   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 75
ccs 0
cts 53
cp 0
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 44 1
A addValues() 0 16 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\CustomFields;
5
6
use Baka\Database\CustomFields\CustomFields as BakaCustomFields;
7
use Canvas\Models\CustomFieldsValues;
8
9
class CustomFields extends BakaCustomFields
10
{
11
    /**
12
     * Initialize method for model.
13
     */
14
    public function initialize()
15
    {
16
        parent::initialize();
17
18
        $this->belongsTo(
19
            'custom_fields_modules_id',
20
            'Canvas\Models\CustomFieldsModules',
21
            'id',
22
            ['alias' => 'modules']
23
        );
24
25
        $this->hasMany(
26
            'id',
27
            'Canvas\Models\CompanyCustomFields',
28
            'custom_field_id',
29
            ['alias' => 'company-fields']
30
        );
31
32
        $this->belongsTo(
33
            'companies_id',
34
            'Canvas\Models\Apps',
35
            'id',
36
            ['alias' => 'companies']
37
        );
38
39
        $this->hasMany(
40
            'id',
41
            'Canvas\Models\CustomFieldsSettings',
42
            'custom_fields_id',
43
            ['alias' => 'fields-settings']
44
        );
45
46
        $this->hasMany(
47
            'id',
48
            'Canvas\Models\CustomFieldsValues',
49
            'custom_fields_id',
50
            ['alias' => 'fields-values']
51
        );
52
53
        $this->hasMany(
54
            'id',
55
            'Canvas\Models\CustomFieldsValues',
56
            'custom_fields_id',
57
            ['alias' => 'values']
58
        );
59
    }
60
61
    /**
62
     * Given an array of values, add it to this custom fields
63
     * related module.
64
     *
65
     * @param array $values
66
     * @return bool
67
     */
68
    public function addValues(array $values): bool
69
    {
70
        if ($this->values) {
71
            $this->values->delete();
72
        }
73
74
        foreach ($values as $key => $value) {
75
            $customFieldsValue = new CustomFieldsValues();
76
            $customFieldsValue->custom_fields_id = $this->getId();
77
            $customFieldsValue->label = $value;
78
            $customFieldsValue->value = $key;
79
            $customFieldsValue->saveOrFail();
80
        }
81
82
        
83
        return true;
84
    }
85
}
86