Vote   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 53
dl 0
loc 124
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 16 4
A behaviors() 0 7 1
A attributeLabels() 0 10 1
A getAggregate() 0 5 1
A updateRating() 0 24 3
A afterDelete() 0 4 1
A afterSave() 0 4 1
A tableName() 0 3 1
1
<?php
2
3
namespace hauntd\vote\models;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
8
/**
9
 * This is the model class for table "vote".
10
 *
11
 * @author Alexander Kononenko <[email protected]>
12
 * @package hauntd\vote\models
13
 * @property integer $id
14
 * @property integer $entity
15
 * @property integer $target_id
16
 * @property integer $user_id
17
 * @property string $user_ip
18
 * @property integer $value
19
 * @property integer $created_at
20
 *
21
 * @property \hauntd\vote\models\VoteAggregate $aggregate
22
 */
23
class Vote extends \yii\db\ActiveRecord
24
{
25
    const VOTE_POSITIVE = 1;
26
    const VOTE_NEGATIVE = 0;
27
28
    /**
29
     * @return string
30
     */
31
    public static function tableName()
32
    {
33
        return '{{%vote}}';
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function behaviors()
40
    {
41
        return [
42
            [
43
                'class' => TimestampBehavior::class,
44
                'createdAtAttribute' => 'created_at',
45
                'updatedAtAttribute' => false,
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function rules()
54
    {
55
        return [
56
            [['entity', 'target_id', 'value'], 'required'],
57
            [['entity', 'target_id', 'user_id', 'value', 'created_at'], 'integer'],
58
            [['user_ip'], 'default', 'value' => function () {
59
                if (Yii::$app instanceof \yii\web\Application) {
60
                    return Yii::$app->request->userIP;
61
                }
62
                return null;
63
            }],
64
            [['user_id'], 'default', 'value' => function () {
65
                if (isset(Yii::$app->user) && !Yii::$app->user->isGuest) {
66
                    return Yii::$app->user->id;
67
                }
68
                return null;
69
            }],
70
        ];
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function attributeLabels()
77
    {
78
        return [
79
            'id' => 'ID',
80
            'entity' => 'Entity',
81
            'target_id' => 'Target Model ID',
82
            'user_id' => 'User ID',
83
            'user_ip' => 'User Ip',
84
            'value' => 'Value',
85
            'created_at' => 'Created At',
86
        ];
87
    }
88
89
    /**
90
     * @return \yii\db\ActiveQuery
91
     */
92
    public function getAggregate()
93
    {
94
        return $this->hasOne(VoteAggregate::class, [
95
            'vote.entity' => 'vote_aggregate.entity',
96
            'vote.target_id' => 'vote_aggregate.target_id'
97
        ]);
98
    }
99
100
    /**
101
     * @param bool $insert
102
     * @param array $changedAttributes
103
     */
104
    public function afterSave($insert, $changedAttributes)
105
    {
106
        static::updateRating($this->attributes['entity'], $this->attributes['target_id']);
107
        parent::afterSave($insert, $changedAttributes);
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    public function afterDelete()
114
    {
115
        static::updateRating($this->attributes['entity'], $this->attributes['target_id']);
116
        parent::afterDelete();
117
    }
118
119
    /**
120
     * @param $entity
121
     * @param $targetId
122
     */
123
    public static function updateRating($entity, $targetId)
124
    {
125
        $positive = static::find()->where(['entity' => $entity, 'target_id' => $targetId, 'value' => self::VOTE_POSITIVE])->count();
126
        $negative = static::find()->where(['entity' => $entity, 'target_id' => $targetId, 'value' => self::VOTE_NEGATIVE])->count();
127
        if ($positive + $negative !== 0) {
128
            $rating = (($positive + 1.9208) / ($positive + $negative) - 1.96 * SQRT(($positive * $negative)
129
                        / ($positive + $negative) + 0.9604) / ($positive + $negative)) / (1 + 3.8416 / ($positive + $negative));
130
        } else {
131
            $rating = 0;
132
        }
133
        $rating = round($rating * 10, 2);
134
        $aggregateModel = VoteAggregate::findOne([
135
            'entity' => $entity,
136
            'target_id' => $targetId,
137
        ]);
138
        if ($aggregateModel == null) {
139
            $aggregateModel = new VoteAggregate();
140
            $aggregateModel->entity = $entity;
141
            $aggregateModel->target_id = $targetId;
142
        }
143
        $aggregateModel->positive = $positive;
0 ignored issues
show
Documentation Bug introduced by
It seems like $positive can also be of type string. However, the property $positive is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
144
        $aggregateModel->negative = $negative;
0 ignored issues
show
Documentation Bug introduced by
It seems like $negative can also be of type string. However, the property $negative is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
145
        $aggregateModel->rating = $rating;
146
        $aggregateModel->save();
147
    }
148
}
149