| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Ray\AuraSqlModule; |
||
| 6 | |||
| 7 | use Aura\Sql\ExtendedPdo; |
||
| 8 | use SensitiveParameter; |
||
|
0 ignored issues
–
show
|
|||
| 9 | |||
| 10 | use function array_rand; |
||
| 11 | use function explode; |
||
| 12 | use function getenv; |
||
| 13 | use function preg_match; |
||
| 14 | use function sprintf; |
||
| 15 | |||
| 16 | final class EnvConnection |
||
| 17 | { |
||
| 18 | /** @var array<ExtendedPdo> */ |
||
| 19 | private static array $pdo = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @phpstan-param array<string> $options |
||
| 23 | * @phpstan-param array<string> $queries |
||
| 24 | */ |
||
| 25 | public function __construct( |
||
| 26 | private readonly string $dsn, |
||
| 27 | private readonly ?string $slave, |
||
| 28 | private readonly string $username = '', |
||
| 29 | #[SensitiveParameter] |
||
| 30 | private readonly string $password = '', |
||
| 31 | /** @var array<string> */ |
||
| 32 | private readonly array $options = [], |
||
| 33 | /** @var array<string> */ |
||
| 34 | private readonly array $queries = [] |
||
| 35 | ) { |
||
| 36 | } |
||
| 37 | |||
| 38 | public function __invoke(): ExtendedPdo |
||
| 39 | { |
||
| 40 | $dsn = $this->getDsn(); |
||
| 41 | if (isset(self::$pdo[$dsn])) { |
||
| 42 | return self::$pdo[$dsn]; |
||
| 43 | } |
||
| 44 | |||
| 45 | self::$pdo[$dsn] = new ExtendedPdo( |
||
| 46 | $dsn, |
||
| 47 | (string) getenv($this->username), |
||
| 48 | (string) getenv($this->password), |
||
| 49 | $this->options, |
||
| 50 | $this->queries, |
||
| 51 | ); |
||
| 52 | |||
| 53 | return self::$pdo[$dsn]; |
||
| 54 | } |
||
| 55 | |||
| 56 | private function getDsn(): string |
||
| 57 | { |
||
| 58 | // write |
||
| 59 | if ($this->slave === null) { |
||
| 60 | return (string) getenv($this->dsn); |
||
| 61 | } |
||
| 62 | |||
| 63 | // random read |
||
| 64 | $slaveList = explode(',', (string) getenv($this->slave)); |
||
| 65 | $slave = $slaveList[array_rand($slaveList)]; |
||
| 66 | |||
| 67 | return $this->changeHost((string) getenv($this->dsn), $slave); |
||
| 68 | } |
||
| 69 | |||
| 70 | private function changeHost(string $dsn, string $host): string |
||
| 71 | { |
||
| 72 | preg_match(AuraSqlModule::PARSE_PDO_DSN_REGEX, $dsn, $parts); |
||
|
0 ignored issues
–
show
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 73 | if (! $parts) { |
||
| 74 | // @codeCoverageIgnoreStart |
||
| 75 | return $dsn; |
||
| 76 | // @codeCoverageIgnoreEnd |
||
| 77 | } |
||
| 78 | |||
| 79 | return sprintf('%s:%s=%s;%s', $parts[1], $parts[2], $host, $parts[3]); |
||
| 80 | } |
||
| 81 | } |
||
| 82 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths