-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
TableGatewayFactory.php
57 lines (50 loc) · 1.48 KB
/
TableGatewayFactory.php
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
<?php
declare(strict_types=1);
namespace StatusLib;
use DomainException;
use Psr\Container\ContainerInterface;
use function sprintf;
/**
* Service factory for the StatusLib TableGateway
*
* If the "statuslib" key is present, and either the "db" or "table" subkeys
* are present and valid, uses those; otherwise, uses defaults of "Db\StatusLib"
* and "status", respectively.
*
* If the DB service does not exist, raises an error.
*
* Otherwise, creates a TableGateway instance with the DB service and table.
*/
class TableGatewayFactory
{
/**
* @param ContainerInterface $services
* @return TableGateway
*/
public function __invoke($services)
{
$db = 'Db\StatusLib';
$table = 'status';
if ($services->has('config')) {
$config = $services->get('config');
switch (isset($config['statuslib'])) {
case true:
$config = $config['statuslib'];
$db = $config['db'] ?? $db;
$table = $config['table'] ?? $table;
break;
case false:
default:
break;
}
}
if (! $services->has($db)) {
throw new DomainException(sprintf(
'Unable to create %s due to missing "%s" service',
TableGateway::class,
$db
));
}
return new TableGateway($table, $services->get($db));
}
}