Completed
Push — master ( d0bd4d...b4b0fd )
by Igor
23:04
created

File::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 0
cts 11
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
namespace app\models\entity;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
8
/**
9
 * This is the model class 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::class,
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
     * @inheritdoc
66
     * @return \query\FileQuery the active query used by this AR class.
67
     */
68
    public static function find()
69
    {
70
        return new \app\models\query\FileQuery(get_called_class());
71
    }
72
}
73