DatabaseService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 99
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A setCloudEnvironment() 0 5 1
A getCloudEnvironment() 0 4 1
A setResolver() 0 5 1
A getResolver() 0 8 2
A credentials() 0 14 3
A getCurrentHost() 0 10 2
1
<?php
2
3
namespace Acquia\Cloud\Database;
4
5
use Acquia\Cloud\Environment\CloudEnvironment;
6
use Acquia\Cloud\Environment\CloudEnvironmentAware;
7
use Acquia\Cloud\Environment\CloudEnvironmentInterface;
8
9
class DatabaseService implements CloudEnvironmentAware, DatabaseServiceInterface
10
{
11
    /**
12
     * @var \Acquia\Cloud\Environment\CloudEnvironmentInterface
13
     */
14
    private $cloudEnvironment;
15
16
    /**
17
     * @var \Net_DNS2_Resolver
18
     */
19
    private $resolver;
20
21
    /**
22
     * @param \Acquia\Cloud\Environment\CloudEnvironmentInterface $cloudEnvironment
23
     */
24 24
    public function __construct(CloudEnvironmentInterface $cloudEnvironment = null)
25
    {
26 24
        if ($cloudEnvironment === null) {
27 21
            $cloudEnvironment = new CloudEnvironment();
28 21
        }
29 24
        $this->setCloudEnvironment($cloudEnvironment);
30 24
    }
31
32
    /**
33
     * {@inheritDoc}
34
     *
35
     * @return \Acquia\Cloud\Database\DatabaseService
36
     */
37 24
    public function setCloudEnvironment(CloudEnvironmentInterface $cloudEnvironment)
38
    {
39 24
        $this->cloudEnvironment = $cloudEnvironment;
40 24
        return $this;
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46 6
    public function getCloudEnvironment()
47
    {
48 6
        return $this->cloudEnvironment;
49
    }
50
51
    /**
52
     * @param \Net_DNS2_Resolver $resolver
53
     *
54
     * @return \Acquia\Cloud\Database\DatabaseService
55
     */
56 12
    public function setResolver(\Net_DNS2_Resolver $resolver)
57
    {
58 12
        $this->resolver = $resolver;
59 12
        return $this;
60
    }
61
62
    /**
63
     * @return \Net_DNS2_Resolver
64
     */
65 15
    public function getResolver()
66
    {
67 15
        if (!isset($this->resolver)) {
68 6
            $options = array('nameservers' => array('127.0.0.1', 'dns-master'));
69 6
            $this->resolver = new \Net_DNS2_Resolver($options);
70 6
        }
71 15
        return $this->resolver;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 9
    public function credentials($dbName)
78
    {
79 9
        $creds = $this->cloudEnvironment->serviceCredentials();
80
81 9
        if (!isset($creds['databases'][$dbName])) {
82 3
            throw new \OutOfBoundsException('Invalid database: ' . $dbName);
83
        }
84
85 6
        $database = $creds['databases'][$dbName];
86 6
        $host = $this->getCurrentHost($database['db_cluster_id']);
87 6
        $database['host'] = ($host) ?: key($database['db_url_ha']);
88
89 6
        return new DatabaseCredentials($database);
90
    }
91
92
    /**
93
     * @param in $clusterId
94
     *
95
     * @return string
96
     */
97 9
    public function getCurrentHost($clusterId)
98
    {
99
        try {
100 9
            $resolver = $this->getResolver();
101 9
            $response = $resolver->query('cluster-' . $clusterId . '.mysql', 'CNAME');
102 3
            return $response->answer[0]->cname;
103 6
        } catch (\Net_DNS2_Exception $e) {
104 6
            return '';
105
        }
106
    }
107
}
108