Completed
Push — master ( 61b824...9bd84d )
by Aleksandr
04:57
created

ActiveFormBehavior::events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 6
c 2
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
4
namespace carono\checksum;
5
6
7
use yii\base\Behavior;
8
use yii\base\Widget;
9
use yii\helpers\Html;
10
use yii\widgets\ActiveForm;
11
12
/**
13
 * Class ActiveFormBehavior
14
 *
15
 * @package carono\checksum
16
 * @property ActiveForm $owner
17
 */
18
class ActiveFormBehavior extends Behavior
19
{
20
    public $_checksumInit = true;
21
22
    public function events()
23
    {
24
        return [
25
            Widget::EVENT_BEFORE_RUN => 'registerChecksumField'
26
        ];
27
    }
28
29
    public function registerChecksumField()
30
    {
31
        if (strtolower($this->owner->method) == 'post' && \Yii::$app->request instanceof Request && \Yii::$app->request->checksumIsEnabled()) {
32
            $stack = \Yii::$app->request->getStack($this->owner->id);
33
            $key = Checksum::calculate($stack, \Yii::$app->request->checksumKey);
34
            \Yii::$app->request->stackField($this->owner->id, \Yii::$app->request->checksumParam, $key);
35
            echo Html::hiddenInput(\Yii::$app->request->checksumParam, $key);
36
        }
37
    }
38
}