Test Failed
Push — dev5a ( 7155d2...8099d5 )
by Ron
07:36
created

SetCustomerDetails::detachParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Domains\Customers;
4
5
use App\Customers;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Log;
8
9
10
class SetCustomerDetails
11
{
12 6
    public function createCustomer($request)
13
    {
14 6
        if($request->parent_id != null)
15
        {
16 2
            $request->parent_id = $this->validateParentID($request->parent_id);
17
        }
18
19 6
        $newCust = Customers::create($request->toArray());
20
21 6
        return $newCust->cust_id;
22
    }
23
24
    public function updateCustomer($custID, $details)
25 2
    {
26
        Customers::find($custID)->update($details->toArray());
27 2
        return true;
28
    }
29 2
30
    public function deactivateCustomer($custID)
31
    {
32
        Customers::find($custID)->delete();
33
        return true;
34
    }
35
36
    public function linkParent($request)
37
    {
38
        if(isset($request->parent_id))
39
        {
40
            return $this->attachParent($request->cust_id, $request->parent_id);
41
        }
42
43
        return $this->detachParent($request->cust_id);
44
    }
45
46
    //  Determine if the assigned parent has a partent of its own
47
    protected function validateParentID($parentID)
48
    {
49
        $parent = Customers::find($parentID);
50
51
        return $parent->parent_id ? $parent->parent_id : $parentID;
52
    }
53
54
    //  Link the parent to the customer
55
    protected function attachParent($custID, $parentID)
56
    {
57
        $parent = $this->validateParentID($parentID);
58
        Customers::find($custID)->update(['parent_id' => $parent]);
59
60
        return true;
61
    }
62
63
    //  Remove the parent from the customer
64
    protected function detachParent($custID)
65
    {
66
        Customers::find($custID)->update(['parent_id' => null]);
67
68
        return true;
69
    }
70
}
71