Passed
Push — dev5 ( 03a57e...c2dc0b )
by Ron
09:22
created

SetCustomerFiles   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 75
ccs 33
cts 39
cp 0.8462
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createFile() 0 23 3
A checkParent() 0 6 2
A deleteCustFile() 0 10 1
A updateFile() 0 16 2
A __construct() 0 4 1
1
<?php
2
3
namespace App\Domains\Customers;
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\Auth;
7
8
use App\Domains\FilesDomain;
9
10
use App\Customers;
11
use App\CustomerFiles;
12
13
use App\Http\Requests\CustomerFileNewRequest;
14
use App\Http\Requests\CustomerFileUpdateRequest;
15
16
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
17
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
18
19
class SetCustomerFiles extends FilesDomain
20
{
21
    protected $custID;
22
23 10
    public function __construct($custID)
24
    {
25 10
        $this->custID = $custID;
26 10
        $this->path = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$custID;
27 10
    }
28
29
    //  Create a new note for the customer
30 4
    public function createFile(CustomerFileNewRequest $request)
31
    {
32 4
        $this->receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
33
34 4
        $save = $this->receiver->receive();
35 4
        if($save->isFinished())
36
        {
37 4
            $fileID = $this->saveFile($save->getFile());
38
39
            //  Input the file into the customer files table
40 4
            CustomerFiles::create([
41 4
                'file_id'      => $fileID,
42 4
                'file_type_id' => $request->file_type_id,
43 4
                'cust_id'      => $request->cust_id,
44 4
                'user_id'      => Auth::user()->user_id,
45 4
                'shared'       => $request->shared ? 1 : 0,
46 4
                'name'         => $request->name
47
            ]);
48
49 4
            return true;
50
        }
51
52
        return false;
53
    }
54
55
    //  Update an existing file for the customer
56 4
    public function updateFile(CustomerFileUpdateRequest $request, $fileID)
57
    {
58 4
        if($request->shared)
59
        {
60
            $this->checkParent();
61
        }
62
63 4
        CustomerFiles::find($fileID)->update([
64 4
            'name'         => $request->name,
65 4
            'file_type_id' => $request->customer_file_types['file_type_id'],
66 4
            'cust_id'      => $request->cust_id,
67 4
            'shared'       => $request->shared,
68
        ]);
69
70 4
        Log::info('Customer File updated for Customer ID '.$this->custID.' updated by '.Auth::user()->full_name.'. File Details - ', array($request));
71 4
        return true;
72
    }
73
74
    //  Delete an existing file for the customer
75 2
    public function deleteCustFile($id)
76
    {
77 2
        $fileData = CustomerFiles::find($id);
78 2
        $fileID   = $fileData->file_id;
79 2
        Log::notice('A file for a customer has been deleted by '.Auth::user()->full_name.'.  File Data - ', array($fileData));
80 2
        $fileData->delete();
81
82 2
        $this->deleteFile($fileID);
83
84 2
        return true;
85
    }
86
87
    //  Verify if the parent should get the note
88
    protected function checkParent()
89
    {
90
        $parent = Customers::find($this->custID)->parent_id;
91
        if($parent)
92
        {
93
            $this->custID = $parent;
94
        }
95
    }
96
}
97