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

Command::getFiles()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
nop 2
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
use DirectoryIterator;
13
use gplcart\core\Container;
14
use gplcart\core\Handler;
15
16
/**
17
 * Base handler class
18
 */
19
class Command extends Handler
20
{
21
22
    /**
23
     * Scanner model class instance
24
     * @var \gplcart\modules\file_manager\models\Scanner $scanner
25
     */
26
    protected $scanner;
27
28
    /**
29
     * Translation UI model class instance
30
     * @var \gplcart\core\models\Translation $translation
31
     */
32
    protected $translation;
33
34
    /**
35
     * Constructor
36
     */
37
    public function __construct()
38
    {
39
        $this->translation = Container::get('gplcart\\core\\models\\Translation');
40
        $this->scanner = Container::get('gplcart\\modules\\file_manager\\models\\Scanner');
41
    }
42
43
    /**
44
     * Sets a property
45
     * @param string $name
46
     * @param mixed $value
47
     */
48
    public function setProperty($name, $value)
49
    {
50
        $this->{$name} = $value;
51
    }
52
53
    /**
54
     * Returns a total number of scanned files
55
     * @param string $directory
56
     * @param array $options
57
     * @return integer
58
     */
59
    protected function getTotal($directory, array $options = array())
60
    {
61
        $options['count'] = true;
62
        return (int) $this->scanner->scan($directory, $options);
63
    }
64
65
    /**
66
     * Returns an array of scanned files
67
     * @param string $directory
68
     * @param array $options
69
     * @return array
70
     */
71
    protected function getFiles($directory, array $options = array())
72
    {
73
        return (array) $this->scanner->scan($directory, $options);
74
    }
75
76
    /**
77
     * Returns a normalized relative path for the file
78
     * @param string $file
79
     * @return string
80
     */
81
    protected function getRelativeFilePath($file)
82
    {
83
        return gplcart_path_normalize(gplcart_file_relative($file));
84
    }
85
86
    /**
87
     * Returns a relative file path or initial directory
88
     * @param null|string $path
89
     * @return string
90
     */
91
    protected function getRelativePath($path = null)
92
    {
93
        if (!isset($path)) {
94
            $path = $this->scanner->getInitialPath(true);
95
        }
96
97
        return gplcart_path_normalize(gplcart_path_relative($path));
98
    }
99
100
    /**
101
     * Returns a relative directory path for the file
102
     * @param string $file
103
     * @return string
104
     */
105
    protected function getRelativeDirectory($file)
106
    {
107
        return trim(dirname($this->getRelativeFilePath($file)), '.');
108
    }
109
110
    /**
111
     * Moves a file to a new destination
112
     * @param string $src
113
     * @param string $dest
114
     * @param int $errors
115
     * @param int $success
116
     * @return bool
117
     */
118
    protected function move($src, $dest, &$errors = 0, &$success = 0)
119
    {
120
        $this->copy($src, $dest, $errors, $success);
121
122
        if (empty($errors)) {
123
            gplcart_file_delete_recursive($src, $errors);
124
        }
125
126
        return empty($errors);
127
    }
128
129
    /**
130
     * Copy a file / directory
131
     * @param string $src
132
     * @param string $dest
133
     * @param int $errors
134
     * @param int $success
135
     * @return boolean
136
     */
137
    protected function copy($src, $dest, &$errors = 0, &$success = 0)
138
    {
139
        if (is_file($src)) {
140
141
            if (copy($src, $dest)) {
142
                $success++;
143
                return true;
144
            }
145
146
            $errors++;
147
            return false;
148
        }
149
150
        if (!is_dir($dest) && !mkdir($dest)) {
151
            $errors++;
152
            return false;
153
        }
154
155
        foreach (new DirectoryIterator($src) as $file) {
156
157
            $result = null;
158
            $copyto = "$dest/" . $file->getBasename();
159
160
            if ($file->isFile() || (!$file->isDot() && $file->isDir())) {
161
                $result = $this->copy($file->getRealPath(), $copyto, $errors, $success);
162
            }
163
164
            if (!isset($result)) {
165
                continue;
166
            }
167
168
            if ($result) {
169
                $success++;
170
            } else {
171
                $errors++;
172
            }
173
        }
174
175
        $success++;
176
        return true;
177
    }
178
179
    /**
180
     * Whether the current file is the initial file manager path
181
     * @param \SplFileInfo $file
182
     * @return bool
183
     */
184
    protected function isInitialPath($file)
185
    {
186
        $current_path = gplcart_path_normalize($file->getRealPath());
187
        $initial_path = gplcart_path_normalize($this->scanner->getInitialPath(true));
188
189
        return $current_path === $initial_path;
190
    }
191
192
}
193