Passed
Push — dev5a ( a81ef3...276986 )
by Ron
07:39
created

GetCustomerContacts   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 50
ccs 25
cts 25
cp 1
rs 10
c 2
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllContacts() 0 9 1
A execute() 0 11 2
A getOneContact() 0 24 4
1
<?php
2
3
namespace App\Domains\Customers;
4
5
use App\CustomerContacts;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Log;
8
use JeroenDesloovere\VCard\VCard;
9
10
class GetCustomerContacts extends GetCustomerDetails
11
{
12 4
    public function execute($custID)
13
    {
14 4
        $contacts = $this->getAllContacts($custID);
15
16
        //  Get any contacts that are shared between sites
17 4
        if($parent = $this->getParentID($custID))
18
        {
19 2
            $contacts = $contacts->merge($this->getAllContacts($parent, true));
20
        }
21
22 4
        return $contacts;
23
    }
24
25 2
    public function getOneContact($contID)
26
    {
27 2
        $cont = CustomerContacts::where('cont_id', $contID)->with('CustomerContactPhones')->first();
28 2
        $cust = $this->getDetails($cont->cust_id);
29 2
        $name = explode(' ', $cont->name);
30
31
        //  Build the VCard
32 2
        $vcard = new VCard();
33 2
        $vcard->addName(isset($name[1]) ? $name[1] : null, $name[0]);
34 2
        $vcard->addCompany($cust->name);
35 2
        $vcard->addAddress(null, null, $cust->address, $cust->city, $cust->state, $cust->zip, null);
36
37
        //  Add phone numbers to the VCard
38 2
        foreach($cont->CustomerContactPhones as $phone)
39
        {
40 2
            $number = $phone->readable;
41 2
            if($phone->extension)
42
            {
43 2
                $number .= ', '.$phone->extension;
44
            }
45 2
            $vcard->addPhoneNumber($number, $phone->type_name);
46
        }
47
48 2
        return $vcard;
49
    }
50
51 4
    protected function getAllContacts($custID, $shared = false)
52
    {
53 4
        return CustomerContacts::where('cust_id', $custID)
54
            ->when($shared, function($q)
55
            {
56 2
                $q->where('shared', 1);
57 4
            })
58 4
            ->with('CustomerContactPhones')
59 4
            ->get();
60
    }
61
}
62