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

SetCustomerDetails   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 59
ccs 8
cts 8
cp 1
rs 10
c 2
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createCustomer() 0 10 2
A detachParent() 0 5 1
A linkParent() 0 8 2
A deactivateCustomer() 0 4 1
A attachParent() 0 6 1
A updateCustomer() 0 4 1
A validateParentID() 0 5 2
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