1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Repository.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:WebSocketSession! |
9
|
|
|
* @subpackage Users |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 14.02.17 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\WebSocketsSession\Users; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
|
21
|
|
|
use IPub; |
22
|
|
|
use IPub\WebSockets\Clients as WebSocketsClients; |
23
|
|
|
use IPub\WebSockets\Entities as WebSocketsEntities; |
24
|
|
|
use IPub\WebSockets\Exceptions as WebSocketsExceptions; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Connected users repository |
28
|
|
|
* |
29
|
|
|
* @package iPublikuj:WebSocketSession! |
30
|
|
|
* @subpackage Users |
31
|
|
|
* |
32
|
|
|
* @author Adam Kadlec <[email protected]> |
33
|
|
|
*/ |
34
|
1 |
|
final class Repository implements IRepository |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Implement nette smart magic |
38
|
|
|
*/ |
39
|
1 |
|
use Nette\SmartObject; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var WebSocketsClients\IStorage |
43
|
|
|
*/ |
44
|
|
|
private $storage; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param WebSocketsClients\IStorage $storage |
48
|
|
|
*/ |
49
|
|
|
public function __construct(WebSocketsClients\IStorage $storage) |
50
|
|
|
{ |
51
|
1 |
|
$this->storage = $storage; |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getUser(WebSocketsEntities\Clients\IClient $client) |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
return $client->getUser(); |
61
|
|
|
|
62
|
|
|
} catch (WebSocketsExceptions\ClientNotFoundException $ex) { |
63
|
|
|
return NULL; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $username |
69
|
|
|
* |
70
|
|
|
* @return array|bool |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function findByUsername(string $username) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
/** @var WebSocketsEntities\Clients\IClient $client */ |
75
|
|
|
foreach ($this->storage as $client) { |
76
|
|
|
$user = $client->getUser(); |
77
|
|
|
|
78
|
|
|
if (!$user || !$user->isLoggedIn()) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (method_exists($user, 'getUsername') && $user->getUsername() === $username) { |
83
|
|
|
return [ |
84
|
|
|
'user' => $client, |
85
|
|
|
'client' => $client |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return FALSE; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $userId |
95
|
|
|
* |
96
|
|
|
* @return array|bool |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function findById($userId) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
/** @var WebSocketsEntities\Clients\IClient $client */ |
101
|
|
|
foreach ($this->storage as $client) { |
102
|
|
|
$user = $client->getUser(); |
103
|
|
|
|
104
|
|
|
if (!$user || !$user->isLoggedIn()) { |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($user->getId() === $userId) { |
109
|
|
|
return [ |
110
|
|
|
'user' => $client, |
111
|
|
|
'client' => $client |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return FALSE; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param bool $anonymous |
121
|
|
|
* |
122
|
|
|
* @return array|bool |
123
|
|
|
*/ |
124
|
|
|
public function findAll(bool $anonymous = FALSE) |
125
|
|
|
{ |
126
|
|
|
$results = []; |
127
|
|
|
|
128
|
|
|
/** @var WebSocketsEntities\Clients\IClient $client */ |
129
|
|
|
foreach ($this->storage as $client) { |
130
|
|
|
$user = $client->getUser(); |
131
|
|
|
|
132
|
|
|
if ($anonymous !== TRUE && (!$user->isLoggedIn() || $user === NULL)) { |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$results[] = [ |
137
|
|
|
'user' => $user, |
138
|
|
|
'client' => $client, |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return empty($results) ? FALSE : $results; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $roles |
147
|
|
|
* |
148
|
|
|
* @return array|bool |
149
|
|
|
*/ |
150
|
|
|
public function findByRoles(array $roles) |
151
|
|
|
{ |
152
|
|
|
$results = []; |
153
|
|
|
|
154
|
|
|
/** @var WebSocketsEntities\Clients\IClient $client */ |
155
|
|
|
foreach ($this->storage as $client) { |
156
|
|
|
$user = $client->getUser(); |
157
|
|
|
|
158
|
|
|
foreach ($user->getRoles() as $role) { |
159
|
|
|
if (in_array($role, $roles)) { |
160
|
|
|
$results[] = [ |
161
|
|
|
'user' => $user, |
162
|
|
|
'client' => $client, |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
continue 1; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return empty($results) ? FALSE : $results; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.