Completed
Branch 1.x (1d6dc1)
by Akihito
09:08
created

AuraSqlModule::changeHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2.0185
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\AuraSqlModule\Pagerfanta\AuraSqlPagerModule;
13
use Ray\Di\AbstractModule;
14
use Ray\Di\Scope;
15
16
class AuraSqlModule extends AbstractModule
17
{
18
    const PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i';
19
20
    /**
21
     * @var string
22
     */
23
    private $dsn;
24
25
    /**
26
     * @var string
27
     */
28
    private $user;
29
30
    /**
31
     * @var string
32
     */
33
    private $password;
34
35
    /**
36
     * @var string
37
     */
38
    private $slave;
39
40
    /**
41
     * @param string $dsn
42
     * @param string $user
43
     * @param string $password
44
     * @param string $slave    comma separated slave host list
45
     */
46 12
    public function __construct($dsn, $user = '', $password = '', $slave = null)
47
    {
48 12
        $this->dsn = $dsn;
49 12
        $this->user = $user;
50 12
        $this->password = $password;
51 12
        $this->slave = $slave;
52 12
        parent::__construct();
53 12
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 12
    protected function configure()
59
    {
60 12
        $this->slave ? $this->configureMasterSlaveDsn() : $this->configureSingleDsn();
61
        // @Transactional
62 12
        $this->install(new TransactionalModule);
63 12
        $this->install(new AuraSqlPagerModule());
64 12
        preg_match(self::PARSE_PDO_DSN_REGEX, $this->dsn, $parts);
65 12
        $dbType = isset($parts[1]) ? $parts[1] : '';
66 12
        $this->install(new AuraSqlQueryModule($dbType));
67 12
    }
68
69 9
    private function configureSingleDsn()
70
    {
71 9
        $this->bind(ExtendedPdoInterface::class)->toConstructor(ExtendedPdo::class, 'dsn=pdo_dsn,username=pdo_user,password=pdo_pass,options=pdo_option')->in(Scope::SINGLETON);
72 9
        $this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn);
73 9
        $this->bind()->annotatedWith('pdo_user')->toInstance($this->user);
74 9
        $this->bind()->annotatedWith('pdo_pass')->toInstance($this->password);
75 9
        $this->bind()->annotatedWith('pdo_option')->toInstance([]);
76 9
    }
77
78 3
    private function configureMasterSlaveDsn()
79
    {
80 3
        $locator = new ConnectionLocator;
81 3
        $locator->setWrite('master', new Connection($this->dsn, $this->user, $this->password));
82 3
        $i = 1;
83 3
        $slaves = explode(',', $this->slave);
84 3
        foreach ($slaves as $slave) {
85 3
            $slaveDsn = $this->changeHost($this->dsn, $slave);
86 3
            $name = 'slave' . (string) $i++;
87 3
            $locator->setRead($name, new Connection($slaveDsn, $this->user, $this->password));
88 2
        }
89 3
        $this->install(new AuraSqlReplicationModule($locator));
90 3
    }
91
92
    /**
93
     * @param string $dsn
94
     * @param string $host
95
     *
96
     * @return string
97
     */
98 3
    private function changeHost($dsn, $host)
99
    {
100 3
        preg_match(self::PARSE_PDO_DSN_REGEX, $dsn, $parts);
101 3
        if (empty($parts)) {
102
            return $dsn;
103
        }
104
//        [
105
//            0 => 'mysql:host=localhost;port=3307;dbname=testdb',
106
//            1 => 'mysql',
107
//            2 => 'host',
108
//            3 => 'port=3307;dbname=testdb'
109
//        ]
110 3
        $dsn = sprintf('%s:%s=%s;%s', $parts[1], $parts[2], $host, $parts[3]);
111
112 3
        return $dsn;
113
    }
114
}
115