1 | <?php |
||
32 | final class Client |
||
33 | { |
||
34 | private $handler; |
||
35 | private $spaces = []; |
||
36 | |||
37 | 52 | public function __construct(Handler $handler) |
|
41 | |||
42 | public static function fromDefaults() : self |
||
49 | |||
50 | public static function fromOptions(array $options) : self |
||
51 | { |
||
52 | $connectionOptions = []; |
||
53 | |||
54 | if (isset($options['connect_timeout'])) { |
||
55 | $connectionOptions['connect_timeout'] = $options['connect_timeout']; |
||
56 | } |
||
57 | if (isset($options['socket_timeout'])) { |
||
58 | $connectionOptions['socket_timeout'] = $options['socket_timeout']; |
||
59 | } |
||
60 | if (isset($options['tcp_nodelay'])) { |
||
61 | $connectionOptions['tcp_nodelay'] = $options['tcp_nodelay']; |
||
62 | } |
||
63 | |||
64 | $handler = new DefaultHandler( |
||
65 | new StreamConnection($options['uri'] ?? StreamConnection::DEFAULT_URI, $connectionOptions), |
||
66 | new PurePacker() |
||
67 | ); |
||
68 | |||
69 | if (isset($options['username'])) { |
||
70 | $handler = new MiddlewareHandler( |
||
71 | new AuthMiddleware($options['username'], $options['password'] ?? ''), |
||
72 | $handler |
||
73 | ); |
||
74 | } |
||
75 | if (isset($options['max_retries'])) { |
||
76 | $handler = new MiddlewareHandler( |
||
77 | RetryMiddleware::linear($options['max_retries']), |
||
78 | $handler |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | return new self($handler); |
||
83 | } |
||
84 | |||
85 | public static function fromDsn(string $dsn) : self |
||
99 | |||
100 | public function withMiddleware(Middleware $middleware, Middleware ...$middlewares) : self |
||
111 | |||
112 | 4 | public function getHandler() : Handler |
|
116 | |||
117 | 46 | public function ping() : void |
|
118 | { |
||
119 | 46 | $this->handler->handle(new Ping()); |
|
120 | } |
||
121 | |||
122 | 2 | public function getSpace(string $spaceName) : Space |
|
123 | { |
||
124 | 2 | if (isset($this->spaces[$spaceName])) { |
|
125 | return $this->spaces[$spaceName]; |
||
126 | } |
||
127 | |||
128 | 2 | $spaceId = $this->getSpaceIdByName($spaceName); |
|
129 | |||
130 | return $this->spaces[$spaceName] = $this->spaces[$spaceId] = new Space($this->handler, $spaceId); |
||
131 | } |
||
132 | |||
133 | 2 | public function getSpaceById(int $spaceId) : Space |
|
141 | |||
142 | public function call(string $funcName, ...$args) : array |
||
143 | { |
||
144 | $request = new Call($funcName, $args); |
||
145 | |||
146 | return $this->handler->handle($request)->getBodyField(IProto::DATA); |
||
147 | } |
||
148 | |||
149 | public function executeQuery(string $sql, array $params = []) : SqlQueryResult |
||
159 | |||
160 | public function executeUpdate(string $sql, array $params = []) : int |
||
166 | |||
167 | 2 | public function evaluate(string $expr, ...$args) : array |
|
173 | |||
174 | public function flushSpaces() : void |
||
175 | { |
||
176 | $this->spaces = []; |
||
177 | } |
||
178 | |||
179 | 2 | private function getSpaceIdByName(string $spaceName) : int |
|
190 | } |
||
191 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.