Passed
Push — dev5 ( da16e0...6692a0 )
by Ron
08:20
created

FileLinksController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 2
rs 10
1
<?php
2
3
namespace App\Http\Controllers\FileLinks;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Route;
9
10
use App\Domains\FileLinks\GetFileLinks;
11
use App\Domains\FileLinks\KillFileLink;
12
use App\Domains\FileLinks\CreateFileLink;
13
use App\Domains\FileLinks\SaveFileLinkFile;
14
use App\Domains\FileLinks\GetFileLinkDetails;
15
use App\Domains\FileLinks\SetFileLinkDetails;
16
use App\Domains\FileTypes\GetCustomerFileTypes;
17
18
use App\Http\Requests\NewFileLinkRequest;
19
use App\Http\Requests\UpdateFileLinkRequest;
20
use App\Http\Requests\UpdateFileLinkInstructionsRequest;
21
22
class FileLinksController extends Controller
23
{
24
    //  Only authorized users have access
25 88
    public function __construct()
26
    {
27
        //  Verify the user is logged in and has permissions for this page
28 88
        $this->middleware('auth');
29
        $this->middleware(function($request, $next) {
30 66
            $this->authorize('hasAccess', 'Use File Links');
31 44
            return $next($request);
32 88
        });
33 88
    }
34
35
    //  Landing page shows all links that the user owns
36 2
    public function index()
37
    {
38 2
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
39 2
        return view('links.index');
40
    }
41
42
    //  Ajax call to show the links for a specific user
43 8
    public function find($id)
44
    {
45 8
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
46
47
        //  If the user is trying to access the links of another user, they must have the proper permissions permissions
48 8
        if ($id != 0)
49
        {
50 6
            $this->authorize('hasAccess', 'manage_users');
51
        }
52
53 4
        return (new GetFileLinks($id, true))->execute();
54
    }
55
56
    //  Create a new file link form
57 2
    public function create()
58
    {
59 2
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
60 2
        return view('links.newLink');
61
    }
62
63
    //  Submit the new file link form
64 8
    public function store(NewFileLinkRequest $request)
65
    {
66 8
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray());
67
68
        //  If there are files, process them first in their chunks
69 8
        if(!empty($request->file))
70
        {
71 2
            (new SaveFileLinkFile)->execute($request);
72 2
            return response()->json(['success' => true]);
73
        }
74
75 8
        $linkObj = new CreateFileLink();
76 8
        $linkData = $linkObj->create($request);
77
78 8
        Log::info('File Link Created for '.Auth::user()->full_name.'.  Link Data - ', $linkData);
79 8
        return response()->json($linkData);
80
    }
81
82
    //  Show details about a file link
83 4
    public function details($id, /** @scrutinizer ignore-unused */ $name)
84
    {
85 4
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
86
87 4
        $linkDataObj = new GetFileLinkDetails($id);
88
89
        //  If the link is invalid, return an error page
90 4
        if(!$linkDataObj->isLinkValid())
91
        {
92 2
            Log::warning('User '.Auth::user()->full_name.' tried to view invalid file link', ['user_id' => Auth::user()->user_id, 'link_id' => $id]);
93 2
            return view('links.badLink');
94
        }
95
96 2
        return view('links.details', [
97 2
            'link_id'    => $id,
98 2
            'cust_id'    => $linkDataObj->getLinkCustomer(),
99 2
            'file_types' => (new GetCustomerFileTypes)->execute(true),
100
        ]);
101
    }
102
103
    //  Ajax call te get JSON details of the link
104 2
    public function show($id)
105
    {
106 2
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
107
108 2
        $linkDataObj = new GetFileLinkDetails($id);
109 2
        $linkData = $linkDataObj->execute(true);
110
111 2
        return $linkData;
112
    }
113
114
    //  Update the link's details
115 2
    public function update(UpdateFileLinkRequest $request, $id)
116
    {
117 2
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray());
118
119 2
        (new SetFileLinkDetails)->execute($request, $id);
120
121 2
        Log::info('File Link Updated by '.Auth::user()->full_name, ['link_id' => $id]);
122 2
        return response()->json(['success' => true]);
123
    }
124
125
    //  Retrieve the instructions attached to a link
126 2
    public function getInstructions($linkID)
127
    {
128 2
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
129 2
        return response()->json(['note' => (new GetFileLinkDetails($linkID))->getLinkInstructions()]);
130
    }
131
132
    //  Update the instructions attached to the link
133 2
    public function submitInstructions(UpdateFileLinkInstructionsRequest $request, $linkID)
134
    {
135 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name . '. Submitted Data - ', $request->toArray());
136
137 2
        (new SetFileLinkDetails)->setLinkInstructions($request, $linkID);
138
139 2
        return response()->json(['success' => true]);
140
    }
141
142
    //  Disable a file linke, but do not remove it (set the expire date to a previous date)
143 2
    public function disableLink($id)
144
    {
145 2
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
146
147 2
        (new KillFileLink)->disableLink($id);
148
149 2
        Log::info('User '.Auth::user()->full_name.' disabled link ID - '.$id);
150 2
        return response()->json(['success' => true]);
151
    }
152
153
    //  Delete a file link
154 2
    public function destroy($id)
155
    {
156 2
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
157
158 2
        (new KillFileLink)->deleteFileLink($id);
159
160 2
        Log::info('File link ID - '.$id.' deleted by '.Auth::user()->full_name);
161 2
        return response()->json(['success' => true]);
162
    }
163
}
164