ConnectionLocatorFactory::fromInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 6
dl 0
loc 19
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ConnectionLocator;
8
9
use function explode;
10
use function preg_match;
11
use function sprintf;
12
13
final class ConnectionLocatorFactory
14
{
15
    /** @codeCoverageIgnore */
16
    private function __construct()
17
    {
18
    }
19
20
    /**
21
     * @param array<string> $options
22
     * @param array<string> $queries
23
     */
24
    public static function fromInstance(
25
        string $dsn,
26
        string $user,
27
        string $password,
28
        string $slave,
29
        array $options,
30
        array $queries
31
    ): ConnectionLocator {
32
        $writes = ['master' => new Connection($dsn, $user, $password, $options, $queries)];
33
        $i = 1;
34
        $slaves = explode(',', $slave);
35
        $reads = [];
36
        foreach ($slaves as $host) {
37
            $slaveDsn = self::changeHost($dsn, $host);
38
            $name = 'slave' . (string) $i++;
39
            $reads[$name] = new Connection($slaveDsn, $user, $password, $options, $queries);
40
        }
41
42
        return new ConnectionLocator(null, $reads, $writes);
43
    }
44
45
    /**
46
     * @param array<string> $options
47
     * @param array<string> $queries
48
     */
49
    public static function fromEnv(
50
        string $dsn,
51
        string $username,
52
        string $password,
53
        string $slave,
54
        array $options,
55
        array $queries
56
    ): ConnectionLocator {
57
        $writes = ['master' => new EnvConnection($dsn, null, $username, $password, $options, $queries)];
58
        $reads = ['slave' => new EnvConnection($dsn, $slave, $username, $password, $options, $queries)];
59
60
        return new ConnectionLocator(null, $reads, $writes);
61
    }
62
63
    private static function changeHost(string $dsn, string $host): string
64
    {
65
        preg_match(AuraSqlModule::PARSE_PDO_DSN_REGEX, $dsn, $parts);
0 ignored issues
show
Bug introduced by
The type Ray\AuraSqlModule\AuraSqlModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
        if (! $parts) {
67
            // @codeCoverageIgnoreStart
68
            return $dsn;
69
            // @codeCoverageIgnoreEnd
70
        }
71
72
        return sprintf('%s:%s=%s;%s', $parts[1], $parts[2], $host, $parts[3]);
73
    }
74
}
75