Completed
Push — master ( c5eccd...ec8b64 )
by Evgenii
04:45
created

DefaultController::actionImage()   B

Complexity

Conditions 11
Paths 7

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 49
rs 7.3166
c 1
b 0
f 0
cc 11
nc 7
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 01.01.2018
6
 * Time: 12:03
7
 */
8
9
namespace floor12\files\controllers;
10
11
use floor12\files\actions\GetFileAction;
12
use floor12\files\actions\GetPreviewAction;
13
use floor12\files\components\FileInputWidget;
14
use floor12\files\logic\FileCreateFromInstance;
15
use floor12\files\logic\FileCropRotate;
16
use floor12\files\logic\FileRename;
17
use floor12\files\models\File;
18
use Yii;
19
use yii\filters\VerbFilter;
20
use yii\web\BadRequestHttpException;
21
use yii\web\Controller;
22
use yii\web\NotFoundHttpException;
23
use yii\web\Response;
24
use yii\web\UploadedFile;
25
use ZipArchive;
26
27
class DefaultController extends Controller
28
{
29
    /**
30
     * @var array
31
     */
32
    private $actionsToCheck = [
33
        'crop',
34
        'rename',
35
        'upload',
36
    ];
37
38
    /**
39
     * @inheritDoc
40
     * @return array
41
     */
42
    public function behaviors()
43
    {
44
        return [
45
            'verbs' => [
46
                'class' => VerbFilter::class,
47
                'actions' => [
48
                    'zip' => ['GET', 'HEAD'],
49
                    'cropper' => ['GET', 'HEAD'],
50
                    'crop' => ['POST', 'HEAD'],
51
                    'rename' => ['POST', 'HEAD'],
52
                    'upload' => ['POST', 'HEAD'],
53
                    'get' => ['GET', 'HEAD'],
54
                    'preview' => ['GET', 'HEAD'],
55
                ],
56
            ],
57
        ];
58
    }
59
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function beforeAction($action)
65
    {
66
        $this->checkFormToken();
67
        return parent::beforeAction($action);
68
    }
69
70
    /** Првоеряем токен
71
     * @throws BadRequestHttpException
72
     */
73
    private function checkFormToken()
74
    {
75
        if (in_array($this->action->id, $this->actionsToCheck) && FileInputWidget::generateToken() != Yii::$app->request->post('_fileFormToken'))
76
            throw new BadRequestHttpException('File-form token is wrong or missing.');
77
    }
78
79
    /**
80
     * @param array $hashes
81
     * @param string $title
82
     */
83
    public function actionZip(array $hash, $title = 'files')
84
    {
85
        $files = File::find()->where(["IN", "hash", $hash])->all();
86
87
        $zip = new  ZipArchive;
88
        $filename = Yii::getAlias("@webroot/assets/files.zip");
89
        if (file_exists($filename))
90
            @unlink($filename);
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

90
            /** @scrutinizer ignore-unhandled */ @unlink($filename);

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...
91
        if (sizeof($files) && $zip->open($filename, ZipArchive::CREATE)) {
92
93
            foreach ($files as $file)
94
                $zip->addFile($file->rootPath, $file->title);
0 ignored issues
show
Bug introduced by
Accessing rootPath on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing title on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
95
96
            $zip->close();
97
            header("Pragma: public");
98
            header("Expires: 0");
99
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
100
            header("Cache-Control: public");
101
            header("Content-Description: File Transfer");
102
            header("Content-type: application/octet-stream");
103
            header("Content-Disposition: attachment; filename={$title}.zip");
104
            header("Content-Transfer-Encoding: binary");
105
            header("Content-Length: " . filesize($filename));
106
107
            while (ob_get_level()) {
108
                ob_end_clean();
109
            }
110
            readfile($filename);
111
        } else {
112
            echo 'Failed!';
113
        }
114
    }
115
116
    /** Возвращаем HTML шаблон для внедрения в основной макет
117
     * @return string
118
     */
119
    public function actionCropper()
120
    {
121
        return $this->renderPartial('_cropper');
122
    }
123
124
    /** Кропаем и поворачиваем картинку, возращая ее новый адрес.
125
     * @return string
126
     * @throws \yii\base\InvalidConfigException
127
     */
128
    public function actionCrop()
129
    {
130
        return Yii::createObject(FileCropRotate::class, [Yii::$app->request->post()])->execute();
131
    }
132
133
    /** Переименовываем файл
134
     * @return string
135
     * @throws BadRequestHttpException
136
     * @throws \yii\base\InvalidConfigException
137
     */
138
    public function actionRename()
139
    {
140
        return Yii::createObject(FileRename::class, [Yii::$app->request->post()])->execute();
141
    }
142
143
144
    /** Создаем новый файл
145
     * @return string
146
     * @throws BadRequestHttpException
147
     * @throws \yii\base\InvalidConfigException
148
     */
149
    public function actionUpload()
150
    {
151
        $model = Yii::createObject(FileCreateFromInstance::class, [
152
            UploadedFile::getInstanceByName('file'),
153
            Yii::$app->request->post(),
154
            Yii::$app->user->identity,
155
        ])->execute();
156
157
158
        if ($model->errors) {
159
            throw new BadRequestHttpException('Ошибки валидации модели');
160
        }
161
162
        $ratio = Yii::$app->request->post('ratio') ?? null;
163
164
        $view = Yii::$app->request->post('mode') == 'single' ? "_single" : "_file";
165
166
        if ($ratio)
167
            $this->getView()->registerJs("initCropper({$model->id}, '{$model->href}', {$ratio}, true);");
168
169
        return $this->renderAjax($view, [
170
            'model' => $model,
171
            'ratio' => $ratio
172
        ]);
173
    }
174
175
    /**
176
     * @return array|string[]
177
     */
178
    public function actions()
179
    {
180
        return [
181
            'get' => GetFileAction::class,
182
            'image' => GetPreviewAction::class
183
        ];
184
    }
185
186
    /*
187
     * Выдача файлов через контроллер.
188
     */
189
    public function actionPreview($hash)
190
    {
191
        $model = File::findOne(['hash' => $hash]);
192
193
        if (!$model)
0 ignored issues
show
introduced by
$model is of type yii\db\ActiveRecord, thus it always evaluated to true.
Loading history...
194
            throw new NotFoundHttpException("Запрашиваемый файл не найден в базе.");
195
196
        $response = Yii::$app->response;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->response can also be of type yii\web\Response. However, the property $response is declared as type yii\console\Response. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
197
        $response->format = Response::FORMAT_RAW;
198
        $response->getHeaders()->set('Content-Type', 'image/jpeg; charset=utf-8');
199
200
        Yii::$app->response->headers->set('Last-Modified', date("c", $model->created));
201
        Yii::$app->response->headers->set('Cache-Control', 'public, max-age=' . (60 * 60 * 24 * 15));
202
203
        if (!file_exists($model->rootPreviewPath))
204
            throw new NotFoundHttpException('Preview not found.');
205
206
        $response->sendFile($model->rootPreviewPath, 'preview.jpg');
207
208
    }
209
}
210