Passed
Push — master ( e89a22...38e786 )
by Petr
08:13
created

MultiSend::addRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_forms\Controls\Security;
4
5
6
use ArrayAccess;
7
use kalanis\kw_forms\Controls\Hidden;
8
use kalanis\kw_rules\Interfaces\IRules;
9
10
11
/**
12
 * Class MultiSend
13
 * @package kalanis\kw_forms\Controls\Security
14
 * Hidden entry which adds Multisend check
15
 * Must be child of hidden due necessity of pre-setting position in render
16
 * This one did not set another value to compare, on the other way csrf sets new value
17
 */
18
class MultiSend extends Hidden
19
{
20
    /** @var ArrayAccess */
21
    protected $cookie = null;
22
23 2
    public function setHidden(string $alias, ArrayAccess &$cookie, string $errorMessage): self
24
    {
25 2
        $this->cookie = $cookie;
26 2
        $this->setEntry($alias);
27 2
        $this->setValue(uniqid('multisend', true));
28 2
        $this->addCheckToStack(strval($this->getValue()));
29 2
        parent::addRule(IRules::SATISFIES_CALLBACK, $errorMessage, [$this, 'checkMulti']);
30 2
        return $this;
31
    }
32
33
    /**
34
     * @param string|int|float $incomingValue
35
     * @return bool
36
     */
37 1
    public function checkMulti($incomingValue): bool
38
    {
39 1
        return $this->removeExistingCheckFromStack(strval($incomingValue));
40
    }
41
42 2
    protected function addCheckToStack(string $value): void
43
    {
44 2
        $hashStack = $this->hashStack();
45 2
        $hashStack[$value] = 'FORM_SENDED';
46 2
        $this->cookie->offsetSet($this->getKey() . 'SubmitCheck', json_encode($hashStack));
47 2
    }
48
49 1
    protected function removeExistingCheckFromStack(string $value): bool
50
    {
51 1
        $hashStack = $this->hashStack();
52 1
        if (isset($hashStack[$value])) {
53 1
            unset($hashStack[$value]);
54 1
            $this->cookie->offsetSet($this->getKey() . 'SubmitCheck', json_encode($hashStack));
55 1
            return true;
56
        }
57 1
        return false;
58
    }
59
60
    /**
61
     * @return array<string, string>
62
     */
63 2
    protected function hashStack(): array
64
    {
65 2
        $hashStack = $this->cookie->offsetExists($this->getKey() . 'SubmitCheck')
66 1
            ? (array) json_decode($this->cookie->offsetGet($this->getKey() . 'SubmitCheck'), true)
67 2
            : null ;
68 2
        if (is_null($hashStack)) {
69 2
            $hashStack = [];
70
        }
71 2
        return $hashStack;
72
    }
73
74 1
    public function addRule(/** @scrutinizer ignore-unused */ string $ruleName, /** @scrutinizer ignore-unused */ string $errorText, /** @scrutinizer ignore-unused */ ...$args): void
75
    {
76
        // no additional rules applicable
77 1
    }
78
79 1
    public function addRules(/** @scrutinizer ignore-unused */ iterable $rules = []): void
80
    {
81
        // no rules add applicable
82 1
    }
83
84 1
    public function removeRules(): void
85
    {
86
        // no rules removal applicable
87 1
    }
88
89 1
    public function renderErrors($errors): string
90
    {
91 1
        return '';
92
    }
93
}
94