FilesController::action_get_download()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 13
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
use ICanBoogie\Routing\Controller;
17
use ICanBoogie\Binding\Routing\ControllerBindings;
18
use ICanBoogie\Binding\Routing\ForwardUndefinedPropertiesToApplication;
19
use ICanBoogie\Module\ControllerBindings as ModuleBindings;
20
21
use Icybee\Modules\Files\Binding\CoreBindings;
22
use Icybee\Modules\Files\File;
23
24
/**
25
 * Files public controller.
26
 */
27
class FilesController extends Controller
28
{
29
	use Controller\ActionTrait;
30
	use ControllerBindings, CoreBindings, ModuleBindings;
31
	use ForwardUndefinedPropertiesToApplication;
32
33
	/**
34
	 * @param string $uuid
35
	 *
36
	 * @return FileResponse
37
	 */
38
	public function action_get_show($uuid)
39
	{
40
		$pathname = $this->file_storage->find($uuid);
41
42
		if (!$pathname)
43
		{
44
			return null;
45
		}
46
47
		return new FileResponse($pathname, $this->request, [
0 ignored issues
show
Documentation introduced by
The property $request is declared private in ICanBoogie\Routing\Controller. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
48
49
			FileResponse::OPTION_ETAG => $pathname->hash
50
51
		]);
52
	}
53
54
	/**
55
	 * @param string $uuid
56
	 *
57
	 * @return FileResponse
58
	 */
59
	public function action_get_download($uuid)
60
	{
61
		$matches = $this->file_storage_index->find($uuid);
62
63
		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...
64
		{
65
			return null;
66
		}
67
68
		$key = $matches[0];
69
		$pathname = $this->file_storage->find($key);
70
71
		if (!$pathname)
72
		{
73
			return null;
74
		}
75
76
		/* @var $record File */
77
78
		$record = $this->model[$key->id];
79
80
		return new FileResponse($pathname, $this->request, [
0 ignored issues
show
Documentation introduced by
The property $request is declared private in ICanBoogie\Routing\Controller. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
82
			FileResponse::OPTION_ETAG => $pathname->hash,
83
			FileResponse::OPTION_FILENAME => $record->title . $record->extension,
84
			FileResponse::OPTION_MIME => $record->mime
85
86
		]);
87
	}
88
}
89