File::beforeSave()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.5222
c 0
b 0
f 0
ccs 0
cts 8
cp 0
cc 5
nc 5
nop 1
crap 30
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
     * @internal
24
     */
25
    public static function tableName()
26
    {
27
        return '{{%file}}';
28
    }
29
30
    /**
31
     * @inheritdoc
32
     * @internal
33
     */
34
    public function behaviors()
35
    {
36
        return [
37
            [
38
                'class' => TimestampBehavior::class,
39
                'createdAtAttribute' => 'date_create',
40
                'updatedAtAttribute' => 'date_update',
41
                'value' => new \yii\db\Expression('NOW()'),
42
            ],
43
        ];
44
    }
45
46
    /**
47
     * @internal
48
     */
49
    public function beforeSave($insert)
50
    {
51
        if (parent::beforeSave($insert)) {
52
            if ($insert) {
53
                if (!Yii::$app instanceof \yii\console\Application) {
54
                    $this->user_id = Yii::$app->user->isGuest ? 0 : Yii::$app->user->id;
55
                    $this->ip = ip2long(Yii::$app->request->getUserIP());
56
                }
57
            }
58
            return true;
59
        }
60
        return false;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     * @return \app\models\query\FileQuery The active query used by this AR class
66
     */
67
    public static function find()
68
    {
69
        return new \app\models\query\FileQuery(get_called_class());
70
    }
71
}
72