ConfirmationRequest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 7
dl 0
loc 138
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 0 11 1
A attributeLabels() 0 15 1
A relations() 0 7 1
A behaviors() 0 8 1
A getViewLink() 0 3 1
A generateReleaseToken() 0 4 1
A getCreatedBy() 0 4 1
A getUpdatedBy() 0 4 1
A getUserClassName() 0 3 1
A release() 0 21 4
A constructObject() 0 3 1
1
<?php
2
3
namespace enigmatix\confirmation;
4
5
use yii\base\ErrorException;
6
use yii\behaviors\BlameableBehavior;
7
use yii\helpers\ArrayHelper;
8
use yii\behaviors\TimestampBehavior;
9
use Yii;
10
use yii\helpers\Url;
11
12
13
/**
14
 * This is the model class for table "{{%confirmation_request}}".
15
 *
16
 * @property integer $id
17
 * @property string $model
18
 * @property integer $object_id
19
 * @property string $object
20
 * @property string $release_token
21
 * @property integer $created_at
22
 * @property integer $updated_at
23
 * @property string $values
24
 * @property integer $created_by
25
 * @property integer $updated_by
26
 *
27
 * @property User $createdBy
28
 * @property User $updatedBy
29
 */
30
class ConfirmationRequest extends \yii\db\ActiveRecord
31
{
32
33
    protected $delivery = 'display';
34
35
    protected $secondFactor = 'email';
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public static function tableName()
41
    {
42
        return '{{%confirmation_request}}';
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function rules()
49
    {
50
        return [
51
            [['object_id'], 'integer'],
52
            [['object', 'values'], 'string'],
53
            [['model', 'release_token'], 'string', 'max' => 255],
54
            [['release_token'], 'default', 'value' => function($model, $attribute) { return $this->generateReleaseToken(); }],
55
            [['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => $this->getUserClassName(), 'targetAttribute' => ['created_by' => 'id']],
56
            [['updated_by'], 'exist', 'skipOnError' => true, 'targetClass' => $this->getUserClassName(), 'targetAttribute' => ['updated_by' => 'id']],
57
        ];
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function attributeLabels()
64
    {
65
        return [
66
            'id' => Yii::t('app', 'ID'),
67
            'model' => Yii::t('app', 'Model'),
68
            'object_id' => Yii::t('app', 'Object'),
69
            'object' => Yii::t('app', 'Object'),
70
            'release_token' => Yii::t('app', 'Release Token'),
71
            'created_at' => Yii::t('app', 'Created At'),
72
            'updated_at' => Yii::t('app', 'Updated At'),
73
            'values' => Yii::t('app', 'Values'),
74
            'created_by' => Yii::t('app', 'Created By'),
75
            'updated_by' => Yii::t('app', 'Updated By'),
76
        ];
77
    }
78
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function relations()
84
    {
85
        return [
86
            'CreatedBy' => 'one',
87
            'UpdatedBy' => 'one',
88
        ];
89
    }
90
91
    public function behaviors() {
92
93
        return ArrayHelper::merge(parent::behaviors(),
94
            [
95
                TimestampBehavior::className(),
96
                BlameableBehavior::className(),
97
            ]);
98
    }
99
100
    public function getViewLink() {
101
        return Url::to(['@web/confirmation-requests', 'release_token' => $this->release_token], true);
102
    }
103
104
    /**
105
     * Generates new password reset token
106
     */
107
    public function generateReleaseToken()
108
    {
109
        return Yii::$app->security->generateRandomString() . '_' . time();
110
    }
111
112
113
    /**
114
     * @return \yii\db\ActiveQuery
115
     */
116
    public function getCreatedBy()
117
    {
118
        return $this->hasOne($this->getUserClassName(), ['id' => 'created_by']);
119
    }
120
121
    /**
122
     * @return \yii\db\ActiveQuery
123
     */
124
    public function getUpdatedBy()
125
    {
126
        return $this->hasOne($this->getUserClassName(), ['id' => 'updated_by']);
127
    }
128
129
    /**
130
     * @return string ActiveRecord user class, as per application implementation
131
     */
132
    protected function getUserClassName() {
133
        return Yii::$app->user->identityClass;
134
    }
135
136
    public function release() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
137
138
        $model          = $this->constructObject();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 10 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
139
        $changedValues  = $model->getChangedValues();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
140
        $current        = clone $model;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 8 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
141
        $current->refresh();
142
143
        foreach ($changedValues as $field => $value) {
144
            $oldValue = $model->oldAttributes[$field];
145
            if ($current->$field !== $oldValue && $current->$field !== $value) {
146
                throw new ErrorException(
147
                    sprintf('Unable to release change, protected field %s has been updated since this request.'
148
                    . ' Expected to find %s or %s, found %s', $field, $value, $oldValue, $current->$field));
149
            }
150
151
        }
152
153
        $model->releaseToken = $this->release_token;
154
        return $model->save();
155
156
    }
157
158
159
    /**
160
     * @return \enigmatix\core\Model
161
     */
162
    public function constructObject() {
163
        return unserialize($this->object);
164
    }
165
166
167
}
168