Passed
Push — dev5a ( 386e30...9eb7a1 )
by Ron
07:53
created

SetEquipmentDataFields::editExistingField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Domains\Equipment;
4
5
use App\CustomerSystemData;
6
use App\SystemDataFields;
7
use App\SystemDataFieldTypes;
8
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Log;
11
12
13
class SetEquipmentDataFields extends GetEquipDataFields
14
{
15
    protected $sysID, $fields;
16
17 4
    public function createEquipmentFields($fieldArr, $sysID)
18
    {
19 4
        $this->sysID = $sysID;
20 4
        $this->fields = collect($fieldArr);
21
22 4
        $this->findNewFields();
23 4
        $this->processFields();
24
25 4
        return true;
26
    }
27
28 8
    public function updateEquipFields($fieldArr, $sysID)
29
    {
30 8
        $this->sysID = $sysID;
31 8
        $this->fields = collect($fieldArr);
32
33 8
        $removedList = $this->findRemovedFields();
34
        //  Loop will check to see if any customers have a value filled for this field type.
35 8
        foreach($removedList as $rem)
36
        {
37 6
            $inUse = $this->isFieldInUse($rem->field_id);
38 6
            if(!$inUse->isEmpty())
39
            {
40
                //  Will fail on first error
41 4
                return $inUse->toArray();
42
            }
43
        }
44
        //  Remove the fields if no errors
45 4
        $removeSuccess = $this->removeFields($removedList);
46 4
        $this->findNewFields();
47 4
        $this->processFields();
48
49 4
        return $removeSuccess;
50
    }
51
52
    //  Update the equipemnt fields
53 8
    protected function processFields()
54
    {
55 8
        foreach($this->fields as $index => $field)
56
        {
57 8
            SystemDataFields::firstOrCreate(
58
                [
59 8
                    'sys_id'       => $this->sysID,
60 8
                    'data_type_id' => $field['data_type_id'],
61
                ],
62
                [
63 8
                    'sys_id'       => $this->sysID,
64 8
                    'data_type_id' => $field['data_type_id'],
65 8
                    'order'        => $index,
66
                ]
67 8
            )->update([
68 8
                'sys_id'       => $this->sysID,
69 8
                'data_type_id' => $field['data_type_id'],
70 8
                'order'        => $index,
71
            ]);
72
        }
73 8
    }
74
75
    //  Find any fields that have been removed from the list
76 8
    protected function findRemovedFields()
77
    {
78 8
        $curFields = $this->getFieldsForEquip($this->sysID);    //SystemDataFields::where('sys_id', $this->sysID)->get();
79
80 8
        $removedList = [];
81 8
        foreach($curFields as $field)
82
        {
83 6
            if(!$this->fields->contains('field_id', $field->field_id))
84
            {
85 6
                $removedList[] = $field;
86
            }
87
        }
88
89 8
        return $removedList;
90
    }
91
92
    //  Determine if any customers have the field populated
93 6
    protected function isFieldInUse($fieldID)
94
    {
95 6
        $data = CustomerSystemData::where('field_id', $fieldID)->get();
96
97 6
        return $data;
98
    }
99
100
    //  Remove the fields from the equipment
101 4
    protected function removeFields($remArr)
102
    {
103 4
        foreach($remArr as $rem)
104
        {
105 2
            SystemDataFields::find($rem->field_id)->delete();
106
        }
107
108 4
        return true;
109
    }
110
111
    //  Find any new fields that have been added to the array
112 8
    protected function findNewFields()
113
    {
114 8
        foreach($this->fields as $key => $field)
115
        {
116 8
            if(!is_array($field))
117
            {
118 8
                $dataID = $this->processNewField($field);
119 8
                $this->fields[$key] = [
120 8
                    'data_type_id' => $dataID,
121 8
                    'sys_id' => $this->sysID,
122 8
                    'data_field_name' => $field,
123
                ];
124
            }
125
        }
126 8
    }
127
128
    //  Determine if a field is actually new, or already exists in the database
129 10
    public function processNewField($field, $hidden = false)
130
    {
131 10
        $fieldData = SystemDataFieldTypes::where('name', $field)->first();
132
133 10
        if(!$fieldData)
134
        {
135 10
            $dataID = SystemDataFieldTypes::create(['name' => $field, 'hidden' => $hidden])->data_type_id;
136
        }
137
        else
138
        {
139 6
            $dataID = $fieldData->data_type_id;
140
        }
141
142 10
        return $dataID;
143
    }
144
145
    //  Update an existing field
146 4
    public function editExistingField($fieldID, $name, $hidden)
147
    {
148 4
        Log::emergency('data type id - '.$fieldID);
149 4
        SystemDataFieldTypes::find($fieldID)->update(['name' => $name, 'hidden' => $hidden]);
150 4
        return true;
151
    }
152
153
    //  Delete a field
154 6
    public function deleteField($fieldID)
155
    {
156
        try
157
        {
158 6
            SystemDataFieldTypes::find($fieldID)->delete();
159
        }
160 2
        catch(\Illuminate\Database\QueryException $e)
161
        {
162 2
            Log::emergency('Tried to delete a field that is still in use.  Field ID - '.$fieldID);
163 2
            return false;
164
        }
165
166 4
        return true;
167
    }
168
}
169