Passed
Push — master ( 2a90e7...a622eb )
by Ron
02:27 queued 14s
created

GetCustomerContacts   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 54.76%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 76
ccs 23
cts 42
cp 0.5476
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 getPhoneNumberTypes() 0 10 2
A getOneContact() 0 17 2
A execute() 0 11 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 4
    public function __construct($custID = null)
18
    {
19 4
        $this->custID = $custID;
20 4
    }
21
22 4
    public function execute()
23
    {
24 4
        $hasParent = Customers::findOrFail($this->custID)->parent_id;
25 4
        $localContacts = $this->getLocalContacts();
26
27 4
        if($hasParent)
28
        {
29 2
            $parentContacts = $this->getParentContacts($hasParent);
30 2
            return $localContacts->merge($parentContacts);
31
        }
32 2
        return $localContacts;
33
    }
34
35
    //  Pull a specific contact
36
    public function getOneContact($contID)
37
    {
38
        $contact        = CustomerContacts::where('cont_id', $contID)->with('CustomerContactPhones')->first();
39
        $contactName    = explode(' ', $contact->name);
40
        $contactDetails = collect((object) [
41
            'firstName'   => $contactName[0],
42
            'lastName'    => isset($contactName[1]) ? $contactName[1] : '',
43
            'email'       => $contact->eamil,
44
            'additional'  => '',
45
            'prefix'      => '',
46
            'suffix'      => '',
47
            'cust_id'     => $contact->cust_id,
48
            'numbers'     => $contact->CustomerContactPhones,
49
        ]);
50
51
        // return $contact;
52
        return $contactDetails;
53
    }
54
55
    //  Get the type of phone numbers that can be assigned to a customer
56
    public function getPhoneNumberTypes($collection = false)
57
    {
58
        $numberTypes = PhoneNumberTypes::all();
59
        Log::debug('Phone number type list gathered.  Gathered data - ', array($numberTypes));
60
        if($collection)
61
        {
62
            return new PhoneNumberTypesCollection($numberTypes);
63
        }
64
65
        return $numberTypes;
66
    }
67
68
    //  Retrieve any contacts attached to the customer
69 4
    protected function getLocalContacts()
70
    {
71 4
        $contacts = CustomerContacts::where('cust_id', $this->custID)
72 4
                        ->with('CustomerContactPhones')
73 4
                        ->get();
74
75 4
        Log::debug('Customer Contacts Query completed for customer ID '.$this->custID.'.  Results - ', array($contacts));
76 4
        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