DownloadController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
namespace Application\Controller;
20
21
use Zend\Filter\Compress;
22
use Zend\Mvc\Controller\AbstractActionController;
23
use Zend\View\Model\ViewModel;
24
25
class DownloadController extends AbstractActionController
26
{
27
    /**
28
     * @var array
29
     */
30
    private $modulesList;
31
32
    /**
33
     * Construct $modulesList.
34
     *
35
     * @param array $modulesList
36
     */
37
    public function __construct(array $modulesList)
38
    {
39
        $this->modulesList = $modulesList;
40
    }
41
42
    /**
43
     * Download module.
44
     *
45
     * /download/:module
46
     */
47
    public function learnmoduleAction()
48
    {
49
        $module = $this->params()->fromRoute('module', '');
50
        $compress = $this->params()->fromRoute('compress', 'zip');
51
52
        $response = $this->getResponse();
53
        if (in_array($module, $this->modulesList)) {
54
            $currDateTime = date('Y-m-dHis');
55
56
            $fileToArchive = $module.'.'.(($compress === 'zip') ? 'zip' : 'bz2');
57
            $archive = $fileToArchive.'-'.$currDateTime;
58
            $filter = new Compress([
59
                'adapter' => ($compress === 'zip') ? 'Zip' : 'Bz2',
60
                'options' => [
61
                    'archive' => './data/'.$archive,
62
                ],
63
            ]);
64
            $compressed = $filter->filter('./module/'.$module);
65
66
            //setting response header....
67
            $response->getHeaders()->addHeaderLine('Content-Disposition', 'attachment; filename="'.$fileToArchive.'"');
68
            $response->getHeaders()->addHeaderLine('Content-Length', filesize($compressed));
69
            // set response with get content of file
70
            $response->setContent(file_get_contents($compressed));
71
72
            //remove file after no need
73
            @unlink('./data/'.$archive);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
74
75
            return $response;
76
        }
77
78
        return new ViewModel();
79
    }
80
}
81