|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Neo4j PHP Client and Driver package. |
| 7 | + * |
| 8 | + * (c) Nagels <https://nagels.tech> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Laudis\Neo4j\TestkitBackend\Handlers; |
| 15 | + |
| 16 | +use Exception; |
| 17 | +use Laudis\Neo4j\Bolt\BoltDriver; |
| 18 | +use Laudis\Neo4j\Common\GeneratorHelper; |
| 19 | +use Laudis\Neo4j\Databags\Neo4jError; |
| 20 | +use Laudis\Neo4j\Databags\ServerInfo; |
| 21 | +use Laudis\Neo4j\Databags\SessionConfiguration; |
| 22 | +use Laudis\Neo4j\Exception\Neo4jException; |
| 23 | +use Laudis\Neo4j\Exception\TransactionException; |
| 24 | +use Laudis\Neo4j\Neo4j\Neo4jDriver; |
| 25 | +use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface; |
| 26 | +use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface; |
| 27 | +use Laudis\Neo4j\TestkitBackend\MainRepository; |
| 28 | +use Laudis\Neo4j\TestkitBackend\Requests\GetServerInfoRequest; |
| 29 | +use Laudis\Neo4j\TestkitBackend\Responses\DriverErrorResponse; |
| 30 | +use Laudis\Neo4j\TestkitBackend\Responses\ServerInfoResponse; |
| 31 | +use ReflectionClass; |
| 32 | +use Symfony\Component\Uid\Uuid; |
| 33 | + |
| 34 | +/** |
| 35 | + * @implements RequestHandlerInterface<GetServerInfoRequest> |
| 36 | + */ |
| 37 | +final class GetServerInfo implements RequestHandlerInterface |
| 38 | +{ |
| 39 | + public function __construct( |
| 40 | + private MainRepository $repository |
| 41 | + ) {} |
| 42 | + |
| 43 | + /** |
| 44 | + * @param GetServerInfoRequest $request |
| 45 | + */ |
| 46 | + public function handle($request): TestkitResponseInterface |
| 47 | + { |
| 48 | + try { |
| 49 | + $driver = $this->repository->getDriver($request->getDriverId()); |
| 50 | + |
| 51 | + if ($driver instanceof BoltDriver || $driver instanceof Neo4jDriver) { |
| 52 | + return $this->getServerInfoFromDriver($driver); |
| 53 | + } |
| 54 | + |
| 55 | + return $this->getServerInfoFromSession($driver); |
| 56 | + |
| 57 | + } catch (Exception $e) { |
| 58 | + $uuid = Uuid::v4(); |
| 59 | + |
| 60 | + if ($e instanceof Neo4jException || $e instanceof TransactionException) { |
| 61 | + return new DriverErrorResponse($uuid, $e); |
| 62 | + } |
| 63 | + |
| 64 | + $neo4jError = new Neo4jError( |
| 65 | + $e->getMessage(), |
| 66 | + (string) $e->getCode(), |
| 67 | + 'DatabaseError', |
| 68 | + 'Service', |
| 69 | + 'Service Unavailable' |
| 70 | + ); |
| 71 | + |
| 72 | + return new DriverErrorResponse($uuid, new Neo4jException([$neo4jError], $e)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private function getServerInfoFromDriver($driver): ServerInfoResponse |
| 77 | + { |
| 78 | + $connection = null; |
| 79 | + $pool = null; |
| 80 | + |
| 81 | + try { |
| 82 | + $pool = $this->getConnectionPool($driver); |
| 83 | + $connection = $this->acquireConnectionFromPool($pool, SessionConfiguration::default()); |
| 84 | + return new ServerInfoResponse($this->extractServerInfo($connection)); |
| 85 | + } finally { |
| 86 | + if ($connection !== null && $pool !== null) { |
| 87 | + $pool->release($connection); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Extracts connection pool from driver using reflection. |
| 94 | + */ |
| 95 | + private function getConnectionPool($driver) |
| 96 | + { |
| 97 | + $reflection = new ReflectionClass($driver); |
| 98 | + |
| 99 | + foreach (['pool', 'connectionPool', '_pool', 'connections'] as $propertyName) { |
| 100 | + if ($reflection->hasProperty($propertyName)) { |
| 101 | + $property = $reflection->getProperty($propertyName); |
| 102 | + $property->setAccessible(true); |
| 103 | + $pool = $property->getValue($driver); |
| 104 | + |
| 105 | + if ($pool !== null) { |
| 106 | + return $pool; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + throw new Exception('Could not access connection pool from driver'); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Acquire a connection from the pool. |
| 116 | + */ |
| 117 | + private function acquireConnectionFromPool($pool, SessionConfiguration $sessionConfig) |
| 118 | + { |
| 119 | + // Fail early if routing table has no readers |
| 120 | + if (method_exists($pool, 'getRoutingTable')) { |
| 121 | + $routingTable = $pool->getRoutingTable(); |
| 122 | + if ($routingTable !== null && empty($routingTable->getReaders())) { |
| 123 | + throw new Neo4jException([ |
| 124 | + new Neo4jError( |
| 125 | + 'No readers available in routing table', |
| 126 | + 'N/A', |
| 127 | + 'ClientError', |
| 128 | + 'Routing', |
| 129 | + 'RoutingTable' |
| 130 | + ) |
| 131 | + ]); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + $connectionGenerator = $pool->acquire($sessionConfig); |
| 136 | + $connection = GeneratorHelper::getReturnFromGenerator($connectionGenerator); |
| 137 | + |
| 138 | + if ($connection === null) { |
| 139 | + throw new Exception('Connection pool returned no connections'); |
| 140 | + } |
| 141 | + |
| 142 | + return $connection; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Extract server information from an active connection. |
| 147 | + */ |
| 148 | + private function extractServerInfo($connection): ServerInfo |
| 149 | + { |
| 150 | + foreach (['getServerAddress', 'getServerAgent', 'getProtocol'] as $method) { |
| 151 | + if (!method_exists($connection, $method)) { |
| 152 | + throw new Exception("Connection does not support {$method}()"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + $address = $connection->getServerAddress(); |
| 157 | + $agent = $connection->getServerAgent(); |
| 158 | + $protocol = $connection->getProtocol(); |
| 159 | + |
| 160 | + if (empty($address) || empty($agent)) { |
| 161 | + throw new Exception('Server info is incomplete'); |
| 162 | + } |
| 163 | + |
| 164 | + return new ServerInfo($address, $protocol, $agent); |
| 165 | + } |
| 166 | + |
| 167 | + private function getServerInfoFromSession($driver): ServerInfoResponse |
| 168 | + { |
| 169 | + if (method_exists($driver, 'session')) { |
| 170 | + $session = $driver->session(); |
| 171 | + } elseif (method_exists($driver, 'createSession')) { |
| 172 | + $session = $driver->createSession(); |
| 173 | + } elseif (method_exists($driver, 'newSession')) { |
| 174 | + $session = $driver->newSession(); |
| 175 | + } else { |
| 176 | + throw new Exception('No session creation method found on driver'); |
| 177 | + } |
| 178 | + |
| 179 | + try { |
| 180 | + $result = $session->run('RETURN 1'); |
| 181 | + return new ServerInfoResponse($result->summary()->getServerInfo()); |
| 182 | + } finally { |
| 183 | + $session->close(); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments