Passed
Push — master ( de9f16...6836c0 )
by Aleksandr
02:35
created

DefaultController::actionFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
4
namespace carono\exchange1c\controllers;
5
6
7
use yii\data\ArrayDataProvider;
8
use yii\helpers\FileHelper;
9
10
/**
11
 * Class DefaultController
12
 *
13
 * @package carono\exchange1c\controllers
14
 */
15
class DefaultController extends Controller
16
{
17
    public function actionIndex()
18
    {
19
        return $this->render('index');
20
    }
21
22
    public function actionFiles()
23
    {
24
        $data = [];
25
        $dir = $this->module->getTmpDir();
26
        foreach (FileHelper::findFiles($dir) as $file) {
27
            $data[] = ['filename' => substr($file, strlen($dir) + 1), 'size' => filesize($file)];
28
        }
29
        $dataProvider = new ArrayDataProvider(['allModels' => $data]);
30
        return $this->render('files', ['dataProvider' => $dataProvider]);
31
    }
32
33
    public function actionImport()
34
    {
35
        if (\Yii::$app->request->isPost) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
36
37
        }
38
        return $this->render('import');
39
    }
40
41
    public function actionExport()
42
    {
43
        return $this->render('export');
44
    }
45
46
    public function actionMonitor()
47
    {
48
        return $this->render('monitor');
49
    }
50
51
    public function actionDocumentation()
52
    {
53
        $dir = \Yii::getAlias('@vendor/carono/yii2-1c-exchange/files/doc');
54
        $data = [];
55
        foreach (FileHelper::findFiles($dir) as $file) {
56
            $data[] = ['filename' => substr($file, strlen($dir) + 1), 'size' => filesize($file)];
57
        }
58
        $dataProvider = new ArrayDataProvider(['allModels' => $data]);
59
        return $this->render('documentation', ['dataProvider' => $dataProvider]);
60
    }
61
62
    public function actionInterfaces()
63
    {
64
        return $this->render('interfaces');
65
    }
66
67
    public function actionDownload($file)
68
    {
69
        $content = file_get_contents($this->module->getTmpDir($file));
70
        return \Yii::$app->response->sendContentAsFile($content, basename($file));
71
    }
72
73
    public function actionSettings()
74
    {
75
        return $this->render('settings');
76
    }
77
78
    public function actionClearTmp()
79
    {
80
        foreach (FileHelper::findFiles($this->module->getTmpDir()) as $file) {
81
            @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

81
            /** @scrutinizer ignore-unhandled */ @unlink($file);

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...
82
        }
83
        return $this->redirect(\Yii::$app->request->referrer);
84
    }
85
}