Passed
Push — master ( 1d6d9a...2a90e7 )
by Ron
02:47 queued 12s
created

FileLinksController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
namespace App\Http\Controllers\FileLinks;
4
5
use App\Http\Controllers\Controller;
6
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\Auth;
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
        return view('links.index');
39
    }
40
41
    //  Ajax call to show the links for a specific user
42 8
    public function find($id)
43
    {
44
        //  If the user is trying to access the links of another user, they must have the proper permissions permissions
45 8
        if ($id != 0)
46
        {
47 6
            $this->authorize('hasAccess', 'manage_users');
48
        }
49
50 4
        return (new GetFileLinks($id, true))->execute();
51
    }
52
53
    //  Create a new file link form
54 2
    public function create()
55
    {
56 2
        return view('links.newLink');
57
    }
58
59
    //  Submit the new file link form
60 8
    public function store(NewFileLinkRequest $request)
61
    {
62
        //  If there are files, process them first in their chunks
63 8
        if(!empty($request->file))
64
        {
65 2
            (new SaveFileLinkFile)->execute($request);
66 2
            return response()->json(['success' => true]);
67
        }
68
69 8
        $linkObj = new CreateFileLink();
70 8
        $linkData = $linkObj->create($request);
71
72 8
        return response()->json($linkData);
73
    }
74
75
    //  Show details about a file link
76 4
    public function details($id, /** @scrutinizer ignore-unused */ $name)
77
    {
78 4
        $linkDataObj = new GetFileLinkDetails($id);
79
80
        //  If the link is invalid, return an error page
81 4
        if(!$linkDataObj->isLinkValid())
82
        {
83 2
            Log::warning('User '.Auth::user()->full_name.' tried to view invalid file link', ['user_id' => Auth::user()->user_id, 'link_id' => $id]);
84 2
            return view('links.badLink');
85
        }
86
87 2
        return view('links.details', [
88 2
            'link_id'    => $id,
89 2
            'cust_id'    => $linkDataObj->getLinkCustomer(),
90 2
            'file_types' => (new GetCustomerFileTypes)->execute(true),
91
        ]);
92
    }
93
94
    //  Ajax call te get JSON details of the link
95 2
    public function show($id)
96
    {
97 2
        $linkDataObj = new GetFileLinkDetails($id);
98 2
        $linkData = $linkDataObj->execute(true);
99 2
        return $linkData;
100
    }
101
102
    //  Update the link's details
103 2
    public function update(UpdateFileLinkRequest $request, $id)
104
    {
105 2
        (new SetFileLinkDetails)->execute($request, $id);
106 2
        return response()->json(['success' => true]);
107
    }
108
109
    //  Retrieve the instructions attached to a link
110 2
    public function getInstructions($linkID)
111
    {
112 2
        return response()->json(['note' => (new GetFileLinkDetails($linkID))->getLinkInstructions()]);
113
    }
114
115
    //  Update the instructions attached to the link
116 2
    public function submitInstructions(UpdateFileLinkInstructionsRequest $request, $linkID)
117
    {
118 2
        (new SetFileLinkDetails)->setLinkInstructions($request, $linkID);
119 2
        return response()->json(['success' => true]);
120
    }
121
122
    //  Disable a file linke, but do not remove it (set the expire date to a previous date)
123 2
    public function disableLink($id)
124
    {
125 2
        (new KillFileLink)->disableLink($id);
126 2
        return response()->json(['success' => true]);
127
    }
128
129
    //  Delete a file link
130 2
    public function destroy($id)
131
    {
132 2
        (new KillFileLink)->deleteFileLink($id);
133 2
        return response()->json(['success' => true]);
134
    }
135
}
136