DeleteFileAction   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 66
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 15 3
B run() 0 26 6
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 vova07\imperavi\Widget;
14
use Yii;
15
use yii\base\Action;
16
use yii\base\InvalidConfigException;
17
use yii\web\BadRequestHttpException;
18
use yii\web\Response;
19
20
/**
21
 * UploadAction for images and files.
22
 *
23
 * Usage:
24
 *
25
 * ```php
26
 * public function actions()
27
 * {
28
 *     return [
29
 *         'delete-file' => [
30
 *             'class' => 'vova07\imperavi\actions\DeleteFileAction',
31
 *             'url' => 'http://my-site.com/statics/',
32
 *             'path' => '/var/www/my-site.com/web/statics',
33
 *         ],
34
 *     ];
35
 * }
36
 * ```
37
 *
38
 * @author Vasile Crudu <[email protected]>
39
 *
40
 * @link https://github.com/vova07/yii2-imperavi-widget
41
 */
42
class DeleteFileAction extends Action
43
{
44
    /**
45
     * @var string Path to directory where files will be uploaded.
46
     */
47
    public $path;
48
49
    /**
50
     * @var string URL path to directory where files will be uploaded.
51
     */
52
    public $url;
53
54
    /**
55
     * @var string AJAX attribute name will contain the file identifier.
56
     */
57
    public $attribute = 'fileName';
58
59
    /**
60
     * @inheritdoc
61
     */
62 18
    public function init()
63
    {
64 18
        if ($this->url === null) {
65 3
            throw new InvalidConfigException('The "url" attribute must be set.');
66
        } else {
67 15
            $this->url = rtrim($this->url, '/') . '/';
68
        }
69 15
        if ($this->path === null) {
70 3
            throw new InvalidConfigException('The "path" attribute must be set.');
71
        } else {
72 12
            $this->path = rtrim(Yii::getAlias($this->path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
73
        }
74
75 12
        Widget::registerTranslations();
76 12
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 12
    public function run()
82
    {
83 12
        if (Yii::$app->request->isDelete && Yii::$app->request->isAjax) {
84 9
            Yii::$app->response->format = Response::FORMAT_JSON;
85
86 9
            $fileName = Yii::$app->request->post($this->attribute, null);
87
88 9
            if ($fileName === null) {
89 3
                return ['error' => Yii::t('vova07/imperavi', 'ERROR_FILE_IDENTIFIER_MUST_BE_PROVIDED')];
90
            }
91
92 6
            $file = $this->path . DIRECTORY_SEPARATOR . $fileName;
93
94 6
            if (!file_exists($file)) {
95 3
                return ['error' => Yii::t('vova07/imperavi', 'ERROR_FILE_DOES_NOT_EXIST')];
96
            }
97
98 3
            if (!unlink($file)) {
99
                return ['error' => Yii::t('vova07/imperavi', 'ERROR_CANNOT_REMOVE_FILE')];
100
            }
101
102 3
            return ['url' => $this->url . urlencode($fileName)];
103
        } else {
104 3
            throw new BadRequestHttpException('Only DELETE AJAX request is allowed');
105
        }
106
    }
107
}
108