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

DataSession   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setPasswordForShare() 0 3 1
A getValue() 0 7 2
A getPasswordForShare() 0 3 1
A __construct() 0 3 1
A removePasswordForShare() 0 3 1
A removeValue() 0 14 3
A getValues() 0 11 3
A setValue() 0 13 3
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
}