Passed
Push — dev5 ( 67c7cf...c7611f )
by Ron
19:22
created

GetCustomerContacts   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 97.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 76
ccs 41
cts 42
cp 0.9762
rs 10
c 1
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getParentContacts() 0 9 1
A getLocalContacts() 0 8 1
A execute() 0 11 2
A getPhoneNumberTypes() 0 10 2
A getOneContact() 0 17 2
1
<?php
2
3
namespace App\Domains\Customers;
4
5
use App\CustomerContacts;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Auth;
8
9
use App\Customers;
10
use App\Http\Resources\PhoneNumberTypesCollection;
11
use App\PhoneNumberTypes;
12
13
class GetCustomerContacts
14
{
15
    protected $custID;
16
17 10
    public function __construct($custID = null)
18
    {
19 10
        $this->custID = $custID;
20 10
    }
21
22 6
    public function execute()
23
    {
24 6
        $hasParent = Customers::findOrFail($this->custID)->parent_id;
25 6
        $localContacts = $this->getLocalContacts();
26
27 6
        if($hasParent)
28
        {
29 2
            $parentContacts = $this->getParentContacts($hasParent);
30 2
            return $localContacts->merge($parentContacts);
31
        }
32 4
        return $localContacts;
33
    }
34
35
    //  Pull a specific contact
36 2
    public function getOneContact($contID)
37
    {
38 2
        $contact        = CustomerContacts::where('cont_id', $contID)->with('CustomerContactPhones')->first();
39 2
        $contactName    = explode(' ', $contact->name);
40 2
        $contactDetails = collect((object) [
41 2
            'firstName'   => $contactName[0],
42 2
            'lastName'    => isset($contactName[1]) ? $contactName[1] : '',
43 2
            'email'       => $contact->eamil,   //  FIXME:  is this broken???
44 2
            'additional'  => '',
45 2
            'prefix'      => '',
46 2
            'suffix'      => '',
47 2
            'cust_id'     => $contact->cust_id,
48 2
            'numbers'     => $contact->CustomerContactPhones,
49
        ]);
50
51
        // return $contact;
52 2
        return $contactDetails;
53
    }
54
55
    //  Get the type of phone numbers that can be assigned to a customer
56 2
    public function getPhoneNumberTypes($collection = false)
57
    {
58 2
        $numberTypes = PhoneNumberTypes::all();
59 2
        Log::debug('Phone number type list gathered.  Gathered data - ', array($numberTypes));
60 2
        if($collection)
61
        {
62
            return new PhoneNumberTypesCollection($numberTypes);
63
        }
64
65 2
        return $numberTypes;
66
    }
67
68
    //  Retrieve any contacts attached to the customer
69 6
    protected function getLocalContacts()
70
    {
71 6
        $contacts = CustomerContacts::where('cust_id', $this->custID)
72 6
                        ->with('CustomerContactPhones')
73 6
                        ->get();
74
75 6
        Log::debug('Customer Contacts Query completed for customer ID '.$this->custID.'.  Results - ', array($contacts));
76 6
        return $contacts;
77
    }
78
79
    //  Retrieve any equipment attached to the parent customer
80 2
    protected function getParentContacts($parentID)
81
    {
82 2
        $parentList = CustomerContacts::where('cust_id', $parentID)
83 2
                        ->where('shared', 1)
84 2
                        ->with('CustomerContactPhones')
85 2
                        ->get();
86
87 2
        Log::debug('Customer Parent Contacts Query completed for Customer ID '.$this->custID.'.  Results - ', array($parentList));
88 2
        return $parentList;
89
    }
90
}
91