Completed
Push — master ( 408932...ce7eb5 )
by Dawid
06:58
created

ConnectionOptions   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 54
ccs 20
cts 26
cp 0.7692
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isPersistentConnection() 0 3 2
A silenceErrors() 0 3 1
A __construct() 0 8 1
A getAttributes() 0 3 1
A getPassword() 0 3 1
A useExceptions() 0 6 2
A getUsername() 0 3 1
A usePersistentConnection() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Driver\Pdo;
4
5
use PDO;
6
7
final class ConnectionOptions
8
{
9
    private $username;
10
    private $databaseName;
11
    private $password;
12
    private $attributes = [];
13
14 49
    public function __construct(string $databaseName = '', string $username = '', string $password = '')
15
    {
16 49
        $this->username = $username;
17 49
        $this->password = $password;
18 49
        $this->databaseName = $databaseName;
19
20 49
        $this->usePersistentConnection();
21 49
        $this->useExceptions();
22 49
    }
23
24
    public function silenceErrors(): void
25
    {
26
        $this->attributes[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
27
    }
28
29 49
    public function useExceptions($use = true): void
30
    {
31 49
        if ($use) {
32 49
            $this->attributes[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
33
        } else {
34
            $this->attributes[PDO::ATTR_ERRMODE] = PDO::ERRMODE_WARNING;
35
        }
36 49
    }
37
38 49
    public function usePersistentConnection($use = true): void
39
    {
40 49
        $this->attributes[PDO::ATTR_PERSISTENT] = $use;
41 49
    }
42
43
    public function isPersistentConnection(): bool
44
    {
45
        return isset($this->attributes[PDO::ATTR_PERSISTENT]) && $this->attributes[PDO::ATTR_PERSISTENT];
46
    }
47
48 25
    public function getUsername(): string
49
    {
50 25
        return $this->username;
51
    }
52
53 25
    public function getPassword(): string
54
    {
55 25
        return $this->password;
56
    }
57
58 25
    public function getAttributes(): array
59
    {
60 25
        return $this->attributes;
61
    }
62
}
63