Skip to content

Commit ae8e700

Browse files
committed
Switched to methods on the base controller to quiet Codacy
1 parent c22ed21 commit ae8e700

5 files changed

Lines changed: 61 additions & 33 deletions

File tree

app/Http/Controllers/Api/UploadedFilesController.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function index(Request $request, $object_type, $id): JsonResponse|array
3131
{
3232

3333
// Check the permissions to make sure the user can view the object
34-
$object = parent::$map_object_type[$object_type]::withTrashed()->find($id);
34+
$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);
3535
$this->authorize('files', $object);
3636

3737
if (! $object) {
@@ -49,7 +49,7 @@ public function index(Request $request, $object_type, $id): JsonResponse|array
4949
'created_at',
5050
];
5151

52-
$uploads = parent::$map_object_type[$object_type]::withTrashed()->find($id)->uploads()
52+
$uploads = parent::getMapObjectType()[$object_type]::withTrashed()->find($id)->uploads()
5353
->with('adminuser');
5454

5555
$limit = app('api_limit_value');
@@ -97,29 +97,29 @@ public function store(UploadFileRequest $request, $object_type, $id): JsonRespon
9797
// to the object. `manageFiles` is stricter than `files` (used by
9898
// index/show below) so a read-only cascade like the one on
9999
// AssetModelPolicy does not accidentally grant write access.
100-
$object = parent::$map_object_type[$object_type]::withTrashed()->find($id);
100+
$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);
101101
$this->authorize('manageFiles', $object);
102102

103103
if (! $object) {
104104
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.file_upload_status.invalid_object')));
105105
}
106106

107107
// If the file storage directory doesn't exist, create it
108-
if (! Storage::exists(parent::$map_storage_path[$object_type])) {
109-
Storage::makeDirectory(parent::$map_storage_path[$object_type], 775);
108+
if (! Storage::exists(parent::getMapStoragePath()[$object_type])) {
109+
Storage::makeDirectory(parent::getMapStoragePath()[$object_type], 775);
110110
}
111111

112112
if ($request->hasFile('file')) {
113113
// Loop over the attached files and add them to the object
114114
foreach ($request->file('file') as $file) {
115-
$file_name = $request->handleFile(parent::$map_storage_path[$object_type], parent::$map_file_prefix[$object_type].'-'.$object->id, $file);
115+
$file_name = $request->handleFile(parent::getMapStoragePath()[$object_type], parent::getMapFilePrefix()[$object_type].'-'.$object->id, $file);
116116
$files[] = $file_name;
117117
$object->logUpload($file_name, $request->input('notes'));
118118
}
119119

120120
if (isset($files)) {
121121
$file_results = Actionlog::select('action_logs.*')->where('action_type', '=', 'uploaded')
122-
->where('item_type', '=', parent::$map_object_type[$object_type])
122+
->where('item_type', '=', parent::getMapObjectType()[$object_type])
123123
->where('item_id', '=', $id)->whereIn('filename', $files)
124124
->get();
125125

@@ -147,25 +147,25 @@ public function store(UploadFileRequest $request, $object_type, $id): JsonRespon
147147
public function show($object_type, $id, $file_id): JsonResponse|StreamedResponse|Storage|StorageHelper|BinaryFileResponse
148148
{
149149
// Check the permissions to make sure the user can view the object
150-
$object = parent::$map_object_type[$object_type]::withTrashed()->find($id);
150+
$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);
151151
$this->authorize('files', $object);
152152

153153
if (! $object) {
154154
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.file_upload_status.invalid_object')));
155155
}
156156

157157
// Check that the file being requested exists for the object
158-
if (! $log = Actionlog::whereNotNull('filename')->where('item_type', parent::$map_object_type[$object_type])->where('item_id', $object->id)->find($file_id)
158+
if (! $log = Actionlog::whereNotNull('filename')->where('item_type', parent::getMapObjectType()[$object_type])->where('item_id', $object->id)->find($file_id)
159159
) {
160160
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.file_upload_status.invalid_id')), 200);
161161
}
162162

163-
if (! Storage::exists(parent::$map_storage_path[$object_type].$log->filename)) {
163+
if (! Storage::exists(parent::getMapStoragePath()[$object_type].$log->filename)) {
164164
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.file_upload_status.file_not_found'), 200));
165165
}
166166

167167
if (request('inline') == 'true') {
168-
$path = parent::$map_storage_path[$object_type];
168+
$path = parent::getMapStoragePath()[$object_type];
169169

170170
// Only allowlisted extensions may be served inline. Everything
171171
// else (including XML, which can pull an XSLT stylesheet and
@@ -180,7 +180,7 @@ public function show($object_type, $id, $file_id): JsonResponse|StreamedResponse
180180
]);
181181
}
182182

183-
return StorageHelper::downloader(parent::$map_storage_path[$object_type].$log->filename);
183+
return StorageHelper::downloader(parent::getMapStoragePath()[$object_type].$log->filename);
184184

185185
}
186186

@@ -201,7 +201,7 @@ public function destroy($object_type, $id, $file_id): JsonResponse
201201

202202
// See store(): `manageFiles` is the strict write ability so a
203203
// read-only cascade in files() does not authorize deletion.
204-
$object = parent::$map_object_type[$object_type]::withTrashed()->find($id);
204+
$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);
205205
$this->authorize('manageFiles', $object);
206206

207207
if (! $object) {
@@ -212,14 +212,14 @@ public function destroy($object_type, $id, $file_id): JsonResponse
212212
$log = Actionlog::query()
213213
->where('id', $file_id)
214214
->where('action_type', 'uploaded')
215-
->where('item_type', parent::$map_object_type[$object_type])
215+
->where('item_type', parent::getMapObjectType()[$object_type])
216216
->where('item_id', $object->id)
217217
->first();
218218

219219
if ($log) {
220220
// Check the file actually exists, and delete it
221-
if (Storage::exists(parent::$map_storage_path[$object_type].$log->filename)) {
222-
Storage::delete(parent::$map_storage_path[$object_type].$log->filename);
221+
if (Storage::exists(parent::getMapStoragePath()[$object_type].$log->filename)) {
222+
Storage::delete(parent::getMapStoragePath()[$object_type].$log->filename);
223223
}
224224
// Delete the record of the file
225225
if ($log->logUploadDelete($object, $log->filename)) {

app/Http/Controllers/Controller.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,32 @@ public function __construct()
102102
view()->share('signedIn', Auth::check());
103103
view()->share('user', auth()->user());
104104
}
105+
106+
/**
107+
* Accessor for the object-type map. The public static array above is
108+
* kept for back-compat with any external callers that reach into it
109+
* directly, but internal callers should prefer this getter so static
110+
* analyzers (Codacy) don't misparse `parent::$map_object_type` as a
111+
* variable-variable dereference.
112+
*/
113+
public static function getMapObjectType(): array
114+
{
115+
return static::$map_object_type;
116+
}
117+
118+
/**
119+
* Accessor for the storage-path map. See getMapObjectType for rationale.
120+
*/
121+
public static function getMapStoragePath(): array
122+
{
123+
return static::$map_storage_path;
124+
}
125+
126+
/**
127+
* Accessor for the file-prefix map. See getMapObjectType for rationale.
128+
*/
129+
public static function getMapFilePrefix(): array
130+
{
131+
return static::$map_file_prefix;
132+
}
105133
}

app/Http/Controllers/MaintenancesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private function storeUploadedFiles(ImageUploadRequest $request, Maintenance $ma
299299
}
300300

301301
$objectType = 'maintenances';
302-
$storagePath = parent::$map_storage_path[$objectType];
302+
$storagePath = parent::getMapStoragePath()[$objectType];
303303

304304
if (! Storage::exists($storagePath)) {
305305
Storage::makeDirectory($storagePath, 775);
@@ -314,7 +314,7 @@ private function storeUploadedFiles(ImageUploadRequest $request, Maintenance $ma
314314

315315
$fileName = $uploadFileRequest->handleFile(
316316
$storagePath,
317-
parent::$map_file_prefix[$objectType].'-'.$maintenance->id,
317+
parent::getMapFilePrefix()[$objectType].'-'.$maintenance->id,
318318
$file
319319
);
320320

app/Http/Controllers/QrCodeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function show($object_type, $id): Response|BinaryFileResponse|string|bool
3535
return $object_type.' is not a valid type.';
3636
}
3737

38-
$object = parent::$map_object_type[$object_type]::withTrashed()->find($id);
38+
$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);
3939

4040
if (! $object) {
4141
return 'That item is invalid';

app/Http/Controllers/UploadedFilesController.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,28 @@ public function store(UploadFileRequest $request, $object_type, $id): RedirectRe
3939
// to the object. `manageFiles` is stricter than `files` (used by
4040
// show/download) so a read-only cascade like the one on
4141
// AssetModelPolicy does not accidentally grant write access.
42-
$object = parent::$map_object_type[$object_type]::withTrashed()->find($id);
42+
$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);
4343
$this->authorize('manageFiles', $object);
4444

4545
if (! $object) {
4646
return redirect()->back()->withFragment('files')->with('error', trans('general.file_upload_status.invalid_object'));
4747
}
4848

4949
// If the file storage directory doesn't exist, create it
50-
if (! Storage::exists(parent::$map_storage_path[$object_type])) {
51-
Storage::makeDirectory(parent::$map_storage_path[$object_type], 775);
50+
if (! Storage::exists(parent::getMapStoragePath()[$object_type])) {
51+
Storage::makeDirectory(parent::getMapStoragePath()[$object_type], 775);
5252
}
5353

5454
if ($request->hasFile('file')) {
5555
// Loop over the attached files and add them to the object
5656
foreach ($request->file('file') as $file) {
57-
$file_name = $request->handleFile(parent::$map_storage_path[$object_type], parent::$map_file_prefix[$object_type].'-'.$object->id, $file);
57+
$file_name = $request->handleFile(parent::getMapStoragePath()[$object_type], parent::getMapFilePrefix()[$object_type].'-'.$object->id, $file);
5858
$files[] = $file_name;
5959
$object->logUpload($file_name, $request->input('notes'));
6060
}
6161

6262
$files = Actionlog::select('action_logs.*')->where('action_type', '=', 'uploaded')
63-
->where('item_type', '=', parent::$map_object_type[$object_type])
63+
->where('item_type', '=', parent::getMapObjectType()[$object_type])
6464
->where('item_id', '=', $id)->whereIn('filename', $files)
6565
->get();
6666

@@ -87,24 +87,24 @@ public function store(UploadFileRequest $request, $object_type, $id): RedirectRe
8787
public function show($object_type, $id, $file_id): RedirectResponse|StreamedResponse|Storage|StorageHelper|BinaryFileResponse
8888
{
8989
// Check the permissions to make sure the user can view the object
90-
$object = parent::$map_object_type[$object_type]::withTrashed()->find($id);
90+
$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);
9191
$this->authorize('files', $object);
9292

9393
if (! $object) {
9494
return redirect()->back()->withFragment('files')->with('error', trans('general.file_upload_status.invalid_object'));
9595
}
9696

9797
// Check that the file being requested exists for the object
98-
if (! $log = Actionlog::whereNotNull('filename')->where('item_type', parent::$map_object_type[$object_type])->where('item_id', $object->id)->find($file_id)) {
98+
if (! $log = Actionlog::whereNotNull('filename')->where('item_type', parent::getMapObjectType()[$object_type])->where('item_id', $object->id)->find($file_id)) {
9999
return redirect()->back()->withFragment('files')->with('error', trans('general.file_upload_status.invalid_id'));
100100
}
101101

102-
if (! Storage::exists(parent::$map_storage_path[$object_type].$log->filename)) {
102+
if (! Storage::exists(parent::getMapStoragePath()[$object_type].$log->filename)) {
103103
return redirect()->back()->withFragment('files')->with('error', trans('general.file_upload_status.file_not_found'));
104104
}
105105

106106
if (request('inline') == 'true') {
107-
$path = parent::$map_storage_path[$object_type];
107+
$path = parent::getMapStoragePath()[$object_type];
108108

109109
if (! StorageHelper::allowSafeInline($path.$log->filename)) {
110110
return StorageHelper::downloader($path.$log->filename);
@@ -113,7 +113,7 @@ public function show($object_type, $id, $file_id): RedirectResponse|StreamedResp
113113
return Storage::download($path.$log->filename, $log->filename, ['Content-Disposition' => 'inline']);
114114
}
115115

116-
return StorageHelper::downloader(parent::$map_storage_path[$object_type].$log->filename);
116+
return StorageHelper::downloader(parent::getMapStoragePath()[$object_type].$log->filename);
117117

118118
}
119119

@@ -134,21 +134,21 @@ public function destroy($object_type, $id, $file_id): RedirectResponse
134134

135135
// See store(): `manageFiles` is the strict write ability so a
136136
// read-only cascade in files() does not authorize deletion.
137-
$object = parent::$map_object_type[$object_type]::withTrashed()->find($id);
137+
$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);
138138
$this->authorize('manageFiles', $object);
139139

140140
if (! $object) {
141141
return redirect()->back()->withFragment('files')->with('error', trans('general.file_upload_status.invalid_object'));
142142
}
143143

144144
// Check for the file
145-
$log = Actionlog::where('id', $file_id)->where('item_type', parent::$map_object_type[$object_type])
145+
$log = Actionlog::where('id', $file_id)->where('item_type', parent::getMapObjectType()[$object_type])
146146
->where('item_id', $object->id)->first();
147147

148148
if ($log) {
149149
// Check the file actually exists, and delete it
150-
if (Storage::exists(parent::$map_storage_path[$object_type].$log->filename)) {
151-
Storage::delete(parent::$map_storage_path[$object_type].$log->filename);
150+
if (Storage::exists(parent::getMapStoragePath()[$object_type].$log->filename)) {
151+
Storage::delete(parent::getMapStoragePath()[$object_type].$log->filename);
152152
}
153153
// Delete the record of the file
154154
if ($log->logUploadDelete($object, $log->filename)) {

0 commit comments

Comments
 (0)