PdoQueueStoreConnection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Da\Mailer\Queue\Backend\Pdo;
4
5
use Da\Mailer\Queue\Backend\AbstractQueueStoreConnection;
6
use PDO;
7
8
class PdoQueueStoreConnection extends AbstractQueueStoreConnection
9
{
10
    /**
11
     * PdoQueueStoreConnection constructor.
12
     *
13
     * @param array $configuration
14 4
     */
15
    public function __construct(array $configuration)
16 4
    {
17 4
        parent::__construct($configuration);
18
        $this->defineConnectionString();
19
    }
20
21
    protected function defineConnectionString()
22 6
    {
23
        if (! isset($this->configuration['dsn'])) {
24 6
            $this->configuration['dsn'] = sprintf("mysql:host=%s;dbname=%s;port=%s", $this->configuration['host'] ?? '', $this->configuration['db'] ?? '', $this->configuration['port'] ?? 3306);
25 6
        }
26 6
    }
27 6
28 6
    /**
29
     * @return PdoQueueStoreConnection
30 6
     */
31 6
    public function connect()
32
    {
33 6
        $this->disconnect();
34
        $username = $this->getConfigurationValue('username');
35
        $password = $this->getConfigurationValue('password');
36
        $options = $this->getConfigurationValue('options');
37
        $this->instance = new PDO($this->getConfigurationValue('dsn'), $username, $password, $options);
38
        $this->instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
39
        return $this;
40
    }
41 6
42
    /**
43 6
     * Returns the connection instance.
44 1
     *
45 1
     * @return PDO
46
     */
47 6
    public function getInstance()
48
    {
49
        if ($this->instance === null) {
50
            $this->connect();
51
        }
52
53
        return $this->instance;
54
    }
55
}
56