Passed
Push — master ( 2a90e7...a622eb )
by Ron
02:27 queued 14s
created

SetFileLinkDetails   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 102
ccs 51
cts 51
cp 1
rs 10
c 1
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A processNewLink() 0 16 4
A processFiles() 0 22 3
A generateHash() 0 10 2
A updateLink() 0 11 1
A setLinkInstructions() 0 8 1
A createLink() 0 15 3
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\FileLinkUpdateRequest;
16
use App\Http\Requests\FileLinkInstructionsRequest;
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
    //  Update the file link
42 2
    public function updateLink(FileLinkUpdateRequest $request, $linkID)
43
    {
44 2
        $linkData = FileLinks::find($linkID)->update([
45 2
            'link_name'    => $request->name,
46 2
            'expire'       => $request->expire,
47 2
            'allow_upload' => $request->allow_upload,
48 2
            'cust_id'      => $request->cust_id,
49
        ]);
50
51 2
        Log::info('File Link ID '.$linkID.' has been updated by '.Auth::user()->full_name.'.  Link Data - ', array($linkData));
52 2
        return true;
53
    }
54
55
    //  Update only the instructions attached to the link
56 2
    public function setLinkInstructions(FileLinkInstructionsRequest $request, $linkID)
57
    {
58 2
        FileLinks::find($linkID)->update([
59 2
            'note' => $request->instructions,
60
        ]);
61
62 2
        Log::info('Instructions for File Link ID '.$linkID.' have been updated by '.Auth::user()->full_name.'.  Instruction Details - ', array($request));
63 2
        return true;
64
    }
65
66
    //  Create the new File Link
67 8
    protected function createLink($linkData)
68
    {
69 8
        $link = FileLinks::create([
70 8
            'user_id'      => Auth::user()->user_id,
71 8
            'cust_id'      => $linkData->customerID,
72 8
            'link_hash'    => $this->generateHash(),
73 8
            'link_name'    => $linkData->name,
74 8
            'expire'       => $linkData->expire,
75 8
            'allow_upload' => isset($linkData->allowUp) && $linkData->allowUp ? true : false,
76 8
            'note'         => $linkData->instructions
77
        ]);
78
79 8
        $this->processFiles($link->link_id);
80 8
        Log::info('User '.Auth::user()->full_name.' created new file link.  Data - ', array($link));
81 8
        return $link->link_id;
82
    }
83
84
    //  Generate a random hash to use as the link.  Verify it is not already in use
85 8
    protected function generateHash()
86
    {
87
        do
88
        {
89 8
            $hash = strtolower(Str::random(15));
90 8
            $dup  = FileLinks::where('link_hash', $hash)->get()->count();
91 8
            Log::debug('New hash created - '.$hash.'.  Checking for duplicate.  Result - '.$dup);
92 8
        } while($dup != 0);
93
94 8
        return $hash;
95
    }
96
97
    //  For all files that were uploaded, move to the proper folder and attach to the tip
98 8
    protected function processFiles($linkID)
99
    {
100 8
        if(session('newLinkFile') != null)
101
        {
102 2
            $files = session('newLinkFile');
103
            // $this->path = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID;
104 2
            $this->path = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$linkID;
105 2
            foreach($files as $file)
106
            {
107 2
                $this->moveFile($this->path, $file);
108
109
                //  Attach file to Tech Tip
110 2
                FileLinkFiles::create([
111 2
                    'link_id' => $linkID,
112 2
                    'file_id' => $file,
113
                ]);
114
            }
115
116 2
            session()->forget('newLinkFile');
117
        }
118
119 8
        return true;
120
    }
121
}
122