Passed
Push — master ( ab5002...bcfa32 )
by Alexander
02:25
created

BaseWidget::initJsEvents()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 6
nc 4
nop 0
1
<?php
2
3
namespace hauntd\vote\widgets;
4
5
use hauntd\vote\assets\VoteAsset;
6
use hauntd\vote\models\VoteAggregate;
7
use hauntd\vote\traits\ModuleTrait;
8
use yii\base\InvalidParamException;
9
use yii\base\Widget;
10
use Yii;
11
12
/**
13
 * @author Alexander Kononenko <[email protected]>
14
 * @package hauntd\vote\widgets
15
 */
16
abstract class BaseWidget extends Widget
17
{
18
    use ModuleTrait;
19
20
    /**
21
     * @var string
22
     */
23
    public $entity;
24
25
    /**
26
     * @var null|\yii\db\ActiveRecord
27
     */
28
    public $model;
29
30
    /**
31
     * @var null|integer;
32
     */
33
    public $targetId;
34
35
    /**
36
     * @var string
37
     */
38
    public $voteUrl;
39
40
    /**
41
     * @var null|\hauntd\vote\models\VoteAggregate
42
     */
43
    public $aggregateModel;
44
45
    /**
46
     * @var null|integer
47
     */
48
    public $userValue = null;
49
50
    /**
51
     * @var string
52
     */
53
    public $jsBeforeVote;
54
55
    /**
56
     * @var string
57
     */
58
    public $jsAfterVote;
59
60
    /**
61
     * @var string
62
     */
63
    public $jsCodeKey = 'vote';
64
65
    /**
66
     * @var string
67
     */
68
    public $jsErrorVote;
69
70
    /**
71
     * @var string
72
     */
73
    public $jsShowMessage;
74
75
    /**
76
     * @var string
77
     */
78
    public $jsChangeCounters;
79
80
    /**
81
     * @var array
82
     */
83
    public $options = [];
84
85
    /**
86
     * @var string
87
     */
88
    public $viewFile = 'vote';
89
90
    /**
91
     * @param $classes
92
     * @return string
93
     */
94
    public function getSelector($classes)
95
    {
96
        $classes = str_replace(' ', '.', $classes);
97
        return ".{$classes}[data-entity=\"' + entity + '\"][data-target-id=\"' + target  + '\"]";
98
    }
99
100
    /**
101
     * @inheritdoc
102
     * @throws \yii\base\InvalidConfigException
103
     */
104
    public function init()
105
    {
106
        parent::init();
107
108
        if (!isset($this->entity) || !isset($this->model)) {
109
            throw new InvalidParamException(Yii::t('vote', 'Entity and model must be set.'));
110
        }
111
112
        $this->initDefaults();
113
114
        if ($this->getModule()->registerAsset) {
115
            $this->view->registerAssetBundle(VoteAsset::class);
116
        }
117
    }
118
119
    /**
120
     * Initialize widget with default options.
121
     *
122
     * @throws \yii\base\InvalidConfigException
123
     */
124
    public function initDefaults()
125
    {
126
        if (!isset($this->voteUrl)) {
127
            $this->voteUrl = Yii::$app->getUrlManager()->createUrl(['vote/default/vote']);
128
        }
129
        if (!isset($this->targetId)) {
130
            $this->targetId = $this->model->getPrimaryKey();
131
        }
132
        if (!isset($this->aggregateModel)) {
133
            $this->aggregateModel = VoteAggregate::findOne([
1 ignored issue
show
Documentation Bug introduced by
It seems like \hauntd\vote\models\Vote...d' => $this->targetId)) of type array or boolean is incompatible with the declared type null|object<hauntd\vote\models\VoteAggregate> of property $aggregateModel.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
134
                'entity' => $this->getModule()->encodeEntity($this->entity),
135
                'target_id' => $this->targetId,
136
            ]);
137
        }
138
    }
139
140
    /**
141
     * Initialize default JS events.
142
     */
143
    public function initJsEvents()
144
    {
145
        $selector = $this->getSelector($this->options['class']);
0 ignored issues
show
Unused Code introduced by
$selector is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
146
        if (!isset($this->jsShowMessage)) {
147
            $this->jsShowMessage = "
148
                /** todo **/
149
            ";
150
        }
151
        if (!isset($this->jsErrorVote)) {
152
            $this->jsErrorVote = "
153
                /** todo **/
154
            ";
155
        }
156
    }
157
}
158