VoteForm::checkModel()   B
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 29
rs 8.0555
c 0
b 0
f 0
cc 9
nc 6
nop 0
1
<?php
2
3
namespace hauntd\vote\models;
4
5
use Yii;
6
use hauntd\vote\Module;
7
use hauntd\vote\traits\ModuleTrait;
8
use yii\base\Model;
9
use yii\helpers\ArrayHelper;
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. "userLike" or "pageVoting")
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
     */
41
    public function rules()
42
    {
43
        return [
44
            [['entity', 'targetId', 'action'], 'required'],
45
            ['targetId', 'integer'],
46
            ['action', 'in', 'range' => [self::ACTION_NEGATIVE, self::ACTION_POSITIVE, self::ACTION_TOGGLE]],
47
            ['entity', 'checkModel'],
48
        ];
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function getValue()
55
    {
56
        return $this->action == self::ACTION_NEGATIVE ? Vote::VOTE_NEGATIVE : Vote::VOTE_POSITIVE;
57
    }
58
59
    /**
60
     * @return bool
61
     * @throws \yii\base\InvalidConfigException
62
     */
63
    public function checkModel()
64
    {
65
        $module = $this->getModule();
66
        $settings = $module->getSettingsForEntity($this->entity);
67
        if ($settings === null) {
68
            $this->addError('entity', Yii::t('vote', 'This entity is not supported.'));
69
            return false;
70
        }
71
        $allowGuests = ArrayHelper::getValue($settings, 'allowGuests', false);
72
        if (Yii::$app->user->isGuest && ($settings['type'] == Module::TYPE_TOGGLE || !$allowGuests)) {
73
            $this->addError('entity', Yii::t('vote', 'Guests are not allowed for this voting.'));
74
            return false;
75
        }
76
        $targetModel = Yii::createObject($settings['modelName']);
77
        $entityModel = $targetModel->findOne([$targetModel::primaryKey()[0] => $this->targetId]);
78
        if ($entityModel == null) {
79
            $this->addError('targetId', Yii::t('vote', 'Target model not found.'));
80
            return false;
81
        }
82
        $allowSelfVote = ArrayHelper::getValue($settings, 'allowSelfVote', false);
83
        if (!$allowSelfVote) {
84
            $entityAuthorAttribute = ArrayHelper::getValue($settings, 'entityAuthorAttribute', 'user_id');
85
            if (!Yii::$app->user->isGuest && Yii::$app->user->id == $entityModel->{$entityAuthorAttribute}) {
86
                $this->addError('entity', Yii::t('vote', 'Self-voting are not allowed.'));
87
                return false;
88
            }
89
        }
90
91
        return true;
92
    }
93
}
94