Completed
Push — master ( f45cc5...2edd5b )
by Igor
05:32
created

File   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 7
dl 0
loc 57
ccs 0
cts 25
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A behaviors() 0 11 1
B beforeSave() 0 13 5
A generateName() 0 5 1
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
8
/**
9
 * ActiveRecord for table "file"
10
 *
11
 * @property integer $id
12
 * @property integer $user_id
13
 * @property string $title
14
 * @property string $name
15
 * @property string $date_create
16
 * @property string $date_update
17
 * @property integer $ip
18
 */
19
class File extends \yii\db\ActiveRecord
20
{
21
    /**
22
     * @inheritdoc
23
     * @codeCoverageIgnore
24
     * @internal
25
     */
26
    public static function tableName()
27
    {
28
        return 'file';
29
    }
30
31
    /**
32
     * @inheritdoc
33
     * @internal
34
     */
35
    public function behaviors()
36
    {
37
        return [
38
            [
39
                'class' => TimestampBehavior::className(),
40
                'createdAtAttribute' => 'date_create',
41
                'updatedAtAttribute' => 'date_update',
42
                'value' => new \yii\db\Expression('NOW()'),
43
            ],
44
        ];
45
    }
46
47
    /**
48
     * @internal
49
     */
50
    public function beforeSave($insert)
51
    {
52
        if (parent::beforeSave($insert)) {
53
            if ($insert) {
54
                if (!Yii::$app instanceof \yii\console\Application) {
55
                    $this->user_id = Yii::$app->user->isGuest ? 0 : Yii::$app->user->id; // @codeCoverageIgnore
56
                    $this->ip = ip2long(Yii::$app->request->getUserIP()); // @codeCoverageIgnore
57
                } // @codeCoverageIgnore
58
            }
59
            return true;
60
        }
61
        return false; // @codeCoverageIgnore
62
    }
63
64
    /**
65
     * Generate a new name
66
     *
67
     * @param string $extension The file extension
68
     * @return string
69
     */
70
    public function generateName($extension)
71
    {
72
        $name = date('YmdHis') . substr(md5(microtime() . uniqid()), 0, 10);
73
        $this->name = $name . '.' . $extension;
74
    }
75
}
76