Passed
Push — dev5 ( 2a90e7...f54fc3 )
by Ron
16:20
created

SetFileLinkDetails::createLink()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 15
ccs 12
cts 12
cp 1
crap 3
rs 9.9
1
<?php
2
3
namespace App\Domains\FileLinks;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Auth;
8
9
use App\FileLinks;
10
use App\FileLinkFiles;
11
12
use App\Domains\FilesDomain;
13
14
use App\Http\Requests\FileLinkCreateRequest;
15
use App\Http\Requests\UpdateFileLinkRequest;
16
use App\Http\Requests\UpdateFileLinkInstructionsRequest;
17
18
class SetFileLinkDetails extends FilesDomain
19
{
20
    //  Create a new File Link
21 8
    public function processNewLink(FileLinkCreateRequest $request)
22
    {
23 8
        if(isset($request->file))
24
        {
25 2
            $fileID = $this->processFileChunk($request);
26 2
            if($fileID)
27
            {
28 2
                $fileArr = session('newLinkFile') != null ? session('newLinkFile') : [];
29 2
                $fileArr[] = $fileID;
30 2
                session(['newLinkFile' => $fileArr]);
31
            }
32
33 2
            return false;
34
        }
35
36 8
        return $this->createLink($request);
37
    }
38
39
40
41
    //  Execute will process an uploading file
42
    //  TODO - rename this function
43 2
    public function execute(UpdateFileLinkRequest $request, $linkID)
44
    {
45 2
        $linkData = FileLinks::find($linkID)->update([
46 2
            'link_name'    => $request->name,
47 2
            'expire'       => $request->expire,
48 2
            'allow_upload' => $request->allowUp,
49 2
            'cust_id'      => $request->customerID,
50
        ]);
51
52 2
        Log::info('File Link ID '.$linkID.' has been updated by '.Auth::user()->full_name.'.  Link Data - ', array($linkData));
53 2
        return true;
54
    }
55
56
    //  Update only the instructions attached to the link
57 2
    public function setLinkInstructions(UpdateFileLinkInstructionsRequest $request, $linkID)
58
    {
59 2
        FileLinks::find($linkID)->update([
60 2
            'note' => $request->instructions,
61
        ]);
62
63 2
        Log::info('Instructions for File Link ID '.$linkID.' have been updated by '.Auth::user()->full_name.'.  Instruction Details - ', array($request));
64 2
        return true;
65
    }
66
67
    //  Create the new File Link
68 8
    protected function createLink($linkData)
69
    {
70 8
        $link = FileLinks::create([
71 8
            'user_id'      => Auth::user()->user_id,
72 8
            'cust_id'      => $linkData->customerID,
73 8
            'link_hash'    => $this->generateHash(),
74 8
            'link_name'    => $linkData->name,
75 8
            'expire'       => $linkData->expire,
76 8
            'allow_upload' => isset($linkData->allowUp) && $linkData->allowUp ? true : false,
77 8
            'note'         => $linkData->instructions
78
        ]);
79
80 8
        $this->processFiles($link->link_id);
81 8
        Log::info('User '.Auth::user()->full_name.' created new file link.  Data - ', array($link));
82 8
        return $link->link_id;
83
    }
84
85
    //  Generate a random hash to use as the link.  Verify it is not already in use
86 8
    protected function generateHash()
87
    {
88
        do
89
        {
90 8
            $hash = strtolower(Str::random(15));
91 8
            $dup  = FileLinks::where('link_hash', $hash)->get()->count();
92 8
            Log::debug('New hash created - '.$hash.'.  Checking for duplicate.  Result - '.$dup);
93 8
        } while($dup != 0);
94
95 8
        return $hash;
96
    }
97
98
    //  For all files that were uploaded, move to the proper folder and attach to the tip
99 8
    protected function processFiles($linkID)
100
    {
101 8
        if(session('newLinkFile') != null)
102
        {
103 2
            $files = session('newLinkFile');
104
            // $this->path = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID;
105 2
            $this->path = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$linkID;
106 2
            foreach($files as $file)
107
            {
108 2
                $this->moveFile($this->path, $file);
109
110
                //  Attach file to Tech Tip
111 2
                FileLinkFiles::create([
112 2
                    'link_id' => $linkID,
113 2
                    'file_id' => $file,
114
                ]);
115
            }
116
117 2
            session()->forget('newLinkFile');
118
        }
119
120 8
        return true;
121
    }
122
}
123