Passed
Push — dev5 ( 1140b1...da16e0 )
by Ron
09:38
created

GuestLinksController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
20
class GuestLinksController extends Controller
21
{
22
    //  Landing page if no link is sent
23 2
    public function index()
24
    {
25 2
        Log::debug('Route '.Route::currentRouteName().' visited by IP Address '.\Request::ip());
26 2
        return view('links.guestIndex');
27
    }
28
29
    //  Show the link details for the user
30 12
    public function show($id)
31
    {
32 12
        Log::debug('Route ' . Route::currentRouteName() . ' visited by IP Address ' . \Request::ip());
33
34 12
        $details = FileLinks::where('link_hash', $id)->first();
35
36
        //  Verify that the link is valid
37 12
        if(empty($details))
38
        {
39 2
            Log::warning('Visitor '.\Request::ip().' visited bad link Hash - '.$id);
40 2
            return view('links.guestBadLink');
41
        }
42
        //  Verify that the link has not expired
43 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...
44
        {
45 2
            Log::warning('Visitor '.\Request::ip().' visited expired link Hash - '.$id);
46 2
            return view('links.guestExpiredLink');
47
        }
48
49
        //  Link is valid - determine if the link has files that can be downloaded
50 8
        $files = FileLinkFiles::where('link_id', $details->link_id)
51 8
            ->where('upload', false)
52 8
            ->count();
53
54 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...
55
        {
56 2
            Log::warning('Visitor '.\Request::ip().' visited a link that they cannot do anything with.  Hash - '.$id);
57 2
            return view('links.guestDeadLink');
58
        }
59
60 6
        return view('links.guestDetails', [
61 6
            'hash'    => $id,
62 6
            'details' => $details,
63 6
            'hasFiles' => $files > 0 ? true : false,
64 6
            'allowUp' => $details->allow_upload === 'Yes' ? true : false,
65
        ]);
66
    }
67
68
    //  Get the guest available files for the link
69 6
    public function getFiles($id)
70
    {
71 6
        Log::debug('Route '.Route::currentRouteName().' visited by IP Address '.\Request::ip());
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
        Log::debug('Files gathered for link ID '.$linkID, array($files));
83 6
        return $files;
84
    }
85
86
    //  Upload new file
87 6
    public function update(Request $request, $id)
88
    {
89 6
        Log::debug('Route '.Route::currentRouteName().' visited by IP Address '.\Request::ip().'. Submitted Data - ', $request->toArray());
90 6
        $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
91 6
            'name' => 'required',
92
            'file' => 'required'
93
        ]);
94
95 6
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
96
97
        //  Recieve and process the file
98 6
        $save = $receiver->receive();
99
100
        //  See if the uploade has finished
101 6
        if($save->isFinished())
102
        {
103 6
            $this->saveFile($save->getFile(), $id, $request);
104 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...
105
        }
106
107
        //  Get the current progress
108
        $handler = $save->handler();
109
110
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
111
        return response()->json([
112
            'done'   => $handler->getPercentageDone(),
113
            'status' => true
114
        ]);
115
    }
116
117
    //  Save the file in the database
118 6
    private function saveFile(UploadedFile $file, $id, $request)
119
    {
120 6
        $details = FileLinks::where('link_hash', $id)->first();
121 6
        $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id;
122
123 6
        $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
124 6
        $file->storeAs($filePath, $fileName);
125
126
        //  Place file in Files table of DB
127 6
        $newFile = Files::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Files. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
128 6
            'file_name' => $fileName,
129 6
            'file_link' => $filePath.DIRECTORY_SEPARATOR
130
        ]);
131 6
        $fileID = $newFile->file_id;
132
133
        //  Place the file in the file link files table of DB
134 6
        FileLinkFiles::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\FileLinkFiles. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
135 6
            'link_id'  => $details->link_id,
136 6
            'file_id'  => $fileID,
137 6
            'added_by' => $request->name,
138 6
            'upload'   => 1,
139 6
            'note'     => $request->comments
140
        ]);
141
142 6
        Log::info('File uploaded by guest '.\Request::ip().' for file link -'.$details->link_id);
143 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...
144
    }
145
146
    //  Notify the owner of the link that files were uploaded
147 2
    public function notify(Request $request, $id)
148
    {
149 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by IP Address ' . \Request::ip() . '. Submitted Data - ', $request->toArray());
150
151 2
        $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
152 2
            '_complete' => 'required',
153
            'count'     => 'required|integer'
154
        ]);
155
156 2
        $details = FileLinks::where('link_hash', $id)->first();
157 2
        $user = User::find($details->user_id);
158 2
        Notification::send($user, new NewFileUpload($details));
159
160 2
        Log::info('Notification of file upload sent to '.$user->full_name);
161 2
    }
162
}
163