ConnectionConfig::getUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * This file is part of graze/sprout.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/sprout/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/sprout
12
 */
13
14
namespace Graze\Sprout\Config;
15
16
use Graze\ConfigValidation\ConfigValidatorInterface;
17
use Graze\ConfigValidation\Validate;
18
use Respect\Validation\Validator as v;
19
20
class ConnectionConfig implements ConnectionConfigInterface
21
{
22
    const CONFIG_DRIVER        = 'driver';
23
    const CONFIG_HOST          = 'host';
24
    const CONFIG_PORT          = 'port';
25
    const CONFIG_USER          = 'user';
26
    const CONFIG_PASSWORD      = 'password';
27
    const CONFIG_DATABASE_NAME = 'dbName';
28
29
    /** @var array */
30
    private $options;
31
32
    /**
33
     * @param array $options
34
     *
35
     * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
36
     */
37
    public function __construct(array $options = [])
38
    {
39
        $this->options = static::getValidator()->validate($options);
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getDriver(): string
46
    {
47
        return $this->options[static::CONFIG_DRIVER];
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getHost(): string
54
    {
55
        return $this->options[static::CONFIG_HOST];
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function getPort(): int
62
    {
63
        return (int) $this->options[static::CONFIG_PORT];
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getUser(): string
70
    {
71
        return $this->options[static::CONFIG_USER];
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getPassword(): string
78
    {
79
        return $this->options[static::CONFIG_PASSWORD];
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getDbName(): string
86
    {
87
        return $this->options['dbName'];
88
    }
89
90
    /**
91
     * @return ConfigValidatorInterface
92
     */
93
    public static function getValidator(): ConfigValidatorInterface
94
    {
95
        return Validate::arr(false)
96
                       ->required(
97
                           'driver',
98
                           v::stringType()->in([ConnectionConfigInterface::DRIVER_MYSQL])
99
                       )
100
                       ->required('user', v::stringType())
101
                       ->required('password', v::stringType())
102
                       ->required('host', v::stringType())
103
                       ->optional('port', v::intVal(), 3306)
104
                       ->optional('dbName', v::stringType());
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getDsn(): string
111
    {
112
        return sprintf(
113
            '%s:dnbame=%s;host=%s;port=%d',
114
            $this->getDriver(),
115
            $this->getDbName(),
116
            $this->getHost(),
117
            $this->getPort()
118
        );
119
    }
120
}
121