Passed
Push — dev5a ( 8099d5...bf034e )
by Ron
07:35
created

SetCustomerEquipment::updateExistingEquipment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
3
namespace App\Domains\Customers;
4
5
use App\CustomerSystemData;
6
use App\CustomerSystems;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Log;
9
10
11
class SetCustomerEquipment extends GetCustomerDetails
12
{
13 6
    public function createNewEquipment($request, $custID)
14
    {
15 6
        if($request->shared)
16
        {
17 6
            $parent = $this->getParentID($custID);
18 6
            if($parent)
19
            {
20 2
                $custID = $parent;
21
            }
22
        }
23
24 6
        $equipID = $this->createEquipment($request->sys_id, $custID, $request->shared);
25 6
        $this->processFields($request->fields, $equipID);
26
27 6
        return true;
28
    }
29
30 6
    public function updateExistingEquipment($request, $custID, $equipID)
31
    {
32 6
        if($request->shared)
33
        {
34 6
            $parent = $this->getParentID($custID);
35 6
            if($parent)
36
            {
37 2
                $custID = $parent;
38
            }
39
        }
40
41 6
        $this->updateEquipment($equipID, $custID, $request->shared);
42 6
        $this->processFields($request->fields, $equipID);
43
44 6
        return true;
45
    }
46
47 4
    public function deleteEquip($equipID)
48
    {
49 4
        CustomerSystems::find($equipID)->delete();
50 4
        return true;
51
    }
52
53 6
    protected function createEquipment($sysID, $custID, $shared)
54
    {
55 6
        $newSys = CustomerSystems::create([
56 6
            'cust_id' => $custID,
57 6
            'sys_id'  => $sysID,
58 6
            'shared'  => $shared,
59
        ]);
60
61 6
        return $newSys->cust_sys_id;
62
    }
63
64 6
    protected function updateEquipment($equipID, $custID, $shared)
65
    {
66 6
        CustomerSystems::find($equipID)->update([
67 6
            'cust_id' => $custID,
68 6
            'shared'  => $shared,
69
        ]);
70 6
    }
71
72 12
    protected function processFields($fieldData, $equipID)
73
    {
74 12
        foreach($fieldData as $field)
75
        {
76 12
            Log::emergency($field);
77 12
            CustomerSystemData::firstOrCreate([
78 12
                'cust_sys_id' => $equipID,
79 12
                'field_id'    => $field['field_id'],
80
            ],
81 12
            [   'cust_sys_id' => $equipID,
82 12
                'field_id'    => $field['field_id'],
83 12
                'value' => isset($field['value']) ? $field['value'] : null,])->update([
84 12
                'value' => isset($field['value']) ? $field['value'] : null,
85
            ]);
86
        }
87
88 12
        return true;
89
    }
90
}
91