Passed
Push — dev5a ( e86993...328ab1 )
by Ron
10:15 queued 39s
created

setCustomerFiles::processCustFile()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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