Completed
Push — master ( 24f445...43b2cf )
by Iurii
01:25
created

Copy::submit()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 12
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 "copy" command
14
 */
15
class Copy 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 in_array($file->getType(), array('file', 'dir')) && $file->isReadable() && !$this->isInitialPath($file);
26
    }
27
28
    /**
29
     * Returns an array data used to display the command
30
     * @return array
31
     */
32
    public function view()
33
    {
34
        return array('file_manager|commands/copy' => array('path' => $this->getRelativePath()));
35
    }
36
37
    /**
38
     * Copies files
39
     * @param \gplcart\core\Controller $controller
40
     * @return array
41
     */
42
    public function submit($controller)
43
    {
44
        set_time_limit(0);
45
46
        $submitted = $controller->getSubmitted();
47
48
        $destination = null;
49
        $errors = $success = 0;
50
51
        foreach ($submitted['files'] as $index => $file) {
52
53
            if (empty($submitted['destinations'][$index])) {
54
                $errors++;
55
                continue;
56
            }
57
58
            $destination = $submitted['destinations'][$index];
59
60
            /* @var $file \SplFileInfo */
61
            $this->copy($file->getRealPath(), $destination, $errors, $success);
62
        }
63
64
        $query = array(
65
            'cmd' => 'list',
66
            'path' => isset($destination) ? dirname($this->getRelativeFilePath($destination)) : ''
67
        );
68
69
        $vars = array('@num_success' => $success, '@num_errors' => $errors);
70
71
        return array(
72
            'redirect' => $controller->url('', $query),
73
            'severity' => empty($errors) ? 'success' : 'warning',
74
            'message' => $this->translation->text('Copied @num_success, errors: @num_errors', $vars)
75
        );
76
    }
77
78
}
79