Completed
Push — master ( d627d1...90752c )
by Joel
01:58
created

ConfirmationRequest.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 custom logic model class for table "{{%confirmation_requests}}".
15
 * @inheritdoc
16
 */
17
class ConfirmationRequest extends \yii\db\ActiveRecord
18
{
19
20
    protected $delivery = 'display';
21
22
    protected $secondFactor = 'email';
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public static function tableName()
28
    {
29
        return '{{%confirmation_request}}';
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function rules()
36
    {
37
        return [
38
            [['object_id'], 'integer'],
39
            [['object', 'values'], 'string'],
40
            [['model', 'release_token'], 'string', 'max' => 255],
41
            [['release_token'], 'default',  'value' => function ($model, $attribute){ return $this->generateReleaseToken();}],
2 ignored issues
show
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
            [['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => $this->getUserClassName(), 'targetAttribute' => ['created_by' => 'id']],
43
            [['updated_by'], 'exist', 'skipOnError' => true, 'targetClass' => $this->getUserClassName(), 'targetAttribute' => ['updated_by' => 'id']],
44
        ];
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function attributeLabels()
51
    {
52
        return [
53
            'id' => Yii::t('app', 'ID'),
54
            'model' => Yii::t('app', 'Model'),
55
            'object_id' => Yii::t('app', 'Object'),
56
            'object' => Yii::t('app', 'Object'),
57
            'release_token' => Yii::t('app', 'Release Token'),
58
            'created_at' => Yii::t('app', 'Created At'),
59
            'updated_at' => Yii::t('app', 'Updated At'),
60
            'values' => Yii::t('app', 'Values'),
61
            'created_by' => Yii::t('app', 'Created By'),
62
            'updated_by' => Yii::t('app', 'Updated By'),
63
        ];
64
    }
65
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function relations()
71
    {
72
        return [
73
            'CreatedBy' => 'one',
74
            'UpdatedBy' => 'one',
75
        ];
76
    }
77
78
    public function behaviors() {
79
80
        return ArrayHelper::merge(parent::behaviors(),
81
            [
82
                TimestampBehavior::className(),
83
                BlameableBehavior::className(),
84
            ]);
85
    }
86
87
    public function getViewLink(){
88
        return Url::to(['@web/confirmation-requests', 'release_token' => $this->release_token],true);
0 ignored issues
show
The property release_token does not exist on object<enigmatix\confirm...on\ConfirmationRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
89
    }
90
91
    /**
92
     * Generates new password reset token
93
     */
94
    public function generateReleaseToken()
95
    {
96
        return Yii::$app->security->generateRandomString() . '_' . time();
97
    }
98
99
100
    /**
101
     * @return \yii\db\ActiveQuery
102
     */
103
    public function getCreatedBy()
104
    {
105
        return $this->hasOne($this->getUserClassName(), ['id' => 'created_by']);
106
    }
107
108
    /**
109
     * @return \yii\db\ActiveQuery
110
     */
111
    public function getUpdatedBy()
112
    {
113
        return $this->hasOne($this->getUserClassName(), ['id' => 'updated_by']);
114
    }
115
116
    protected function getUserClassName(){
0 ignored issues
show
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...
117
        return Yii::$app->user->identityClass;
118
    }
119
120
    public function release(){
121
122
        $model          = $this->constructObject();
123
        $changedValues  = $model->getChangedValues();
124
        $current        = clone $model;
125
        $current->refresh();
126
127
        foreach ($changedValues as $field => $value){
128
            $oldValue = $model->oldAttributes[$field];
129
            if($current->$field !== $oldValue && $current->$field !== $value)
130
                throw new ErrorException("Unable to release change, protected field $field has been updated since this request."
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $field instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
131
                . " Expected to find $value or $oldValue, found " . $current->$field);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $value instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $oldValue instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
132
133
        }
134
135
        $model->releaseToken = $this->release_token;
0 ignored issues
show
The property release_token does not exist on object<enigmatix\confirm...on\ConfirmationRequest>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
136
        return $model->save();
137
138
    }
139
140
141
    /**
142
     * @return \enigmatix\core\Model
143
     */
144
    public function constructObject(){
145
        return unserialize($this->object);
0 ignored issues
show
The property object does not exist on object<enigmatix\confirm...on\ConfirmationRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
146
    }
147
148
149
}
150