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

setCustomerFiles::createFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 2.0078
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Domains\Customers;
4
5
use App\CustomerFiles;
6
7
use App\Domains\Files\SetFiles;
8
9
use Illuminate\Support\Facades\Log;
10
11
class setCustomerFiles extends SetFiles
12
{
13
    protected $custObj;
14
15 12
    public function __construct()
16
    {
17 12
        $this->path    = config('filesystems.paths.default');
18 12
        $this->disk    = 'local';
19 12
        $this->custObj = new GetCustomerDetails;
20 12
    }
21
22
    //  Attach a new file to the customer
23 2
    public function createFile($request, $userID)
24
    {
25 2
        $this->path = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$request->cust_id;
26 2
        $filename = $this->getChunk($request);
27 2
        if(!$filename)
28
        {
29
            return false;
30
        }
31
32 2
        $fileID   = $this->addDatabaseRow($filename, $this->path);
33 2
        $this->processCustFile($fileID, $userID, $request);
34
35 2
        return true;
36
    }
37
38
    //  Update the database information attached to the file, but will not modify the file itself
39 6
    public function updateFile($request, $custFileID)
40
    {
41 6
        $custID = $request->cust_id;
42 6
        if($request->shared)
43
        {
44 2
            $parent = $this->custObj->getParentID($request->cust_id);
45 2
            if($parent)
46
            {
47 2
                $custID = $parent;
48
            }
49
        }
50
51 6
        CustomerFiles::find($custFileID)->update([
52 6
            'cust_id'      => $custID,
53 6
            'file_type_id' => $request->file_type_id,
54 6
            'shared'       => $request->shared,
55 6
            'name'         => $request->name,
56
        ]);
57
58 6
        return true;
59
    }
60
61
    //  Delete a file from the database and storage
62 4
    public function deleteCustFile($custFileID)
63
    {
64 4
        $file = CustomerFiles::find($custFileID);
65 4
        $fileID = $file->file_id;
66 4
        $file->delete();
67
68 4
        $this->deleteFile($fileID);
69
70 4
        return true;
71
    }
72
73 2
    protected function processCustFile($fileID, $userID, $fileData)
74
    {
75 2
        $custID = $fileData->cust_id;
76 2
        if($fileData->shared)
77
        {
78
            $parent = $this->custObj->getParentID($fileData->cust_id);
79
            if($parent)
80
            {
81
                $custID = $parent;
82
            }
83
        }
84
85 2
        $fileID = CustomerFiles::create([
86 2
            'file_id'      => $fileID,
87 2
            'file_type_id' => $fileData->file_type_id,
88 2
            'cust_id'      => $custID,
89 2
            'user_id'      => $userID,
90 2
            'shared'       => $fileData->shared === 'true' ? true : false,
91 2
            'name'         => $fileData->name,
92
        ]);
93
94 2
        return $fileID->cust_file_id;
95
    }
96
}
97