Passed
Push — dev5 ( de9a93...e80f59 )
by Ron
06:19
created

CustomerContactsController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Customers;
6
// use App\PhoneNumberType;
7
use App\CustomerContacts;
8
use Illuminate\Http\Request;
9
use App\CustomerContactsView;
10
use App\CustomerContactPhones;
11
use JeroenDesloovere\VCard\VCard;
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Support\Facades\Auth;
14
use App\Http\Controllers\Controller;
15
use App\PhoneNumberTypes;
16
use Illuminate\Support\Facades\Route;
17
18
class CustomerContactsController extends Controller
19
{
20 16
    public function __construct()
21
    {
22 16
        $this->middleware('auth');
23 16
    }
24
25
    //  Store a new customer contact
26 2
    public function store(Request $request)
27
    {
28 2
        $request->validate([
29 2
            'cust_id' => 'required',
30
            'name'    => 'required'
31
        ]);
32
33 2
        $cont = CustomerContacts::create([
34 2
            'cust_id' => $request->cust_id,
35 2
            'name'    => $request->name,
36 2
            'email'   => !empty($request->email) ? $request->email : null,
37 2
        ])->cont_id;
38
39
        // $contID = $cont->cont_id;
40
41 2
        foreach($request->numbers['type'] as $key => $num)
42
        {
43 2
            if(!empty($request->numbers['number'][$key]))
44
            {
45 2
                CustomerContactPhones::create([
46 2
                    'cont_id'       => $cont,
47 2
                    'phone_type_id' => $request->numbers['type'][$key],
48 2
                    'phone_number'  => PhoneNumberTypes::cleanPhoneNumber($request->numbers['number'][$key]),
49 2
                    'extension'     => isset($request->numbers['ext'][$key]) ? $request->numbers['ext'][$key] : null
50
                ]);
51
            }
52
        }
53
54 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
55 2
        Log::debug('Submitted Data - ', $request->toArray());
56 2
        Log::info('New Customer Contact Created for Cust ID - '.$request->cust_id.'.  Contact ID-'.$cont);
57 2
        return response()->json(['success' => true]);
58
    }
59
60
    //  Get the contacts for a customer
61 2
    public function show($id)
62
    {
63 2
        $contacts = CustomerContacts::where('cust_id', $id)
64 2
            ->with('CustomerContactPhones')
65 2
            ->get();
66
67
        // Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
68
        // Log::debug('Fetched Data - ', $contacts->toArray());
69 2
        return $contacts;
70
    }
71
72
    //  Edit function will actually download the contact information in V-Card format
73
    public function edit($id)
74
    {
75
        $contact  = CustomerContacts::find($id);
76
        $numbers  = CustomerContactsView::where('cont_id', $id)->get();
77
        $custData = Customers::find($contact->cust_id);
78
79
        $contactName = explode(' ', $contact->name);
80
        $firstName   = $contactName[0];
81
        $lastName    = isset($contactName[1]) ? $contactName[1] : '';
82
        $additional  = '';
83
        $prefix      = '';
84
        $suffix      = '';
85
86
        $vcard = new VCard();
87
        $vcard->addName($lastName, $firstName, $additional, $prefix, $suffix);
88
        $vcard->addCompany($custData->name);
89
        $vcard->addEmail($contact->email);
90
        $vcard->addAddress(null, null, $custData->address, $custData->city, $custData->state, $custData->zip, null);
91
92
        if(!empty($numbers))
93
        {
94
            foreach($numbers as $phone)
95
            {
96
                $vcard->addPhoneNumber($phone->phone_number, $phone->description);
97
            }
98
        }
99
100
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
101
        Log::info('Customer Contact Downloaded - Contact ID-'.$id);
102
103
        return $vcard->download();
1 ignored issue
show
Bug introduced by
Are you sure the usage of $vcard->download() targeting JeroenDesloovere\VCard\VCard::download() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
104
    }
105
106
    //  Update an existing Customer Contact
107 2
    public function update(Request $request, $id)
108
    {
109 2
        $request->validate([
110 2
            'cust_id' => 'required',
111
            'name'    => 'required'
112
        ]);
113
114
        //  Update the primary contact information
115 2
        CustomerContacts::find($id)->update([
116 2
            'name'    => $request->name,
117 2
            'email'   => isset($request->email) ? $request->email : null,
118
        ]);
119
120 2
        $contID = $id;
121
122
        //  Clear all contact phone numbers and re-enter them
123 2
        CustomerContactPhones::where('cont_id', $id)->delete();
124 2
        foreach($request->numbers['type'] as $key => $num)
125
        {
126 2
            if(!empty($request->numbers['number'][$key]))
127
            {
128 2
                CustomerContactPhones::create([
129 2
                    'cont_id'       => $contID,
130 2
                    'phone_type_id' => $request->numbers['type'][$key],
131 2
                    'phone_number'  => PhoneNumberTypes::cleanPhoneNumber($request->numbers['number'][$key]),
132 2
                    'extension'     => isset($request->numbers['ext'][$key]) ? $request->numbers['ext'][$key] : null
133
                ]);
134
            }
135
        }
136
137 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
138 2
        Log::debug('Submitted Data - ', $request->toArray());
139 2
        Log::info('Customer Contact Updated for Cust ID - '.$request->cust_id.'.  Contact ID-'.$contID);
140 2
        return response()->json(['success' => true]);
141
    }
142
143
    //  Delete an existing contact
144 2
    public function destroy($id)
145
    {
146 2
        $cont = CustomerContacts::find($id);
147
148 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
149 2
        Log::info('Customer Contact deleted for Customer ID-'.$cont->cust_id.' by User ID-'.Auth::user()->user_id.'. Deleted Contact ID-'.$id);
150
151 2
        $cont->delete();
152
153 2
        return response()->json(['success' => true]);
154
    }
155
}
156