-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionStorage.php
More file actions
88 lines (72 loc) · 2.16 KB
/
ConnectionStorage.php
File metadata and controls
88 lines (72 loc) · 2.16 KB
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
<?php
declare(strict_types=1);
namespace Octamp\Server\Connection;
use Octamp\Server\Adapter\AdapterInterface;
use Octamp\Server\Server;
class ConnectionStorage
{
private ConnectionRepository $connectionRepository;
/**
* @var Connection[]
*/
private array $connections = [];
public function __construct(private Server $server, private AdapterInterface $adapter)
{
$this->connectionRepository = new ConnectionRepository($this->server, $this->adapter);
}
public function save(Connection $connection): void
{
$this->connections[$connection->getId()] = $connection;
$this->connectionRepository->saveConnection($connection);
}
public function remove(Connection $connection): void
{
$connection = $this->connections[$connection->getId()];
$this->connectionRepository->removeConnection($connection->getId());
unset($this->connections[$connection->getId()]);
}
public function getUsingServerFd(string $serverId, int $fd): ?Connection
{
$id = Connection::generateId($serverId, $fd);
return $this->get($id);
}
public function get(string $id): ?Connection
{
if ($this->isLocal($id)) {
return $this->connections[$id];
}
try {
return $this->connectionRepository->getConnection($id);
} catch (\Exception $e) {
return null;
}
}
/**
* @return array<string, array{serverId: string, fd: int}>
*/
public function allIds(): array
{
return $this->connectionRepository->getAllIdParted();
}
/**
* @return Connection[]
*/
public function getAll(): array
{
$raw = $this->connectionRepository->allRaw();
$results = [];
foreach ($raw as $value) {
try {
$result = Connection::createFromArray($value, $this->server);
$results[] = $result;
} catch (\Exception $exception) {
// don nothing
}
}
return $results;
}
public function isLocal(string $id): bool
{
return isset($this->connections[$id]);
}
}