|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Laudis Neo4j package. |
| 7 | + * |
| 8 | + * (c) Laudis technologies <http://laudis.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; |
| 15 | + |
| 16 | +use function in_array; |
| 17 | +use function is_string; |
| 18 | +use Laudis\Neo4j\Bolt\BoltDriver; |
| 19 | +use Laudis\Neo4j\Common\Uri; |
| 20 | +use Laudis\Neo4j\Contracts\AuthenticateInterface; |
| 21 | +use Laudis\Neo4j\Contracts\DriverInterface; |
| 22 | +use Laudis\Neo4j\Contracts\FormatterInterface; |
| 23 | +use Laudis\Neo4j\Databags\DriverConfiguration; |
| 24 | +use Laudis\Neo4j\Http\HttpDriver; |
| 25 | +use Laudis\Neo4j\Neo4j\Neo4jDriver; |
| 26 | +use Psr\Http\Message\UriInterface; |
| 27 | + |
| 28 | +/** |
| 29 | + * @psalm-import-type OGMResults from \Laudis\Neo4j\Formatter\OGMFormatter |
| 30 | + */ |
| 31 | +final class DriverFactory |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @template U |
| 35 | + * |
| 36 | + * @param FormatterInterface<U> $formatter |
| 37 | + * @param string|UriInterface $uri |
| 38 | + * |
| 39 | + * @return ( |
| 40 | + * func_num_args() is 4 |
| 41 | + * ? DriverInterface<U> |
| 42 | + * : DriverInterface<OGMResults> |
| 43 | + * ) |
| 44 | + */ |
| 45 | + public static function create($uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null, FormatterInterface $formatter = null): DriverInterface |
| 46 | + { |
| 47 | + if (is_string($uri)) { |
| 48 | + $uri = Uri::create($uri); |
| 49 | + } |
| 50 | + $scheme = $uri->getScheme(); |
| 51 | + $scheme = $scheme === '' ? 'bolt' : $scheme; |
| 52 | + |
| 53 | + if (in_array($scheme, ['bolt', 'bolt+s', 'bolt+ssc'])) { |
| 54 | + return self::createBoltDriver($uri, $configuration, $authenticate, $formatter); |
| 55 | + } |
| 56 | + |
| 57 | + if (in_array($scheme, ['neo4j', 'neo4j+s', 'neo4j+ssc'])) { |
| 58 | + return self::createNeo4jDriver($uri, $configuration, $authenticate, $formatter); |
| 59 | + } |
| 60 | + |
| 61 | + return self::createHttpDriver($uri, $configuration, $authenticate, $formatter); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @template U |
| 66 | + * |
| 67 | + * @param FormatterInterface<U> $formatter |
| 68 | + * @param string|UriInterface $uri |
| 69 | + * |
| 70 | + * @return ( |
| 71 | + * func_num_args() is 4 |
| 72 | + * ? DriverInterface<U> |
| 73 | + * : DriverInterface<OGMResults> |
| 74 | + * ) |
| 75 | + * @psalm-mutation-free |
| 76 | + */ |
| 77 | + private static function createBoltDriver($uri, ?DriverConfiguration $configuration, ?AuthenticateInterface $authenticate, FormatterInterface $formatter = null): DriverInterface |
| 78 | + { |
| 79 | + if ($formatter !== null) { |
| 80 | + return BoltDriver::create($uri, $configuration, $authenticate, $formatter); |
| 81 | + } |
| 82 | + |
| 83 | + return BoltDriver::create($uri, $configuration, $authenticate); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @template U |
| 88 | + * |
| 89 | + * @param FormatterInterface<U> $formatter |
| 90 | + * @param string|UriInterface $uri |
| 91 | + * |
| 92 | + * @return ( |
| 93 | + * func_num_args() is 4 |
| 94 | + * ? DriverInterface<U> |
| 95 | + * : DriverInterface<OGMResults> |
| 96 | + * ) |
| 97 | + * @psalm-mutation-free |
| 98 | + */ |
| 99 | + private static function createNeo4jDriver($uri, ?DriverConfiguration $configuration, ?AuthenticateInterface $authenticate, FormatterInterface $formatter = null): DriverInterface |
| 100 | + { |
| 101 | + if ($formatter !== null) { |
| 102 | + return Neo4jDriver::create($uri, $configuration, $authenticate, $formatter); |
| 103 | + } |
| 104 | + |
| 105 | + return Neo4jDriver::create($uri, $configuration, $authenticate); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @template U |
| 110 | + * |
| 111 | + * @param FormatterInterface<U> $formatter |
| 112 | + * @param string|UriInterface $uri |
| 113 | + * |
| 114 | + * @return ( |
| 115 | + * func_num_args() is 4 |
| 116 | + * ? DriverInterface<U> |
| 117 | + * : DriverInterface<OGMResults> |
| 118 | + * ) |
| 119 | + * @psalm-mutation-free |
| 120 | + */ |
| 121 | + private static function createHttpDriver($uri, ?DriverConfiguration $configuration, ?AuthenticateInterface $authenticate, FormatterInterface $formatter = null): DriverInterface |
| 122 | + { |
| 123 | + if ($formatter !== null) { |
| 124 | + return HttpDriver::create($uri, $configuration, $authenticate, $formatter); |
| 125 | + } |
| 126 | + |
| 127 | + return HttpDriver::create($uri, $configuration, $authenticate); |
| 128 | + } |
| 129 | +} |
0 commit comments