Passed
Branch master (7b1276)
by Marcel
06:24
created

DataSession::getValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Data Analytics
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the LICENSE.md file.
8
 *
9
 * @author Marcel Scherello <[email protected]>
10
 * @copyright 2019 Marcel Scherello
11
 */
12
13
namespace OCA\Analytics;
14
15
use OCP\ISession;
16
17
class DataSession
18
{
19
    /** @var ISession */
20
    protected $session;
21
22
    public function __construct(ISession $session)
23
    {
24
        $this->session = $session;
25
    }
26
27
    public function getPasswordForShare(string $token): ?string
28
    {
29
        return $this->getValue('data-password', $token);
30
    }
31
32
    protected function getValue(string $key, string $token): ?string
33
    {
34
        $values = $this->getValues($key);
35
        if (!isset($values[$token])) {
36
            return null;
37
        }
38
        return $values[$token];
39
    }
40
41
    protected function getValues(string $key): array
42
    {
43
        $values = $this->session->get($key);
44
        if ($values === null) {
45
            return [];
46
        }
47
        $values = json_decode($values, true);
48
        if ($values === null) {
49
            return [];
50
        }
51
        return $values;
52
    }
53
54
    public function setPasswordForShare(string $token, string $password): void
55
    {
56
        $this->setValue('data-password', $token, $password);
57
    }
58
59
    protected function setValue(string $key, string $token, string $value): void
60
    {
61
        $values = $this->session->get($key);
62
        if ($values === null) {
63
            $values = [];
64
        } else {
65
            $values = json_decode($values, true);
66
            if ($values === null) {
67
                $values = [];
68
            }
69
        }
70
        $values[$token] = $value;
71
        $this->session->set($key, json_encode($values));
72
    }
73
74
    public function removePasswordForShare(string $token): void
75
    {
76
        $this->removeValue('data-password', $token);
77
    }
78
79
    protected function removeValue(string $key, string $token): void
80
    {
81
        $values = $this->session->get($key);
82
        if ($values === null) {
83
            $values = [];
84
        } else {
85
            $values = json_decode($values, true);
86
            if ($values === null) {
87
                $values = [];
88
            } else {
89
                unset($values[$token]);
90
            }
91
        }
92
        $this->session->set($key, json_encode($values));
93
    }
94
}