GetFileAction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 20
c 1
b 0
f 1
dl 0
loc 28
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 26 5
1
<?php
2
3
namespace floor12\files\actions;
4
5
use floor12\files\components\SimpleImage;
6
use floor12\files\models\File;
7
use floor12\files\models\FileType;
8
use Yii;
9
use yii\base\Action;
10
use yii\web\NotFoundHttpException;
11
12
class GetFileAction extends Action
13
{
14
    public function run($hash)
15
    {
16
        $model = File::findOne(['hash' => $hash]);
17
18
        if (!$model)
0 ignored issues
show
introduced by
$model is of type yii\db\ActiveRecord, thus it always evaluated to true.
Loading history...
19
            throw new NotFoundHttpException("Запрашиваемый файл не найден");
20
21
        if (!file_exists($model->rootPath))
0 ignored issues
show
Bug introduced by
It seems like $model->rootPath can also be of type null; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

21
        if (!file_exists(/** @scrutinizer ignore-type */ $model->rootPath))
Loading history...
22
            throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.');
23
24
        Yii::$app->response->headers->set('Last-Modified', date("c", $model->created));
25
        Yii::$app->response->headers->set('Cache-Control', 'public, max-age=' . (60 * 60 * 24 * 15));
26
27
        if ($model->type == FileType::IMAGE && $model->watermark) {
28
            $image = new SimpleImage();
29
            $image->load($model->rootPath);
30
            $image->watermark(Yii::getAlias("@frontend/web/design/logo-big.png"));
31
            $tmpName = Yii::getAlias("@runtime/" . md5(time() . $model->id));
32
            $image->save($tmpName, IMAGETYPE_JPEG);
33
            $stream = fopen($tmpName, 'rb');
0 ignored issues
show
Bug introduced by
It seems like $tmpName can also be of type false; however, parameter $filename of fopen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

33
            $stream = fopen(/** @scrutinizer ignore-type */ $tmpName, 'rb');
Loading history...
34
            Yii::$app->response->sendStreamAsFile($stream, $model->title, ['inline' => true, 'mimeType' => "image/jpeg", 'filesize' => $model->size]);
35
            @unlink($tmpName);
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

35
            /** @scrutinizer ignore-unhandled */ @unlink($tmpName);

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...
Bug introduced by
It seems like $tmpName can also be of type false; however, parameter $filename of unlink() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

35
            @unlink(/** @scrutinizer ignore-type */ $tmpName);
Loading history...
36
37
        } else {
38
            $stream = fopen($model->rootPath, 'rb');
0 ignored issues
show
Bug introduced by
It seems like $model->rootPath can also be of type null; however, parameter $filename of fopen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

38
            $stream = fopen(/** @scrutinizer ignore-type */ $model->rootPath, 'rb');
Loading history...
39
            Yii::$app->response->sendStreamAsFile($stream, $model->title, ['inline' => true, 'mimeType' => $model->content_type, 'filesize' => $model->size]);
40
        }
41
    }
42
}
43