ConnectionFactory   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A createConnection() 0 15 1
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace LIN3S\SharedKernel\Infrastructure\Persistence\Sql;
15
16
/**
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
final class ConnectionFactory
20
{
21
    private $driver;
22
    private $dbName;
23
    private $host;
24
    private $port;
25
    private $username;
26
    private $password;
27
    private $charset;
28
29
    public function __construct(
30
        string $driver,
31
        string $dbName,
32
        string $host,
33
        ?string $port,
34
        string $username,
35
        ?string $password,
36
        string $charset = 'utf8'
37
    ) {
38
        $this->driver = $driver;
39
        $this->dbName = $dbName;
40
        $this->host = $host;
41
        $this->port = $port;
42
        $this->username = $username;
43
        $this->password = $password;
44
        $this->charset = $charset;
45
    }
46
47
    public function createConnection() : PDO
48
    {
49
        $dsn = sprintf(
50
            '%s:dbname=%s;host=%s;port=%s;charset=%s',
51
            $this->driver,
52
            $this->dbName,
53
            $this->host,
54
            $this->port,
55
            $this->charset
56
        );
57
58
        return new Pdo(
59
            new \PDO($dsn, $this->username, $this->password)
60
        );
61
    }
62
}
63