generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
AssignmentsStorage.php
156 lines (137 loc) · 3.82 KB
/
AssignmentsStorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace Yiisoft\Rbac\Cycle;
use Cycle\Database\DatabaseInterface;
use Cycle\Database\DatabaseProviderInterface;
use Cycle\Database\Injection\Fragment;
use Cycle\Database\Table;
use Yiisoft\Rbac\Assignment;
use Yiisoft\Rbac\AssignmentsStorageInterface;
final class AssignmentsStorage implements AssignmentsStorageInterface
{
private DatabaseInterface $database;
/**
* @param non-empty-string $tableName
*/
public function __construct(/**
* @psalm-var non-empty-string
*/
private string $tableName,
DatabaseProviderInterface $dbal
) {
$this->database = $dbal->database();
}
/**
* @inheritDoc
*/
public function getAll(): array
{
$assignments = [];
foreach ($this->database->select()->from($this->tableName)->fetchAll() as $item) {
$assignments[$item['userId']][$item['itemName']] = new Assignment(
$item['userId'],
$item['itemName'],
(int)$item['createdAt']
);
}
return $assignments;
}
/**
* @inheritDoc
*/
public function getByUserId(string $userId): array
{
$assignments = $this->database->select()->from($this->tableName)->where(['userId' => $userId])->fetchAll();
return array_combine(
array_column($assignments, 'itemName'),
array_map(
static fn (array $item) => new Assignment($userId, $item['itemName'], (int)$item['createdAt']),
$assignments
)
);
}
/**
* @inheritDoc
*/
public function get(string $itemName, string $userId): ?Assignment
{
$assignment = $this->database
->select()
->from($this->tableName)
->where(['itemName' => $itemName, 'userId' => $userId])
->run()
->fetch();
return empty($assignment) ? null : new Assignment($userId, $itemName, (int)$assignment['createdAt']);
}
/**
* @inheritDoc
*/
public function add(string $itemName, string $userId): void
{
$this->database
->insert($this->tableName)
->values(
[
'itemName' => $itemName,
'userId' => $userId,
'createdAt' => time(),
],
)
->run();
}
/**
* @inheritDoc
*/
public function hasItem(string $name): bool
{
$result = $this
->database
->select([new Fragment('1')])
->from($this->tableName)
->where(['itemName' => $name])
->limit(1)
->run()
->fetch();
return $result !== false;
}
/**
* @inheritDoc
*/
public function renameItem(string $oldName, string $newName): void
{
if ($oldName === $newName) {
return;
}
$this->database->update($this->tableName, ['itemName' => $newName], ['itemName' => $oldName])->run();
}
/**
* @inheritDoc
*/
public function remove(string $itemName, string $userId): void
{
$this->database->delete($this->tableName, ['itemName' => $itemName, 'userId' => $userId])->run();
}
/**
* @inheritDoc
*/
public function removeByUserId(string $userId): void
{
$this->database->delete($this->tableName, ['userId' => $userId])->run();
}
/**
* @inheritDoc
*/
public function removeByItemName(string $itemName): void
{
$this->database->delete($this->tableName, ['itemName' => $itemName])->run();
}
/**
* @inheritDoc
*/
public function clear(): void
{
/** @var Table $table */
$table = $this->database->table($this->tableName);
$table->eraseData();
}
}