Completed
Push — master ( b04d72...52d636 )
by Jonathan
11:25
created

DownloadController::checkPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Illuminate\Http\Request;
6
use Uccello\Core\Models\Domain;
7
use Uccello\Core\Models\Module;
8
use Storage;
9
10
11
class DownloadController extends Controller
12
{
13
    protected $viewName = null;
14
15
    /**
16
     * Check user permissions
17
     */
18
    protected function checkPermissions()
19
    {
20
        $this->middleware('uccello.permissions:retrieve');
21
    }
22
23
    /**
24
     * Process and display asked page
25
     * @param Domain|null $domain
26
     * @param Module $module
27
     * @param Request $request
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function process(?Domain $domain, Module $module, Request $request)
32
    {
33
        // Pre-process
34
        $this->preProcess($domain, $module, $request);
35
36
        // Get file data from request
37
        $fileData = $this->getFileDataFromRequest();
38
        $fileName = $fileData['name'];
39
        $filePath = $fileData['path'];
40
41
        // Check if file exists
42
        if (!Storage::exists($filePath)) {
43
            abort(404);
44
        }
45
46
        // Download file
47
        return Storage::download($filePath, $fileName);
48
    }
49
50
    /**
51
     * Get file name and path from request data
52
     *
53
     * @return array|\Illuminate\Http\Response
54
     */
55
    protected function getFileDataFromRequest()
56
    {
57
        $fileName = null;
58
        $filePath = null;
59
60
        $recordId = (int) request('id');
61
        $fieldColumn = request('field');
62
63
        // Get record
64
        $record = $this->getRecord($recordId);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $record is correct as $this->getRecord($recordId) targeting Uccello\Core\Http\Contro...Controller::getRecord() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
66
        // Retrieve data from field column
67
        $fileData = $record->{$fieldColumn};
68
69
        if ($fileData) {
70
            $fileParts = explode(';', $fileData);
71
72
            if (count($fileParts) === 2) {
73
                $fileName = $fileParts[0];
74
                $filePath = $fileParts[1];
75
            }
76
        }
77
78
        if (!$fileName || !$filePath) {
79
            abort(404);
80
        }
81
82
        return [
83
            'name' => $fileName,
84
            'path' => $filePath
85
        ];
86
    }
87
88
    /**
89
     * Get record by id
90
     *
91
     * @param int $id
92
     * @return void
93
     */
94
    protected function getRecord(int $id)
95
    {
96
        $record = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $record is dead and can be removed.
Loading history...
97
98
        $modelClass = $this->module->model_class;
99
        $record = $modelClass::findOrFail($id);
100
101
        return $record;
102
    }
103
}
104