Passed
Push — master ( 43f31d...90d4a6 )
by Alexander
02:57
created

VoteForm::checkModel()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 6
eloc 15
nc 4
nop 0
1
<?php
2
3
namespace hauntd\vote\models;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\helpers\ArrayHelper;
8
use hauntd\vote\traits\ModuleTrait;
9
use hauntd\vote\Module;
10
11
/**
12
 * @author Alexander Kononenko <[email protected]>
13
 * @package hauntd\vote\models
14
 */
15
class VoteForm extends Model
16
{
17
    use ModuleTrait;
18
19
    const ACTION_POSITIVE = 'positive';
20
    const ACTION_NEGATIVE = 'negative';
21
    const ACTION_TOGGLE = 'toggle';
22
23
    /**
24
     * @var string entity (e.g. "user.like" or "page.voting")
25
     */
26
    public $entity;
27
28
    /**
29
     * @var integer target model id
30
     */
31
    public $targetId;
32
33
    /**
34
     * @var string +/-?
35
     */
36
    public $action;
37
38
    /**
39
     * @return array
40
     * @throws \yii\base\InvalidConfigException
41
     */
42
    public function rules()
43
    {
44
        return [
45
            [['entity', 'targetId', 'action'], 'required'],
46
            ['targetId', 'integer'],
47
            ['action', 'in', 'range' => [self::ACTION_NEGATIVE, self::ACTION_POSITIVE, self::ACTION_TOGGLE]],
48
            ['entity', 'checkModel'],
49
        ];
50
    }
51
52
    /**
53
     * @return int
54
     */
55
    public function getValue()
56
    {
57
        return $this->action == self::ACTION_NEGATIVE ? Vote::VOTE_NEGATIVE : Vote::VOTE_POSITIVE;
58
    }
59
60
    /**
61
     * @return bool
62
     * @throws \yii\base\InvalidConfigException
63
     */
64
    public function checkModel()
65
    {
66
        $module = $this->getModule();
67
        $settings = $module->getSettingsForEntity($this->entity);
68
        $allowGuests = ArrayHelper::getValue($settings, 'allowGuests', false);
0 ignored issues
show
Bug introduced by
It seems like $settings defined by $module->getSettingsForEntity($this->entity) on line 67 can also be of type null; however, yii\helpers\BaseArrayHelper::getValue() does only seem to accept array|object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69
70
        if (!$settings) {
71
            $this->addError('entity', Yii::t('vote', 'This entity is not supported.'));
72
            return false;
73
        }
74
        if (Yii::$app->user->isGuest && ($settings['type'] == Module::TYPE_TOGGLE || !$allowGuests)) {
75
            $this->addError('entity', Yii::t('vote', 'Guests are not allowed for this voting.'));
76
            return false;
77
        }
78
        $targetModel = Yii::createObject($settings['modelName']);
79
        if ($targetModel->findOne(['id' => $this->targetId]) == null) {
80
            $this->addError('targetId', Yii::t('vote', 'Target model not found.'));
81
            return false;
82
        }
83
84
        return true;
85
    }
86
}
87