Completed
Pull Request — master (#5)
by Alessandro
03:18
created

ConnectionConfiguration::getDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Models;
6
7
/**
8
 * Class ConnectionConfiguration.
9
 */
10
class ConnectionConfiguration
11
{
12
    /** @var string */
13
    private $host;
14
    /** @var int */
15
    private $port;
16
    /** @var string */
17
    private $username;
18
    /** @var string */
19
    private $password;
20
21
    /**
22
     * ConnectionConfiguration constructor.
23
     *
24
     * @param string $host
25
     * @param int    $port
26
     * @param string $username
27
     * @param string $password
28
     */
29 4
    public function __construct(string $host, int $port, string $username = '', string $password = '')
30
    {
31 4
        $this->host = $host;
32 4
        $this->port = $port;
33 4
        $this->username = $username;
34 4
        $this->password = $password;
35 4
    }
36
37
    /**
38
     * @return string
39
     */
40 2
    public function getHost(): string
41
    {
42 2
        return $this->host;
43
    }
44
45
    /**
46
     * @return int
47
     */
48 2
    public function getPort(): int
49
    {
50 2
        return $this->port;
51
    }
52
53
    /**
54
     * @return string
55
     */
56 2
    public function getUsername(): string
57
    {
58 2
        return $this->username;
59
    }
60
61
    /**
62
     * @return bool
63
     */
64 4
    public function hasCredentials(): bool
65
    {
66 4
        return !empty($this->username);
67
    }
68
69
    /**
70
     * @return string
71
     */
72 2
    public function getPassword(): string
73
    {
74 2
        return $this->password;
75
    }
76
77
    /**
78
     * @param string $databaseName
79
     *
80
     * @return string
81
     */
82 4
    public function getConnectionUri(string $databaseName = null): string
83
    {
84
        // Refactor with a uriBuilder service or something like that
85 4
        if ($this->hasCredentials() && !is_null($databaseName)) {
86 2
            return sprintf(
87 2
                'mongodb://%s:%s@%s:%d/%s',
88 2
                $this->username,
89 2
                $this->password,
90 2
                $this->host,
91 2
                $this->port,
92
                $databaseName
93
            );
94
        }
95
96 2
        if ($this->hasCredentials()) {
97 1
            return sprintf(
98 1
                'mongodb://%s:%s@%s:%d',
99 1
                $this->username,
100 1
                $this->password,
101 1
                $this->host,
102 1
                $this->port
103
            );
104
        }
105
106 1
        return sprintf(
107 1
            'mongodb://%s:%d',
108 1
            $this->host,
109 1
            $this->port
110
        );
111
    }
112
}
113