Passed
Push — dev5 ( b08d9a...c43caf )
by Ron
04:08
created

GuestLinksController::getFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Http\Controllers\FileLinks;
4
5
use App\User;
6
use App\Files;
7
use App\FileLinks;
8
use App\FileLinkFiles;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\UploadedFile;
11
use Illuminate\Support\Facades\Log;
12
use App\Notifications\NewFileUpload;
13
use App\Http\Controllers\Controller;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Notification;
16
use App\Http\Resources\FileLinkFilesCollection;
17
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
18
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
19
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
20
21
class GuestLinksController extends Controller
22
{
23
    //  Landing page if no link is sent
24 2
    public function index()
25
    {
26 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
27 2
        return view('links.guestIndex');
28
    }
29
30
    //  Show the link details for the user
31 12
    public function show($id)
32
    {
33 12
        $details = FileLinks::where('link_hash', $id)->first();
34
35
        //  Verify that the link is valid
36 12
        if(empty($details))
37
        {
38 2
            Log::warning('Visitor '.\Request::ip().' visited bad link Hash - '.$id);
39 2
            return view('links.guestBadLink');
40
        }
41
        //  Verify that the link has not expired
42 10 View Code Duplication
        else if($details->expire <= date('Y-m-d'))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
        {
44 2
            Log::warning('Visitor '.\Request::ip().' visited expired link Hash - '.$id);
45 2
            return view('links.guestExpiredLink');
46
        }
47
48
        //  Link is valid - determine if the link has files that can be downloaded
49 8
        $files = FileLinkFiles::where('link_id', $details->link_id)
50 8
            ->where('upload', false)
51 8
            ->count();
52
53 8 View Code Duplication
        if($files == 0 && $details->allow_upload === 'No')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
        {
55 2
            Log::warning('Visitor ' . \Request::ip() . ' visited a link that they cannot do anything with.  Hash - ' . $id);
56 2
            return view('links.guestDeadLink');
57
        }
58
59 6
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
60 6
        Log::debug('Link Hash-'.$id);
61 6
        return view('links.guestDetails', [
62 6
            'hash'    => $id,
63 6
            'details' => $details,
64 6
            'hasFiles' => $files > 0 ? true : false,
65 6
            'allowUp' => $details->allow_upload === 'Yes' ? true : false,
66
        ]);
67
    }
68
69
    //  Get the guest available files for the link
70 6
    public function getFiles($id)
71
    {
72 6
        $linkID = FileLinks::where('link_hash', $id)->first()->link_id;
73
74 6
        $files = new FileLinkFilesCollection(
75 6
            FileLinkFiles::where('link_id', $linkID)
76 6
                ->where('upload', 0)
77 6
                ->orderBy('created_at', 'ASC')
78 6
                ->with('Files')
79 6
                ->get()
80
        );
81
82 6
        return $files;
83
    }
84
85
    //  Upload new file
86 6
    public function update(Request $request, $id)
87
    {
88 6
        $request->validate(['name' => 'required', 'file' => 'required']);
89
90 6
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
91
92
        //  Verify that the upload is valid and being processed
93 6
        if($receiver->isUploaded() === false)
94
        {
95
            Log::error('Upload File Missing - '.$request->toArray());
96
            throw new UploadMissingFileException();
97
        }
98
99
        //  Recieve and process the file
100 6
        $save = $receiver->receive();
101
102
        //  See if the uploade has finished
103 6
        if($save->isFinished())
104
        {
105 6
            $this->saveFile($save->getFile(), $id, $request);
106
107 6
            return 'uploaded successfully';
108
        }
109
110
        //  Get the current progress
111
        $handler = $save->handler();
112
113
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
114
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
115
        return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
116
            'done'   => $handler->getPercentageDone(),
117
            'status' => true
118
        ]);
119
    }
120
121
    //  Save the file in the database
122 6
    private function saveFile(UploadedFile $file, $id, $request)
123
    {
124 6
        $details = FileLinks::where('link_hash', $id)->first();
125 6
        $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id;
126
127 6
        $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
128 6
        $file->storeAs($filePath, $fileName);
129
130
        //  Place file in Files table of DB
131 6
        $newFile = Files::create([
132 6
            'file_name' => $fileName,
133 6
            'file_link' => $filePath.DIRECTORY_SEPARATOR
134
        ]);
135 6
        $fileID = $newFile->file_id;
136
137
        //  Place the file in the file link files table of DB
138 6
        FileLinkFiles::create([
139 6
            'link_id'  => $details->link_id,
140 6
            'file_id'  => $fileID,
141 6
            'added_by' => $request->name,
142 6
            'upload'   => 1,
143 6
            'note'     => $request->note
144
        ]);
145
146 6
        Log::info('File uploaded by guest '.\Request::ip().' for file link -'.$details->link_id);
147 6
        Log::debug('File Data -', $request->toArray());
148
149 6
        return response()->json(['success' => true]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
150
    }
151
152
    //  Notify the owner of the link that files were uploaded
153 2
    public function notify(Request $request, $id)
154
    {
155 2
        $request->validate([
156 2
            '_complete' => 'required',
157
            'count'     => 'required|integer'
158
        ]);
159
160 2
        $details = FileLinks::where('link_hash', $id)->first();
161
162 2
        $user = User::find($details->user_id);
163 2
        Notification::send($user, new NewFileUpload($details));
164
165 2
        Log::debug('Notification of file upload sent to User ID - '.$user->user_id);
166 2
    }
167
}
168