GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ViewObject::tableName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\caching\TagDependency;
7
use yii\db\ActiveRecord;
8
9
/**
10
 * This is the model class for table "view_object".
11
 *
12
 * @property integer $id
13
 * @property integer $object_id
14
 * @property integer $object_model_id
15
 * @property integer $view_id
16
 * @property integer $child_view_id
17
 */
18
class ViewObject extends ActiveRecord
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    public static function tableName()
24
    {
25
        return '{{%view_object}}';
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function rules()
32
    {
33
        return [
34
            [['view_id', 'child_view_id'], 'default', 'value' => 1],
35
            [['object_id', 'object_model_id', 'view_id', 'child_view_id'], 'required'],
36
            [['object_id', 'object_model_id', 'view_id', 'child_view_id'], 'integer']
37
        ];
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function attributeLabels()
44
    {
45
        return [
46
            'id' => Yii::t('app', 'ID'),
47
            'object_id' => Yii::t('app', 'Object ID'),
48
            'object_model_id' => Yii::t('app', 'Object Model ID'),
49
            'view_id' => Yii::t('app', 'View'),
50
            'child_view_id' => Yii::t('app', 'Child view')
51
        ];
52
    }
53
54
    /**
55
     * Поиск представления по модели
56
     *
57
     * @param BaseObject $model
0 ignored issues
show
Documentation introduced by
Should the type for parameter $model not be BaseObject|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
58
     * @return string|null Возвращает имя файла или null, если ничего не найдено
59
     */
60
    public static function getViewByModel($model = null, $childView = false)
61
    {
62
        if ((null === $model) || !is_object($model)) {
63
            return null;
64
        }
65
66
        if (null === $object = BaseObject::getForClass($model::className())) {
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
67
            return null;
68
        }
69
70
        $cacheKey = "View:Object:ModelId" . $object->id . ":" . $model->id;
71
        $viewObject = Yii::$app->cache->get($cacheKey);
72
        if ($viewObject === false) {
73
74
            $viewObject = static::find()
75
                ->where(
76
                    [
77
                        'object_id' => $object->id,
78
                        'object_model_id' => $model->id,
79
                    ]
80
                )->one();
81
82
        }
83
        if ($viewObject !== null) {
84
            Yii::$app->cache->set(
85
                $cacheKey,
86
                $viewObject,
87
                86400,
88
                new TagDependency([
89
                    'tags' => [
90
                        \devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag($viewObject, $viewObject->id),
91
                        \devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag($object, $object->id),
92
                        \devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag($model, $model->id)
93
                    ]
94
                ])
95
            );
96
            return View::getViewById($childView ? $viewObject->child_view_id : $viewObject->view_id);
97
        } else {
98
            return null;
99
        }
100
101
    }
102
103
    /**
104
     * Поиск связи по модели
105
     *
106
     * @param \yii\db\ActiveRecord $model
0 ignored issues
show
Documentation introduced by
Should the type for parameter $model not be ActiveRecord|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
107
     * @param boolean $forceDefault Флаг для принудительного возврата модели
108
     * @return \yii\db\ActiveRecord|null Возвращает модель или null, если ничего не найдено
0 ignored issues
show
Documentation introduced by
Should the return type not be null|ActiveRecord|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
109
     */
110
    public static function getByModel($model = null, $forceDefault = false)
111
    {
112
        if ((null === $model) || !is_object($model)) {
113
            return null;
114
        }
115
116
        if (null === $object = BaseObject::getForClass($model::className())) {
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
117
            return null;
118
        }
119
120
        if (
121
            null === $result = static::find()->where(
122
                [
123
                    'object_id' => $object->id,
124
                    'object_model_id' => $model->id,
125
                ]
126
            )->one()
127
        ) {
128
            if ($forceDefault) {
129
                $result = new static;
130
                $result->object_id = $object->id;
131
                $result->object_model_id = $model->id;
132
                $result->view_id = 1;
133
                $result->child_view_id = 1;
134
            }
135
        }
136
137
        return $result;
138
    }
139
140
    public static function deleteByViewId($view_id = null)
141
    {
142
        if (null === $view_id) {
143
            return null;
144
        }
145
146
        return static::deleteAll(['view_id' => $view_id]);
147
    }
148
}
149