Completed
Push — master ( 3b1f1a...f15f2a )
by Igor
02:55
created

File::detectExtension()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 13
nc 4
nop 2
crap 4
1
<?php
2
3
/**
4
 * @link https://github.com/rkit/filemanager-yii2
5
 * @copyright Copyright (c) 2015 Igor Romanov
6
 * @license [MIT](http://opensource.org/licenses/MIT)
7
 */
8
9
namespace rkit\filemanager\models;
10
11
use Yii;
12
use yii\behaviors\TimestampBehavior;
13
use yii\helpers\FileHelper;
14
15
/**
16
 * ActiveRecord for table "file"
17
 *
18
 * @property integer $id
19
 * @property integer $user_id
20
 * @property string $title
21
 * @property string $name
22
 * @property integer $size
23
 * @property string $extension
24
 * @property string $mime
25
 * @property string $date_create
26
 * @property string $date_update
27
 * @property integer $ip
28
 */
29
class File extends \yii\db\ActiveRecord
30
{
31
    /**
32
     * @var string
33
     */
34
    public $path;
35
    /**
36
     * @inheritdoc
37
     * @codeCoverageIgnore
38
     * @internal
39
     */
40
    public static function tableName()
41
    {
42
        return 'file';
43
    }
44
45
    /**
46
     * @inheritdoc
47
     * @codeCoverageIgnore
48
     * @internal
49
     */
50
    public function attributeLabels()
51
    {
52
        return [
53
            'id' => Yii::t('filemanager-yii2', 'ID'),
54
            'user_id' => Yii::t('filemanager-yii2', 'User'),
55
            'title' => Yii::t('filemanager-yii2', 'Title'),
56
            'name' => Yii::t('filemanager-yii2', 'Name'),
57
            'size' => Yii::t('filemanager-yii2', 'Size'),
58
            'extension' => Yii::t('filemanager-yii2', 'Extension'),
59
            'mime' => Yii::t('filemanager-yii2', 'Mime'),
60
            'date_create' => Yii::t('filemanager-yii2', 'Date create'),
61
            'date_update' => Yii::t('filemanager-yii2', 'Date update'),
62
            'ip' => Yii::t('filemanager-yii2', 'IP'),
63
        ];
64
    }
65
66
    /**
67
     * @inheritdoc
68
     * @internal
69
     */
70
    public function behaviors()
71
    {
72
        return [
73
            [
74
                'class' => TimestampBehavior::className(),
75
                'createdAtAttribute' => 'date_create',
76
                'updatedAtAttribute' => 'date_update',
77
                'value' => new \yii\db\Expression('NOW()'),
78
            ],
79
        ];
80
    }
81
82
    /**
83
     * @internal
84
     */
85 38
    public function beforeSave($insert)
86
    {
87
        if (parent::beforeSave($insert)) {
88
            if ($insert) {
89 38
                if (!Yii::$app instanceof \yii\console\Application) {
90 38
                    $this->user_id = Yii::$app->user->isGuest ? 0 : Yii::$app->user->id; // @codeCoverageIgnore
91 38
                    $this->ip = ip2long(Yii::$app->request->getUserIP()); // @codeCoverageIgnore
92 38
                } // @codeCoverageIgnore
93 38
                $this->fillMetaInfo();
94 38
            }
95
            return true;
96
        }
97
        return false; // @codeCoverageIgnore
98
    }
99
100
    /**
101
     * Fill meta info
102
     *
103
     * @param string $path File path
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
104
     * @return void
105
     * @SuppressWarnings(PHPMD.ElseExpression)
106
     */
107
    private function fillMetaInfo()
108
    {
109
        $pathInfo = pathinfo($this->path);
110
111
        if ($this->title === null) {
112 35
            $this->title = $pathInfo['filename'];
113
        }
114 35
115 35
        $this->size = filesize($this->path);
116 35
        $this->mime = FileHelper::getMimeType($this->path);
117 1
        $this->name = $this->generateName($this->path, $this->mime);
118
    }
119
120 34
    /**
121 34
     * Get extension By MimeType
122
     *
123 34
     * @return string
124 34
     */
125 34
    private function detectExtension($path, $mimeType)
126 34
    {
127
        $extensions = FileHelper::getExtensionsByMimeType($mimeType);
128 34
        $pathExtension = pathinfo($path, PATHINFO_EXTENSION);
129
        $titleExtension = pathinfo($this->title, PATHINFO_EXTENSION);
130
131
        if (array_search($pathExtension, $extensions) !== false) {
132
            return $pathExtension;
133
        } elseif (array_search($titleExtension, $extensions) !== false) {
134 34
            return $titleExtension;
135
        }
136 34
        $extension = explode('/', $mimeType);
137
        $extension = end($extension);
138
        if (array_search($extension, $extensions) !== false) {
139
            return $extension;
140 34
        }
141
142 34
        return current($extensions); // @codeCoverageIgnore
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression current($extensions); of type integer|string|false adds false to the return on line 142 which is incompatible with the return type documented by rkit\filemanager\models\File::detectExtension of type string. It seems like you forgot to handle an error condition.
Loading history...
143
    }
144 34
145
    /**
146 34
     * Generate a name
147 6
     *
148 6
     * @return string
149
     */
150 34
    private function generateName($path, $mimeType)
151 34
    {
152 34
        $name = date('YmdHis') . substr(md5(microtime() . uniqid()), 0, 10);
153 34
        return $name . '.' . $this->detectExtension($path, $mimeType);
154 34
    }
155
}
156