GetFilesAction   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 76
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 13 3
A run() 0 22 2
A getFileSize() 0 8 1
1
<?php
2
/**
3
 * This file is part of yii2-imperavi-widget.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @see https://github.com/vova07/yii2-imperavi-widget
9
 */
10
11
namespace vova07\imperavi\actions;
12
13
use Yii;
14
use yii\base\Action;
15
use yii\base\InvalidConfigException;
16
use yii\helpers\FileHelper;
17
use yii\web\Response;
18
19
/**
20
 * `GetFilesAction` returns a `JSON` array of the files found under the specified directory and subdirectories.
21
 * This array can be used in Imperavi Redactor to insert files that have been already uploaded.
22
 *
23
 * Usage:
24
 *
25
 * ```php
26
 * public function actions()
27
 * {
28
 *     return [
29
 *         'get-image' => [
30
 *             'class' => 'vova07\imperavi\actions\GetFilesAction',
31
 *             'url' => 'http://my-site.com/statics/',
32
 *             'path' => '/var/www/my-site.com/web/statics',
33
 *             'options' => ['only' => ['*.txt', '*.md']],
34
 *         ]
35
 *     ];
36
 * }
37
 * ```
38
 *
39
 * @author Vasile Crudu <[email protected]>
40
 *
41
 * @link https://github.com/vova07/yii2-imperavi-widget
42
 */
43
class GetFilesAction extends Action
44
{
45
    /**
46
     * @var string Files directory path.
47
     */
48
    public $path;
49
50
    /**
51
     * @var string Files http URL.
52
     */
53
    public $url;
54
55
    /**
56
     * @var array FileHelper options.
57
     *
58
     * @see FileHelper::findFiles()
59
     */
60
    public $options = [];
61
62
    /**
63
     * @inheritdoc
64
     */
65 12
    public function init()
66
    {
67 12
        if ($this->url === null) {
68 3
            throw new InvalidConfigException('The "url" attribute must be set.');
69
        } else {
70 9
            $this->url = rtrim($this->url, '/') . '/';
71
        }
72 9
        if ($this->path === null) {
73 3
            throw new InvalidConfigException('The "path" attribute must be set.');
74
        } else {
75 6
            $this->path = Yii::getAlias($this->path);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->path) can also be of type boolean. However, the property $path is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
76
        }
77 3
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 3
    public function run()
83
    {
84 3
        Yii::$app->response->format = Response::FORMAT_JSON;
85
86 3
        $files = [];
87
88 3
        foreach (FileHelper::findFiles($this->path, $this->options) as $path) {
89 3
            $file = basename($path);
90 3
            $size = $this->getFileSize($path);
91 3
            $url = $this->url . urlencode($file);
92
93 3
            $files[] = [
94 3
                'id' => $file,
95 3
                'title' => $file,
96 3
                'name' => $file,
97 3
                'link' => $url,
98 3
                'size' => $size,
99
            ];
100 1
        }
101
102 3
        return $files;
103
    }
104
105
    /**
106
     * @param string $path
107
     *
108
     * @return string File size in(B|KB|MB|GB).
109
     */
110 3
    private function getFileSize($path)
111
    {
112 3
        $size = filesize($path);
113 3
        $labels = ['B', 'KB', 'MB', 'GB'];
114 3
        $factor = (int) floor((strlen($size) - 1) / 3);
115
116 3
        return sprintf("%.1f ", $size / pow(1024, $factor)) . $labels[$factor];
117
    }
118
}
119