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

ActiveFormBehavior   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 21
c 1
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 6 1
A registerChecksumField() 0 9 4
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
}