Passed
Push — dev5a ( 328ab1...cf3a23 )
by Ron
07:38
created

CustomerNotesController::download()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Domains\Customers\GetCustomerNotes;
6
use App\Domains\Customers\SetCustomerNotes;
7
use App\Domains\Customers\GetCustomerDetails;
8
9
use App\Http\Controllers\Controller;
10
11
use App\Http\Requests\Customers\CustomerNoteRequest;
12
13
use Illuminate\Http\Request;
14
use Illuminate\Support\Facades\Log;
15
use Illuminate\Support\Facades\Auth;
16
17
use PDF;
18
19
class CustomerNotesController extends Controller
20
{
21
    //  Create a new customer note
22 2
    public function store(CustomerNoteRequest $request)
23
    {
24 2
        (new SetCustomerNotes)->createNewNote($request, $request->cust_id, Auth::user()->user_id);
25 2
        Log::info('A new note has been created for Customer ID '.$request->cust_id.' by '.Auth::user()->full_name.'.  Data - ', $request->toArray());
26 2
        return response()->json(['success' => true]);
27
    }
28
29
    //  Get all notes for a customer
30 2
    public function show($id)
31
    {
32 2
        return (new GetCustomerNotes)->execute($id);
33
    }
34
35
    //  Download a note as a PDF file
36
    public function download($id)
37
    {
38
        $noteData = (new GetCustomerNotes)->getOneNote($id);
39
        $custData = (new GetCustomerDetails)->getDetails($noteData->cust_id);
40
41
        $pdf = PDF::loadView('pdf.customerNote', [
42
            'cust_name'   => $custData->name,
43
            'subject'     => $noteData->subject,
44
            'description' => $noteData->description,
45
        ]);
46
47
        return $pdf->download($custData->name.' - Note: '.$noteData->subject.'.pdf');
48
    }
49
50
    //  Update an existing note
51 2
    public function update(CustomerNoteRequest $request, $id)
52
    {
53 2
        (new SetCustomerNotes)->updateNote($request, $request->cust_id, $id);
54 2
        Log::info('Customer Note ID '.$id.' updated by '.Auth::user()->full_name);
55 2
        return response()->json(['success' => true]);
56
    }
57
58
    //  Delete an existing note
59 2
    public function destroy($id)
60
    {
61 2
        (new SetCustomerNotes)->deleteNote($id);
62 2
        Log::info('Customer Note ID '.$id.' deleted by '.Auth::user()->full_name);
63 2
        return response()->json(['success' => true]);
64
    }
65
}
66