Upload   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 1
dl 0
loc 71
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A allowed() 0 4 3
A view() 0 4 1
B submit() 0 25 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 gplcart\core\models\User;
13
14
/**
15
 * Contains methods for "upload" command
16
 */
17
class Upload extends Command
18
{
19
20
    /**
21
     * User model class instance
22
     * @var \gplcart\core\models\User $user
23
     */
24
    protected $user;
25
26
    /**
27
     * Upload constructor.
28
     * @param User $user
29
     */
30
    public function __construct(User $user)
31
    {
32
        parent::__construct();
33
34
        $this->user = $user;
35
    }
36
37
    /**
38
     * Whether the command is allowed for the file
39
     * @param \SplFileInfo $file
40
     * @return bool
41
     */
42
    public function allowed($file)
43
    {
44
        return $file->isDir() && $file->isWritable() && $this->user->access('file_upload');
45
    }
46
47
    /**
48
     * Returns an array of data used to display the command
49
     * @return array
50
     */
51
    public function view()
52
    {
53
        return array('file_manager|commands/upload' => array());
54
    }
55
56
    /**
57
     * Upload files
58
     * @param \gplcart\core\Controller $controller
59
     * @return array
60
     */
61
    public function submit($controller)
62
    {
63
        $result = $controller->getSubmitted('uploaded', array());
64
65
        $vars = array(
66
            '@num_errors' => count($result['errors']),
67
            '@num_success' => count($result['transferred'])
68
        );
69
70
        $files = $controller->getSubmitted('files');
71
72
        /* @var $file \SplFileInfo */
73
        $file = reset($files);
74
75
        $query = array(
76
            'cmd' => 'list',
77
            'path' => $this->getRelativeFilePath($file->getRealPath())
78
        );
79
80
        return array(
81
            'redirect' => $controller->url('', $query),
82
            'severity' => empty($result['errors']) ? 'success' : 'warning',
83
            'message' => $this->translation->text('Uploaded @num_success, errors: @num_errors', $vars)
84
        );
85
    }
86
87
}
88