EmptyDir::allowed()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * @package File manager
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\file_manager\handlers\commands;
11
12
/**
13
 * Contains methods for "emptydir" command
14
 */
15
class EmptyDir extends Command
16
{
17
18
    /**
19
     * Whether the command is allowed for the file
20
     * @param \SplFileInfo $file
21
     * @return bool
22
     */
23
    public function allowed($file)
24
    {
25
        return $file->isDir() && !$this->isInitialPath($file);
26
    }
27
28
    /**
29
     * Returns an array of data used to display the command
30
     * @param \SplFileInfo $file
31
     * @return array
32
     */
33
    public function view($file)
34
    {
35
        $path = $file->getRealPath();
36
37
        return array(
38
            'file_manager|commands/empty' => array(
39
                'name' => $file->getBasename(),
40
                'count' => $this->getTotal($path),
41
                'path' => gplcart_path_relative($path)
42
            ));
43
    }
44
45
    /**
46
     * Empty directories
47
     * @param \gplcart\core\Controller $controller
48
     * @return array
49
     */
50
    public function submit($controller)
51
    {
52
        set_time_limit(0);
53
54
        $path = null;
55
        $errors = $success = 0;
56
57
        /* @var $file \SplFileInfo */
58
        foreach ($controller->getSubmitted('files') as $file) {
59
            $path = $file->getRealPath();
60
            foreach ($this->getFiles($path) as $f) {
61
                gplcart_file_delete_recursive($f, $errors, $success);
62
            }
63
        }
64
65
        $query = array(
66
            'cmd' => 'list',
67
            'path' => isset($path) ? $this->getRelativeFilePath($path) : ''
68
        );
69
70
        $vars = array('@num_success' => $success, '@num_errors' => $errors);
71
72
        return array(
73
            'redirect' => $controller->url('', $query),
74
            'severity' => empty($errors) ? 'success' : 'warning',
75
            'message' => $this->translation->text('Deleted @num_success, errors: @num_errors', $vars)
76
        );
77
    }
78
79
}
80