Passed
Pull Request — 2.x (#83)
by Maxim
36:42 queued 16:48
created

SocketConnectionConfig::__set_state()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cycle Database package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Config\MySQL;
13
14
use Cycle\Database\Config\ProvidesSourceString;
15
16
class SocketConnectionConfig extends ConnectionConfig implements ProvidesSourceString
17
{
18
    /**
19
     * @param non-empty-string $socket
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
20
     * @param non-empty-string $database
21
     * @param non-empty-string|null $charset
22
     * @param non-empty-string|null $user
23
     * @param non-empty-string|null $password
24
     * @param array<int, non-empty-string|non-empty-string> $options
25
     */
26
    public function __construct(
27
        public string $database,
28
        public string $socket,
29
        public ?string $charset = null,
30
        ?string $user = null,
31
        ?string $password = null,
32
        array $options = []
33
    ) {
34
        parent::__construct($user, $password, $options);
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function getSourceString(): string
41
    {
42
        return $this->database;
43
    }
44
45
    /**
46
     * Returns the MySQL-specific PDO DataSourceName with connection Unix socket,
47
     * that looks like:
48
     * <code>
49
     *  mysql:unix_socket=/tmp/mysql.sock;dbname=dbname
50
     * </code>
51
     *
52
     * {@inheritDoc}
53
     */
54
    public function getDsn(): string
55
    {
56
        $config = [
57
            'unix_socket' => $this->socket,
58
            'dbname' => $this->database,
59
            'charset' => $this->charset,
60
        ];
61
62
        return \sprintf('%s:%s', $this->getName(), $this->dsn($config));
63
    }
64
65
    public static function __set_state(array $state): self
66
    {
67
        return new self(...$state);
68
    }
69
}
70