AuraSqlReplicationDbProvider::get()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3
1
<?php
2
/**
3
 * This file is part of the Ray.AuraSqlModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\AuraSqlModule;
8
9
use Aura\Sql\ConnectionLocatorInterface;
10
use Ray\Di\InjectorInterface;
11
use Ray\Di\ProviderInterface;
12
use Ray\Di\SetContextInterface;
13
14
class AuraSqlReplicationDbProvider implements ProviderInterface, SetContextInterface
15
{
16
    /**
17
     * @var InjectorInterface
18
     */
19
    private $injector;
20
21
    /**
22
     * @var string
23
     */
24
    private $context = '';
25
26 7
    public function __construct(InjectorInterface $injector)
27
    {
28 7
        $this->injector = $injector;
29 7
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 7
    public function setContext($context)
35
    {
36 7
        $this->context = $context;
37 7
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 7
    public function get()
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
43
    {
44 7
        $connectionLocator = $this->injector->getInstance(ConnectionLocatorInterface::class, $this->context);
45 7
        $isGetRequest = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET';
46 7
        $pdo = $isGetRequest ? $connectionLocator->getRead() : $connectionLocator->getWrite();
47
48 7
        return $pdo;
49
    }
50
}
51