Passed
Pull Request — 2.x (#1431)
by Harings
07:43
created

TwillUtil::pushToTempStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 10
rs 10
1
<?php
2
3
namespace A17\Twill;
4
5
use Illuminate\Support\Facades\Session;
6
7
/**
8
 * @todo: This could "pile up" over time. Maybe we can clear it when a new form is being built.
9
 */
10
class TwillUtil
11
{
12
    private const SESSION_FIELD = 'twill_util';
13
    private const REPEATER_ID_INDEX = 'repeater_ids';
14
15
    public function hasRepeaterIdFor(int $frontEndId): ?int {
16
        return $this->getFromTempStore(self::REPEATER_ID_INDEX, $frontEndId);
17
    }
18
19
    public function registerRepeaterId(int $frontEndId, int $dbId): self
20
    {
21
        $this->pushToTempStore(self::REPEATER_ID_INDEX, $frontEndId, $dbId);
22
23
        return $this;
24
    }
25
26
    private function getFromTempStore(string $key, int $frontendId): ?int {
27
        $data = Session::get(self::SESSION_FIELD . '.' . $key, []);
28
29
        return $data[$frontendId] ?? null;
30
    }
31
32
    private function pushToTempStore(string $key, int $frontendId, int $dbId): void
33
    {
34
        $sessionData = Session::get(self::SESSION_FIELD . '.' . $key, []);
35
36
        $keyData = $sessionData[$key] ?? [];
37
        $keyData[$frontendId] = $dbId;
38
39
        $sessionData[$key] = $keyData;
40
41
        Session::put(self::SESSION_FIELD, $sessionData);
42
    }
43
}
44