AuraSqlReplicationDbProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 8
c 3
b 1
f 1
dl 0
loc 29
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get() 0 7 3
A setContext() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ConnectionLocatorInterface;
8
use Aura\Sql\ExtendedPdoInterface;
9
use Override;
10
use Ray\Di\InjectorInterface;
11
use Ray\Di\ProviderInterface;
12
use Ray\Di\SetContextInterface;
13
14
/** @implements ProviderInterface<ExtendedPdoInterface> */
15
final class AuraSqlReplicationDbProvider implements ProviderInterface, SetContextInterface
16
{
17
    private string $context = '';
18
19
    public function __construct(private readonly InjectorInterface $injector)
20
    {
21
    }
22
23
    /**
24
     * {@inheritDoc}
25
     *
26
     * @param string $context
27
     */
28
    #[Override]
29
    public function setContext($context): void
30
    {
31
        $this->context = $context;
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    #[Override]
38
    public function get(): ExtendedPdoInterface
39
    {
40
        $connectionLocator = $this->injector->getInstance(ConnectionLocatorInterface::class, $this->context);
41
        $isGetRequest = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET';
42
43
        return $isGetRequest ? $connectionLocator->getRead() : $connectionLocator->getWrite();
44
    }
45
}
46