Upload::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
/**
4
 * @package Installer
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\installer\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
use gplcart\core\models\FileTransfer;
14
use gplcart\core\models\Module;
15
use gplcart\modules\installer\models\Install;
16
17
/**
18
 * Handles incoming requests and outputs data related to the Installer module
19
 */
20
class Upload extends Controller
21
{
22
23
    /**
24
     * Module model instance
25
     * @var \gplcart\core\models\Module $module
26
     */
27
    protected $module;
28
29
    /**
30
     * Installer model class instance
31
     * @var \gplcart\modules\installer\models\Install $install
32
     */
33
    protected $install;
34
35
    /**
36
     * File transfer model class instance
37
     * @var \gplcart\core\models\FileTransfer $file_transfer
38
     */
39
    protected $file_transfer;
40
41
    /**
42
     * Upload constructor.
43
     * @param Module $module
44
     * @param FileTransfer $file_transfer
45
     * @param Install $install
46
     */
47
    public function __construct(Module $module, FileTransfer $file_transfer, Install $install)
48
    {
49
        parent::__construct();
50
51
        $this->module = $module;
52
        $this->install = $install;
53
        $this->file_transfer = $file_transfer;
54
    }
55
56
    /**
57
     * Route page callback to display the module upload page
58
     */
59
    public function editUpload()
60
    {
61
        $this->setTitleEditUpload();
62
        $this->setBreadcrumbEditUpload();
63
        $this->submitUpload();
64
        $this->outputEditUpload();
65
    }
66
67
    /**
68
     * Set title on the module upload page
69
     */
70
    protected function setTitleEditUpload()
71
    {
72
        $this->setTitle($this->text('Upload module'));
73
    }
74
75
    /**
76
     * Set breadcrumbs on the module upload page
77
     */
78
    protected function setBreadcrumbEditUpload()
79
    {
80
        $breadcrumbs = array();
81
82
        $breadcrumbs[] = array(
83
            'text' => $this->text('Dashboard'),
84
            'url' => $this->url('admin')
85
        );
86
87
        $breadcrumbs[] = array(
88
            'text' => $this->text('Modules'),
89
            'url' => $this->url('admin/module/list')
90
        );
91
92
        $this->setBreadcrumbs($breadcrumbs);
93
    }
94
95
    /**
96
     * Handles submitted data
97
     */
98
    protected function submitUpload()
99
    {
100
        if ($this->isPosted('install') && $this->validateUpload()) {
101
            $this->installModuleUpload();
102
        }
103
    }
104
105
    /**
106
     * Validate a submitted data
107
     */
108
    protected function validateUpload()
109
    {
110
        $file = $this->request->file('file');
111
112
        if (empty($file)) {
113
            $this->setError('file', $this->text('Nothing to install'));
114
            return false;
115
        }
116
117
        $result = $this->file_transfer->upload($file, 'zip', gplcart_file_private_module('installer'));
118
119
        if ($result !== true) {
120
            $this->setError('file', $result);
121
            return false;
122
        }
123
124
        $this->setSubmitted('file', $this->file_transfer->getTransferred());
125
        return !$this->hasErrors();
126
    }
127
128
    /**
129
     * Install uploaded module
130
     */
131
    protected function installModuleUpload()
132
    {
133
        $this->controlAccess('file_upload');
134
        $this->controlAccess('module_installer_upload');
135
136
        $file = $this->getSubmitted('file');
137
        $result = $this->install->fromZip($file);
138
139
        if ($result !== true) {
140
            $this->redirect('', $result, 'warning');
141
        }
142
143
        if ($this->install->isUpdate()) {
144
            $message = $this->text('Module has been updated. Previous version has been saved to a <a href="@url">backup</a> file', array('@url' => $this->url('admin/report/backup')));
145
        } else {
146
            $message = $this->text('Module has been uploaded. You should <a href="@url">enable</a> it manually', array('@url' => $this->url('admin/module/list')));
147
        }
148
149
        $this->redirect('', $message, 'success');
150
    }
151
152
    /**
153
     * Render and output the module upload page
154
     */
155
    protected function outputEditUpload()
156
    {
157
        $this->output('installer|upload');
158
    }
159
160
}
161