Completed
Pull Request — 1.x (#24)
by inaka
03:42
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
Metric Value
dl 0
loc 16
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 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\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 4
    public function __construct($dsn, $user = '', $password = '', $slave = null)
47
    {
48 4
        $this->dsn = $dsn;
49 4
        $this->user = $user;
50 4
        $this->password = $password;
51 4
        $this->slave = $slave;
52 4
        parent::__construct();
53 4
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 4
    protected function configure()
59
    {
60 4
        $this->slave ? $this->configureMasterSlaveDsn() : $this->configureSingleDsn();
61
        // @Transactional
62 4
        $this->install(new TransactionalModule);
63 4
        $this->install(new AuraSqlPagerModule());
64 4
        preg_match(self::PARSE_PDO_DSN_REGEX, $this->dsn, $parts);
65 4
        $dbType = isset($parts[1]) ? $parts[1] : '';
66 4
        $this->install(new AuraSqlQueryModule($dbType));
67 4
    }
68
69 3
    private function configureSingleDsn()
70
    {
71 3
        $this->bind(ExtendedPdoInterface::class)->toConstructor(ExtendedPdo::class, 'dsn=pdo_dsn,username=pdo_user,password=pdo_pass,options=pdo_option')->in(Scope::SINGLETON);
72 3
        $this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn);
73 3
        $this->bind()->annotatedWith('pdo_user')->toInstance($this->user);
74 3
        $this->bind()->annotatedWith('pdo_pass')->toInstance($this->password);
75 3
        $this->bind()->annotatedWith('pdo_option')->toInstance([]);
76 3
    }
77
78 1
    private function configureMasterSlaveDsn()
79
    {
80 1
        $locator = new ConnectionLocator;
81 1
        $locator->setWrite('master', new Connection($this->dsn, $this->user, $this->password));
82 1
        $i = 1;
83 1
        $slaves = explode(',', $this->slave);
84 1
        foreach ($slaves as $slave) {
85 1
            $slaveDsn = $this->changeHost($this->dsn, $slave);
86 1
            $name = 'slave' . (string) $i++;
87 1
            $locator->setRead($name, new Connection($slaveDsn, $this->user, $this->password));
88
        }
89 1
        $this->install(new AuraSqlReplicationModule($locator));
90 1
    }
91
92
    /**
93
     * @param string $dsn
94
     * @param string $host
95
     *
96
     * @return string
97
     */
98 1
    private function changeHost($dsn, $host)
99
    {
100 1
        preg_match(self::PARSE_PDO_DSN_REGEX, $dsn, $parts);
101 1
        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 1
        $dsn = sprintf('%s:%s=%s;%s', $parts[1], $parts[2], $host, $parts[3]);
111
112 1
        return $dsn;
113
    }
114
}
115