DatabaseCredentials::dsn()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Acquia\Cloud\Database;
4
5
class DatabaseCredentials extends \ArrayObject
6
{
7
    /**
8
     * @return int
9
     */
10 6
    public function id()
11
    {
12 6
        return $this['id'];
13
    }
14
15
    /**
16
     * @return int
17
     */
18 6
    public function clusterId()
19
    {
20 6
        return $this['db_cluster_id'];
21
    }
22
23
    /**
24
     * @return string
25
     */
26 6
    public function role()
27
    {
28 6
        return $this['role'];
29
    }
30
31
    /**
32
     * @return string
33
     */
34 6
    public function databaseName()
35
    {
36 6
        return $this['name'];
37
    }
38
39
    /**
40
     * @return string
41
     */
42 9
    public function username()
43
    {
44 9
        return $this['user'];
45
    }
46
47
    /**
48
     * @return string
49
     */
50 9
    public function password()
51
    {
52 9
        return $this['pass'];
53
    }
54
55
    /**
56
     * @return string
57
     */
58 6
    public function port()
59
    {
60 6
        return $this['port'];
61
    }
62
63
    /**
64
     * @return string
65
     */
66 9
    public function host()
67
    {
68 9
        return $this['host'];
69
    }
70
71
    /**
72
     * @return array
73
     */
74 9
    public function urls()
75
    {
76 9
        return $this['db_url_ha'];
77
    }
78
79
    /**
80
     * @return string
81
     */
82 6
    public function activeUrl()
83
    {
84 6
        $host = $this->host();
85 6
        $urls = $this->urls();
86 6
        return $urls[$host];
87
    }
88
89
    /**
90
     * Returns the DSN for the active host.
91
     *
92
     * @return string
93
     *
94
     * @throws \OutOfBoundsException
95
     */
96 18
    public function dsn()
97
    {
98
        // @see https://github.com/acquia/acquia-sdk-php/issues/27
99 18
        if (!isset($this['name'])) {
100 3
            throw new \OutOfBoundsException('Malformed response: expecting "name" property');
101
        }
102 15
        if (!isset($this['host'])) {
103 3
            throw new \OutOfBoundsException('Malformed response: expecting "host" property');
104
        }
105 12
        if (!isset($this['port'])) {
106 3
            throw new \OutOfBoundsException('Malformed response: expecting "port" property');
107
        }
108
109 9
        return 'mysql:dbname=' . $this['name'] . ';host=' . $this['host'] . ';port=' . $this['port'];
110
    }
111
112
    /**
113
     * Returns the DSN for the active host.
114
     */
115 9
    public function __toString()
116
    {
117 9
        return $this->dsn();
118
    }
119
}
120