Create::view()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
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 "create" command
14
 */
15
class Create 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() && $file->isWritable();
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
        return array(
36
            'file_manager|commands/create' => array(
37
                'path' => $this->getRelativePath($file->getRealPath())
38
            ));
39
    }
40
41
    /**
42
     * Creates a new file
43
     * @param \gplcart\core\Controller $controller
44
     * @return array
45
     */
46
    public function submit($controller)
47
    {
48
        $submitted = $controller->getSubmitted();
49
50
        /* @var $file \SplFileInfo */
51
        $file = reset($submitted['files']);
52
53
        $directory = $submitted['destination'];
54
        $pathinfo = pathinfo($submitted['destination']);
55
56
        if (!empty($pathinfo['extension'])) {
57
            $directory = trim($pathinfo['dirname'], '.');
58
        }
59
60
        $result = true;
61
62
        if ($directory && !file_exists($directory)) {
63
            $result = mkdir($directory, 0777, true);
64
        }
65
66
        if ($result && !empty($pathinfo['basename'])) {
67
            $result = touch($submitted['destination']);
68
        }
69
70
        $query = array(
71
            'cmd' => 'list',
72
            'path' => $this->getRelativeFilePath($file->getRealPath())
73
        );
74
75
        if ($result) {
76
            return array(
77
                'severity' => 'success',
78
                'redirect' => $controller->url('', $query),
79
                'message' => $this->translation->text('File has been created')
80
            );
81
        }
82
83
        return array(
84
            'severity' => 'warning',
85
            'redirect' => $controller->url('', $query),
86
            'message' => $this->translation->text('File has not been created')
87
        );
88
    }
89
90
}
91