NamedPdoModule   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 97
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A configure() 0 5 2
A configureSingleDsn() 0 12 1
A configureMasterSlaveDsn() 0 13 2
A changeHost() 0 10 2
1
<?php
2
/**
3
 * This file is part of the Ray.AuraSqlModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\AuraSqlModule;
8
9
use Aura\Sql\ConnectionLocator;
10
use Aura\Sql\ExtendedPdo;
11
use Aura\Sql\ExtendedPdoInterface;
12
use Ray\Di\AbstractModule;
13
14
class NamedPdoModule extends AbstractModule
15
{
16
    const PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i';
17
18
    /**
19
     * @var string
20
     */
21
    private $qualifer;
22
23
    /**
24
     * @var string
25
     */
26
    private $dsn;
27
28
    /**
29
     * @var string
30
     */
31
    private $user;
32
33
    /**
34
     * @var string
35
     */
36
    private $password;
37
38
    /**
39
     * @var string
40
     */
41
    private $slave;
42
43 6
    public function __construct(
44
        string $qualifer,
45
        string $dsn,
46
        string $user = '',
47
        string $pass = '',
48
        string $slave = ''
49
    ) {
50 6
        $this->qualifer = $qualifer;
51 6
        $this->dsn = $dsn;
52 6
        $this->user = $user;
53 6
        $this->password = $pass;
54 6
        $this->slave = $slave;
55 6
        parent::__construct();
56 6
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 6
    protected function configure()
62
    {
63 6
        $this->slave ? $this->configureMasterSlaveDsn($this->qualifer, $this->dsn, $this->user, $this->password, $this->slave)
64 3
            : $this->configureSingleDsn($this->qualifer, $this->dsn, $this->user, $this->password);
65 6
    }
66
67 3
    private function configureSingleDsn($qualifer, $dsn, $user, $password)
68
    {
69 3
        $this->bind(ExtendedPdoInterface::class)
70 3
            ->annotatedWith($qualifer)
71 3
            ->toConstructor(
72 3
                ExtendedPdo::class,
73 3
                "dsn={$qualifer}_dsn,username={$qualifer}_username,password={$qualifer}_password"
74
            );
75 3
        $this->bind()->annotatedWith("{$qualifer}_dsn")->toInstance($dsn);
76 3
        $this->bind()->annotatedWith("{$qualifer}_username")->toInstance($user);
77 3
        $this->bind()->annotatedWith("{$qualifer}_password")->toInstance($password);
78 3
    }
79
80 3
    private function configureMasterSlaveDsn($qualifer, $dsn, $user, $password, $slaveList)
81
    {
82 3
        $locator = new ConnectionLocator();
83 3
        $locator->setWrite('master', new Connection($dsn, $user, $password));
84 3
        $i = 1;
85 3
        $slaves = \explode(',', $slaveList);
86 3
        foreach ($slaves as $slave) {
87 3
            $slaveDsn = $this->changeHost($dsn, $slave);
88 3
            $name = 'slave' . (string) $i++;
89 3
            $locator->setRead($name, new Connection($slaveDsn, $user, $password));
90
        }
91 3
        $this->install(new AuraSqlReplicationModule($locator, $qualifer));
0 ignored issues
show
Documentation introduced by
new \Ray\AuraSqlModule\A...le($locator, $qualifer) is of type object<Ray\AuraSqlModule...raSqlReplicationModule>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92 3
    }
93
94
    /**
95
     * @param string $dsn
96
     * @param string $host
97
     *
98
     * @return string
99
     */
100 3
    private function changeHost($dsn, $host)
101
    {
102 3
        \preg_match(self::PARSE_PDO_DSN_REGEX, $dsn, $parts);
103 3
        if (! $parts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parts of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104 1
            return $dsn;
105
        }
106 2
        $dsn = \sprintf('%s:%s=%s;%s', $parts[1], $parts[2], $host, $parts[3]);
107
108 2
        return $dsn;
109
    }
110
}
111