Store::getStatistics()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\consent;
6
7
use Exception;
8
use SimpleSAML\Module;
9
10
/**
11
 * Base class for consent storage handlers.
12
 *
13
 * @package SimpleSAMLphp
14
 */
15
16
abstract class Store
17
{
18
    /**
19
     * Constructor for the base class.
20
     *
21
     * This constructor should always be called first in any class which implements this class.
22
     *
23
     * @param array<mixed> &$config The configuration for this storage handler.
24
     */
25
    protected function __construct(
26
        protected array &$config,
27
    ) {
28
    }
29
30
31
    /**
32
     * Check for consent.
33
     *
34
     * This function checks whether a given user has authorized the release of the attributes identified by
35
     * $attributeSet from $source to $destination.
36
     *
37
     * @param string $userId        The hash identifying the user at an IdP.
38
     * @param string $destinationId A string which identifyes the destination.
39
     * @param string $attributeSet  A hash which identifies the attributes.
40
     *
41
     * @return bool True if the user has given consent earlier, false if not
42
     *              (or on error).
43
     */
44
    abstract public function hasConsent(string $userId, string $destinationId, string $attributeSet): bool;
45
46
47
    /**
48
     * Save consent.
49
     *
50
     * Called when the user asks for the consent to be saved. If consent information for the given user and destination
51
     * already exists, it should be overwritten.
52
     *
53
     * @param string $userId        The hash identifying the user at an IdP.
54
     * @param string $destinationId A string which identifyes the destination.
55
     * @param string $attributeSet  A hash which identifies the attributes.
56
     *
57
     * @return bool True if consent is succesfully saved otherwise false.
58
     */
59
    abstract public function saveConsent(string $userId, string $destinationId, string $attributeSet): bool;
60
61
62
    /**
63
     * Delete consent.
64
     *
65
     * Called when a user revokes consent for a given destination.
66
     *
67
     * @param string $userId        The hash identifying the user at an IdP.
68
     * @param string $destinationId A string which identifyes the destination.
69
     *
70
     * @return mixed Should be the number of consent deleted.
71
     */
72
    abstract public function deleteConsent(string $userId, string $destinationId);
73
74
75
    /**
76
     * Delete all consents.
77
     *
78
     * Called when a user revokes all consents
79
     *
80
     * @param string $userId The hash identifying the user at an IdP.
81
     *
82
     * @return mixed Should be the number of consent removed
83
     *
84
     * @throws \Exception
85
     */
86
    public function deleteAllConsents(string $userId)
0 ignored issues
show
Unused Code introduced by
The parameter $userId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
    public function deleteAllConsents(/** @scrutinizer ignore-unused */ string $userId)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        throw new Exception('Not implemented: deleteAllConsents()');
89
    }
90
91
92
    /**
93
     * Get statistics for all consent given in the consent store
94
     *
95
     * @return mixed Statistics from the consent store
96
     *
97
     * @throws \Exception
98
     */
99
    public function getStatistics()
100
    {
101
        throw new Exception('Not implemented: getStatistics()');
102
    }
103
104
105
    /**
106
     * Retrieve consents.
107
     *
108
     * This function should return a list of consents the user has saved.
109
     *
110
     * @param string $userId The hash identifying the user at an IdP.
111
     *
112
     * @return array<mixed> Array of all destination ids the user has given consent for.
113
     */
114
    abstract public function getConsents(string $userId): array;
115
116
117
    /**
118
     * Parse consent storage configuration.
119
     *
120
     * This function parses the configuration for a consent storage method. An exception will be thrown if
121
     * configuration parsing fails.
122
     *
123
     * @param mixed $config The configuration.
124
     *
125
     * @return \SimpleSAML\Module\consent\Store An object which implements the \SimpleSAML\Module\consent\Store class.
126
     *
127
     * @throws \Exception if the configuration is invalid.
128
     */
129
    public static function parseStoreConfig($config): Store
130
    {
131
        if (is_string($config)) {
132
            $config = [$config];
133
        }
134
135
        if (!is_array($config)) {
136
            throw new Exception('Invalid configuration for consent store option: ' . var_export($config, true));
137
        }
138
139
        if (!array_key_exists(0, $config)) {
140
            throw new Exception('Consent store without name given.');
141
        }
142
143
        $className = Module::resolveClass(
144
            $config[0],
145
            'Consent\Store',
146
            '\SimpleSAML\Module\consent\Store',
147
        );
148
149
        unset($config[0]);
150
        /**
151
         * @psalm-suppress InvalidStringClass
152
         * @var \SimpleSAML\Module\consent\Store $retval
153
         */
154
        $retval = new $className($config);
155
        return $retval;
156
    }
157
}
158