FilesAdminController::action_download()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 15
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Icybee package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Icybee\Modules\Files\Routing;
13
14
use ICanBoogie\HTTP\FileResponse;
15
use ICanBoogie\HTTP\Request;
16
17
use Icybee\Modules\Files\Binding\CoreBindings;
18
use Icybee\Modules\Files\File;
19
use Icybee\Modules\Files\FileModel;
20
use Icybee\Modules\Files\Module;
21
use Icybee\Modules\Nodes\Routing\NodesAdminController;
22
23
/**
24
 * @property FileModel $model
25
 */
26
class FilesAdminController extends NodesAdminController
27
{
28
	use CoreBindings;
29
30
	protected function action_show($id)
31
	{
32
		/* @var $record File */
33
34
		$record = $this->model[$id];
35
36
		$this->assert_has_permission(Module::PERMISSION_ACCESS, $record);
37
38
		$pathname = $this->file_storage->find($id);
39
40
		if (!$pathname)
41
		{
42
			return null;
43
		}
44
45
		return new FileResponse($pathname, $this->request, [
46
47
			FileResponse::OPTION_ETAG => $pathname->hash
48
49
		]);
50
	}
51
52
	protected function action_download($id)
53
	{
54
		/* @var $record File */
55
56
		$record = $this->model[$id];
57
58
		$this->assert_has_permission(Module::PERMISSION_ACCESS, $record);
59
60
		$matches = $this->file_storage_index->find($id);
61
62
		if (!$matches)
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type Icybee\Modules\Files\Storage\IndexKey[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
63
		{
64
			return null;
65
		}
66
67
		$key = $matches[0];
68
		$pathname = $this->file_storage->find($key);
69
70
		if (!$pathname)
71
		{
72
			return null;
73
		}
74
75
		/* @var $record File */
76
77
		$record = $this->model[$key->id];
78
79
		return new FileResponse($pathname, $this->request, [
80
81
			FileResponse::OPTION_ETAG => $pathname->hash,
82
			FileResponse::OPTION_FILENAME => $record->title . $record->extension,
83
			FileResponse::OPTION_MIME => $record->mime
84
85
		]);
86
	}
87
}
88