DataSession::getValues()   A
last analyzed

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
 * Analytics
5
 *
6
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\Analytics;
11
12
use OCP\ISession;
0 ignored issues
show
Bug introduced by
The type OCP\ISession was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class DataSession
15
{
16
    /** @var ISession */
17
    protected $session;
18
19
    public function __construct(ISession $session)
20
    {
21
        $this->session = $session;
22
    }
23
24
    public function getPasswordForShare(string $token): ?string
25
    {
26
        return $this->getValue('data-password', $token);
27
    }
28
29
    protected function getValue(string $key, string $token): ?string
30
    {
31
        $values = $this->getValues($key);
32
        if (!isset($values[$token])) {
33
            return null;
34
        }
35
        return $values[$token];
36
    }
37
38
    protected function getValues(string $key): array
39
    {
40
        $values = $this->session->get($key);
41
        if ($values === null) {
42
            return [];
43
        }
44
        $values = json_decode($values, true);
45
        if ($values === null) {
46
            return [];
47
        }
48
        return $values;
49
    }
50
51
    public function setPasswordForShare(string $token, string $password): void
52
    {
53
        $this->setValue('data-password', $token, $password);
54
    }
55
56
    protected function setValue(string $key, string $token, string $value): void
57
    {
58
        $values = $this->session->get($key);
59
        if ($values === null) {
60
            $values = [];
61
        } else {
62
            $values = json_decode($values, true);
63
            if ($values === null) {
64
                $values = [];
65
            }
66
        }
67
        $values[$token] = $value;
68
        $this->session->set($key, json_encode($values));
69
    }
70
71
    public function removePasswordForShare(string $token): void
72
    {
73
        $this->removeValue('data-password', $token);
74
    }
75
76
    protected function removeValue(string $key, string $token): void
77
    {
78
        $values = $this->session->get($key);
79
        if ($values === null) {
80
            $values = [];
81
        } else {
82
            $values = json_decode($values, true);
83
            if ($values === null) {
84
                $values = [];
85
            } else {
86
                unset($values[$token]);
87
            }
88
        }
89
        $this->session->set($key, json_encode($values));
90
    }
91
}