Dbal::getStorageConnectionParameters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php namespace Nwidart\LaravelBroadway\EventStore\Broadway\Drivers;
2
3
use Broadway\EventStore\Dbal\DBALEventStore;
4
use Doctrine\DBAL\Configuration;
5
use Doctrine\DBAL\DriverManager;
6
use Nwidart\LaravelBroadway\EventStore\Driver;
7
8
class Dbal implements Driver
9
{
10
    /**
11
     * @var \Illuminate\Config\Repository
12
     */
13
    private $config;
14
15
    public function __construct()
16
    {
17
        $this->config = app(\Illuminate\Config\Repository::class);
18
    }
19
20
    /**
21
     * @return object
22
     */
23
    public function getDriver()
24
    {
25
        $configuration = new Configuration();
26
27
        $connectionParams = $this->getStorageConnectionParameters();
28
        $connection = DriverManager::getConnection($connectionParams, $configuration);
29
        $payloadSerializer = app(\Broadway\Serializer\Serializer::class);
30
        $metadataSerializer = app(\Broadway\Serializer\Serializer::class);
31
32
        $binaryUuidConverter = app(\Broadway\UuidGenerator\Converter\BinaryUuidConverter::class);
33
34
        $table = $this->config->get('broadway.event-store.table', 'event_store');
35
36
        $app = app();
37
        $app->singleton(\Doctrine\DBAL\Connection::class, function () use ($connection) {
38
            return $connection;
39
        });
40
41
        return new DBALEventStore($connection, $payloadSerializer, $metadataSerializer, $table, false, $binaryUuidConverter);
42
    }
43
44
    /**
45
     * Make a connection parameters array based on the laravel configuration
46
     * @return array
47
     */
48
    private function getStorageConnectionParameters()
49
    {
50
        $databaseConnectionName = $this->config->get('broadway.event-store.connection', 'default');
51
52
        if ($databaseConnectionName === 'default') {
53
            $databaseConnectionName = $this->config->get('database.default');
54
        }
55
56
        $connectionParams = $this->config->get("database.connections.{$databaseConnectionName}");
57
58
        $connectionParams['dbname'] = $connectionParams['database'];
59
        $connectionParams['user'] = $connectionParams['username'];
60
        unset($connectionParams['database'], $connectionParams['username']);
61
        $connectionParams['driver'] = "pdo_".$connectionParams['driver'];
62
63
        return $connectionParams;
64
    }
65
}
66