AuraSqlLocatorModule::configure()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4
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\AuraSqlModule\Annotation\AuraSql;
11
use Ray\AuraSqlModule\Annotation\Read;
12
use Ray\AuraSqlModule\Annotation\ReadOnlyConnection;
13
use Ray\AuraSqlModule\Annotation\Write;
14
use Ray\AuraSqlModule\Annotation\WriteConnection;
15
use Ray\Di\AbstractModule;
16
17
class AuraSqlLocatorModule extends AbstractModule
18
{
19
    /**
20
     * @var ConnectionLocatorInterface
21
     */
22
    private $connectionLocator;
23
24
    /**
25
     * @var string[]
26
     */
27
    private $readMethods;
28
29
    /**
30
     * @var string[]
31
     */
32
    private $writeMethods;
33
34 3
    public function __construct(
35
        ConnectionLocatorInterface $connectionLocator,
36
        array $readMethods = [],
37
        array $writeMethods = [],
38
        AbstractModule $module = null
39
    ) {
40 3
        $this->connectionLocator = $connectionLocator;
41 3
        $this->readMethods = $readMethods;
42 3
        $this->writeMethods = $writeMethods;
43 3
        parent::__construct($module);
0 ignored issues
show
Bug introduced by
It seems like $module defined by parameter $module on line 38 can also be of type object<Ray\Di\AbstractModule>; however, Ray\Di\AbstractModule::__construct() does only seem to accept null|object<self>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44 3
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 3
    protected function configure()
50
    {
51 3
        if ((bool) $this->readMethods && (bool) $this->writeMethods) {
52 3
            $this->bind()->annotatedWith(Read::class)->toInstance($this->readMethods);
53 3
            $this->bind()->annotatedWith(Write::class)->toInstance($this->writeMethods);
54
        }
55 3
        if ($this->connectionLocator) {
56 3
            $this->bind(ConnectionLocatorInterface::class)->toInstance($this->connectionLocator);
57
        }
58 3
        $methods = \array_merge($this->readMethods, $this->writeMethods);
59
        // @AuraSql
60 3
        $this->installLocatorDb($methods);
61
        // @ReadOnlyConnection @WriteConnection
62 3
        $this->installReadWriteConnection();
63
        // @Transactional
64 3
        $this->install(new TransactionalModule);
0 ignored issues
show
Documentation introduced by
new \Ray\AuraSqlModule\TransactionalModule() is of type object<Ray\AuraSqlModule\TransactionalModule>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65 3
    }
66
67 3
    protected function installReadWriteConnection()
68
    {
69
        // @ReadOnlyConnection
70 3
        $this->bindInterceptor(
71 3
            $this->matcher->any(),
72 3
            $this->matcher->annotatedWith(ReadOnlyConnection::class),
73 3
            [AuraSqlSlaveDbInterceptor::class]
74
        );
75
        // @WriteConnection
76 3
        $this->bindInterceptor(
77 3
            $this->matcher->any(),
78 3
            $this->matcher->annotatedWith(WriteConnection::class),
79 3
            [AuraSqlMasterDbInterceptor::class]
80
        );
81 3
    }
82
83
    /**
84
     * @param string[] $methods
85
     */
86 3
    private function installLocatorDb(array $methods)
87
    {
88
        // locator db
89 3
        $this->bindInterceptor(
90 3
            $this->matcher->annotatedWith(AuraSql::class), // @AuraSql in class
91 3
            $this->matcher->logicalAnd(
92 3
                new IsInMethodMatcher($methods),
93 3
                $this->matcher->logicalNot(
94 3
                    $this->matcher->annotatedWith(ReadOnlyConnection::class)
95
                ),
96 3
                $this->matcher->logicalNot(
97 3
                    $this->matcher->annotatedWith(Connection::class)
98
                )
99
            ),
100 3
            [AuraSqlConnectionInterceptor::class]
101
        );
102 3
    }
103
}
104