Skip to content

Commit 61a10fe

Browse files
committed
Refactor Mysqli related factories
Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent bd366a1 commit 61a10fe

5 files changed

Lines changed: 64 additions & 67 deletions

src/Container/ConnectionInterfaceFactory.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpDb\Mysql\Container;
66

77
use PhpDb\Adapter\Driver\ConnectionInterface;
8+
use PhpDb\Adapter\Exception\InvalidConnectionParametersException;
89
use PhpDb\Mysql\Connection;
910
use Psr\Container\ContainerInterface;
1011

@@ -15,23 +16,13 @@ public function __invoke(
1516
string $requestedName,
1617
?array $options = null
1718
): ConnectionInterface&Connection {
18-
/** @var array $config */
19-
$config = $container->get('config');
20-
21-
/** @var array $dbConfig */
22-
$dbConfig = $config['db'] ?? [];
23-
24-
/** @var array $connectionConfig */
25-
$connectionConfig = $dbConfig['connection'] ?? [];
26-
27-
return new Connection($connectionConfig);
28-
}
29-
30-
public static function createFromConfig(
31-
ContainerInterface $container,
32-
string $requestedName
33-
): ConnectionInterface&Connection {
34-
$adapterConfig = $container->get('config')['db']['adapters'][$requestedName] ?? [];
35-
return new Connection($adapterConfig['connection'] ?? []);
19+
if (! is_array($options['connection']) || $options['connection'] === []) {
20+
throw new InvalidConnectionParametersException(
21+
'Connection configuration must be an array of parameters passed via $options["connection"]',
22+
$options['connection']
23+
);
24+
}
25+
26+
return new Connection($options['connection']);
3627
}
3728
}

src/Container/DriverInterfaceFactory.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace PhpDb\Mysql\Container;
66

7+
use Laminas\ServiceManager\ServiceManager;
78
use PhpDb\Adapter\Driver\DriverInterface;
9+
use PhpDb\Adapter\Driver\ResultInterface;
10+
use PhpDb\Exception\ContainerException;
811
use PhpDb\Mysql\Connection;
912
use PhpDb\Mysql\Driver;
1013
use PhpDb\Mysql\Result;
@@ -13,24 +16,32 @@
1316

1417
final class DriverInterfaceFactory
1518
{
16-
public function __invoke(ContainerInterface $container): DriverInterface&Driver
17-
{
18-
/** @var array $config */
19-
$config = $container->get('config');
20-
21-
/** @var array $dbConfig */
22-
$dbConfig = $config['db'] ?? [];
23-
24-
/** @var array $options */
25-
$options = $dbConfig['options'] ?? [];
19+
public function __invoke(
20+
ContainerInterface&ServiceManager $container,
21+
string $requestedName,
22+
?array $options = null
23+
): DriverInterface&Driver {
24+
if (! isset($options['connection'])) {
25+
throw ContainerException::forService(
26+
Driver::class,
27+
self::class,
28+
'$options["connection"] must containe an array of connection configuration.'
29+
);
30+
}
2631

2732
/** @var Driver\ConnectionInterface&Connection $connectionInstance */
28-
$connectionInstance = $container->get(Connection::class);
33+
$connectionInstance = $container->build(Connection::class, $options);
2934

3035
/** @var Driver\StatementInterface&Statement $statementInstance */
31-
$statementInstance = $container->get(Statement::class);
36+
$statementInstance = $container->build(
37+
Statement::class,
38+
$options['options'] ?? []
39+
);
40+
3241
/** @var Driver\ResultInterface&Result $resultInstance */
33-
$resultInstance = $container->get(Result::class);
42+
$resultInstance = $container->has(ResultInterface::class)
43+
? $container->get(ResultInterface::class)
44+
: new Result();
3445

3546
return new Driver(
3647
$connectionInstance,

src/Container/MetadataInterfaceFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111

1212
final class MetadataInterfaceFactory
1313
{
14-
public function __invoke(ContainerInterface $container): MetadataInterface&Metadata\Source
15-
{
16-
$adapterInterface = $container->get(AdapterInterface::class);
17-
return new Metadata\Source($adapterInterface);
14+
public const ADAPTER_SERVICE_NAME = 'adapter_service_name';
15+
public function __invoke(
16+
ContainerInterface $container,
17+
string $requestedName,
18+
?array $options = null
19+
): MetadataInterface&Metadata\Source {
20+
$adapterServiceName = $options[self::ADAPTER_SERVICE_NAME] ?? AdapterInterface::class;
21+
22+
return new Metadata\Source($container->get($adapterServiceName));
1823
}
1924
}

src/Container/PlatformInterfaceFactory.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,28 @@
44

55
namespace PhpDb\Mysql\Container;
66

7-
use mysqli;
8-
use PDO;
9-
use PhpDb\Adapter\Driver\DriverInterface;
107
use PhpDb\Adapter\Platform\PlatformInterface;
8+
use PhpDb\Exception\ContainerException;
119
use PhpDb\Mysql\AdapterPlatform;
10+
use PhpDb\Mysql\Driver as MysqliDriver;
11+
use PhpDb\Mysql\Pdo\Driver as PdoDriver;
1212
use Psr\Container\ContainerInterface;
1313

1414
final class PlatformInterfaceFactory
1515
{
16-
public function __invoke(ContainerInterface $container): PlatformInterface&AdapterPlatform
17-
{
18-
/** @var array $config */
19-
$config = $container->get('config');
20-
21-
/** @var array $dbConfig */
22-
$dbConfig = $config['db'] ?? [];
23-
24-
/** @var string $driver */
25-
$driver = $dbConfig['driver'];
26-
27-
/** @var DriverInterface|mysqli|PDO $driverInstance */
28-
$driverInstance = $container->get($driver);
29-
16+
public function __invoke(
17+
ContainerInterface $container,
18+
string $requestedName,
19+
?array $options = null
20+
): PlatformInterface&AdapterPlatform {
21+
$driverInstance = $options['driver'] ?? null;
22+
if (! $driverInstance instanceof MysqliDriver && ! $driverInstance instanceof PdoDriver) {
23+
throw ContainerException::forService(
24+
AdapterPlatform::class,
25+
self::class,
26+
'$options["driver"] must be an instance of ' . MysqliDriver::class . ' or ' . PdoDriver::class . '.'
27+
);
28+
}
3029
return new AdapterPlatform($driverInstance);
3130
}
3231
}

src/Container/StatementInterfaceFactory.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,11 @@
1010

1111
final class StatementInterfaceFactory
1212
{
13-
public function __invoke(ContainerInterface $container): StatementInterface&Statement
14-
{
15-
/** @var array $config */
16-
$config = $container->get('config');
17-
18-
/** @var array $dbConfig */
19-
$dbConfig = $config['db'] ?? [];
20-
21-
/** @var array $options */
22-
$options = $dbConfig['options'] ?? [];
23-
24-
/** @var bool $bufferResults */
25-
$bufferResults = $options['buffer_results'] ?? false;
26-
27-
return new Statement(bufferResults: $bufferResults);
13+
public function __invoke(
14+
ContainerInterface $container,
15+
string $requestedName,
16+
?array $options = null
17+
): StatementInterface&Statement {
18+
return new Statement(bufferResults: $options['buffer_results'] ?? false);
2819
}
2920
}

0 commit comments

Comments
 (0)