AuraSqlReplicationDbProvider::get()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 4
c 2
b 1
f 1
nc 4
nop 0
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 3
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 7
     * @param string $context
27
     */
28 7
    #[Override]
29 7
    public function setContext($context): void
30
    {
31
        $this->context = $context;
32
    }
33
34 7
    /**
35
     * {@inheritDoc}
36 7
     */
37 7
    #[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 7
43
        return $isGetRequest ? $connectionLocator->getRead() : $connectionLocator->getWrite();
44 7
    }
45
}
46