DbalDriver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 3 1
A __construct() 0 9 2
A connect() 0 9 2
A check() 0 3 1
A createSchema() 0 14 1
1
<?php
2
/**
3
 * This file is part of the EventStoreManager package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SimpleEventStoreManager\Infrastructure\Drivers;
12
13
use Doctrine\DBAL\Configuration;
14
use Doctrine\DBAL\Connection;
15
use Doctrine\DBAL\DBALException;
16
use Doctrine\DBAL\DriverManager;
17
use SimpleEventStoreManager\Infrastructure\Drivers\Contracts\DriverInterface;
18
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\DriverConnectionException;
19
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\MalformedDriverConfigException;
20
use SimpleEventStoreManager\Infrastructure\Drivers\Exceptions\NotInstalledDriverCheckException;
21
22
class DbalDriver implements DriverInterface
23
{
24
    /**
25
     * @var
26
     */
27
    private $config;
28
29
    /**
30
     * @var Connection
31
     */
32
    private $instance;
33
34
    /**
35
     * DbalDriver constructor.
36
     *
37
     * @codeCoverageIgnore
38
     *
39
     * @param array $config
40
     *
41
     * @throws NotInstalledDriverCheckException
42
     */
43
    public function __construct(array $config = [])
44
    {
45
        $this->config = $config;
46
47
        if (!$this->check()) {
48
            throw new NotInstalledDriverCheckException('Dbal is not loaded.');
49
        }
50
51
        $this->connect();
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function check()
58
    {
59
        return class_exists('Doctrine\DBAL\Connection');
60
    }
61
62
    /**
63
     * @return bool
64
     *
65
     * @throws DriverConnectionException
66
     */
67
    public function connect()
68
    {
69
        try {
70
            $this->instance = DriverManager::getConnection($this->config, new Configuration());
71
            $this->createSchema();
72
73
            return true;
74
        } catch (DBALException $e) {
75
            throw new DriverConnectionException($e->getMessage());
76
        }
77
    }
78
79
    /**
80
     * create schema.
81
     */
82
    private function createSchema()
83
    {
84
        $query = "CREATE TABLE IF NOT EXISTS `".PdoDriver::EVENTSTORE_TABLE_NAME."` (
85
          `id` int(11) NOT NULL AUTO_INCREMENT,
86
          `uuid` char(36) COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:guid)',
87
          `version` int(10) unsigned NOT NULL,
88
          `payload` varchar(255) DEFAULT NULL,
89
          `type` varchar(255) DEFAULT NULL,
90
          `body` longtext,
91
          `occurred_on` datetime(6),
92
          PRIMARY KEY (`id`)
93
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
94
95
        $this->instance->exec($query);
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    public function instance()
102
    {
103
        return $this->instance;
104
    }
105
}