AuraSqlReplicationDbProvider::setContext()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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