UploadService::deleteLocalFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace app\core\services;
4
5
use app\core\exceptions\ErrorCodes;
6
use app\core\exceptions\FileException;
7
use Yii;
8
use yii\base\BaseObject;
9
use yii\helpers\FileHelper;
10
use yii\web\UploadedFile;
11
use yiier\graylog\Log;
12
13
class UploadService extends BaseObject
14
{
15
    /**
16
     * @param UploadedFile $uploadedFile
17
     * @param string $filename
18
     * @return bool|string
19
     * @throws \Exception
20
     */
21
    public function uploadRecord(UploadedFile $uploadedFile, string $filename)
22
    {
23
        try {
24
            if (!$this->saveFile($uploadedFile, $filename)) {
25
                throw new \Exception(Yii::t('app', 'Upload file failed'));
26
            }
27
            $this->checkEncoding($filename);
28
            return $this->getFullFilename($filename, params('uploadWebPath'));
0 ignored issues
show
Bug introduced by
It seems like params('uploadWebPath') can also be of type yii\web\Session; however, parameter $path of app\core\services\UploadService::getFullFilename() 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

28
            return $this->getFullFilename($filename, /** @scrutinizer ignore-type */ params('uploadWebPath'));
Loading history...
29
        } catch (\Exception $e) {
30
            Log::error('upload record error', [$uploadedFile, (string)$e]);
31
            throw new \Exception($e->getMessage(), $e->getCode());
32
        }
33
    }
34
35
    /**
36
     * @param $uploadedFile UploadedFile
37
     * @param string $filename
38
     * @return bool
39
     * @throws \yii\base\Exception
40
     */
41
    protected function saveFile(UploadedFile $uploadedFile, string $filename)
42
    {
43
        $filename = $this->getFullFilename($filename);
44
        $this->deleteLocalFile($filename);
0 ignored issues
show
Bug introduced by
It seems like $filename can also be of type false; however, parameter $filename of app\core\services\UploadService::deleteLocalFile() 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

44
        $this->deleteLocalFile(/** @scrutinizer ignore-type */ $filename);
Loading history...
45
        FileHelper::createDirectory(dirname($filename));
0 ignored issues
show
Bug introduced by
It seems like $filename can also be of type false; however, parameter $path of dirname() 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

45
        FileHelper::createDirectory(dirname(/** @scrutinizer ignore-type */ $filename));
Loading history...
46
        return $uploadedFile->saveAs($filename);
0 ignored issues
show
Bug introduced by
It seems like $filename can also be of type false; however, parameter $file of yii\web\UploadedFile::saveAs() 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

46
        return $uploadedFile->saveAs(/** @scrutinizer ignore-type */ $filename);
Loading history...
47
    }
48
49
50
    /**
51
     * @param $filename
52
     * @param $path
53
     * @return bool|string
54
     */
55
    public function getFullFilename($filename, string $path = '')
56
    {
57
        $path = $path ?: params('uploadSavePath');
58
        $filename = Yii::getAlias(rtrim($path, '/') . '/' . $filename);
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type yii\web\Session; however, parameter $string of rtrim() 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

58
        $filename = Yii::getAlias(rtrim(/** @scrutinizer ignore-type */ $path, '/') . '/' . $filename);
Loading history...
59
        return $filename;
60
    }
61
62
    /**
63
     * @param string $filename
64
     */
65
    public function deleteLocalFile(string $filename)
66
    {
67
        $fileAbsoluteName = $this->getFullFilename($filename);
68
        @unlink($fileAbsoluteName);
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

68
        /** @scrutinizer ignore-unhandled */ @unlink($fileAbsoluteName);

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 $fileAbsoluteName 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

68
        @unlink(/** @scrutinizer ignore-type */ $fileAbsoluteName);
Loading history...
69
    }
70
71
    /**
72
     * @param string $filename
73
     * @param string $encoding
74
     * @throws FileException
75
     */
76
    public function checkEncoding(string $filename, $encoding = 'UTF-8')
77
    {
78
        $fileAbsoluteName = $this->getFullFilename($filename);
79
        if (!mb_check_encoding(file_get_contents($fileAbsoluteName), $encoding)) {
0 ignored issues
show
Bug introduced by
It seems like $fileAbsoluteName can also be of type false; however, parameter $filename of file_get_contents() 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

79
        if (!mb_check_encoding(file_get_contents(/** @scrutinizer ignore-type */ $fileAbsoluteName), $encoding)) {
Loading history...
80
            @unlink($fileAbsoluteName);
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

80
            /** @scrutinizer ignore-unhandled */ @unlink($fileAbsoluteName);

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 $fileAbsoluteName 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

80
            @unlink(/** @scrutinizer ignore-type */ $fileAbsoluteName);
Loading history...
81
            throw new FileException(Yii::t('app/error', ErrorCodes::FILE_ENCODING_ERROR));
82
        }
83
    }
84
}
85