Passed
Push — dev5 ( f54fc3...b5930c )
by Ron
09:26
created

SetFileLinkDetails::updateLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\FileLinkInstructionsRequest;
16
use App\Http\Requests\FileLinkUpdateRequest;
17
use App\Http\Requests\UpdateFileLinkRequest;
0 ignored issues
show
Bug introduced by
The type App\Http\Requests\UpdateFileLinkRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use App\Http\Requests\UpdateFileLinkInstructionsRequest;
0 ignored issues
show
Bug introduced by
The type App\Http\Requests\Update...LinkInstructionsRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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