Completed
Push — master ( d8f60e...a72876 )
by Changwan
03:40
created

Manager::setEventDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database;
3
4
use Wandu\Caster\Caster;
5
use Wandu\Caster\CastManagerInterface;
6
use Wandu\Database\Connection\MysqlConnection;
7
use Wandu\Database\Contracts\ConnectionInterface;
8
use Wandu\Database\Contracts\Entity\MetadataReaderInterface;
9
use Wandu\Database\Exception\DriverNotFoundException;
10
use Wandu\Database\Repository\Repository;
11
use Wandu\Event\DispatcherInterface;
12
13
class Manager
14
{
15
    /** @var \Wandu\Database\Contracts\Entity\MetadataReaderInterface */
16
    protected $reader;
17
    
18
    /** @var \Wandu\Caster\Caster */
19
    protected $caster;
20
    
21
    /** @var \Wandu\Event\DispatcherInterface */
22
    protected $dispatcher;
23
    
24
    /** @var \Wandu\Database\Contracts\ConnectionInterface[] */
25
    protected $connections = [];
26
    
27
    /** @var \Wandu\Database\Repository\Repository[] */
28
    protected $repositories = [];
29
30 20
    public function __construct(
31
        MetadataReaderInterface $reader,
32
        CastManagerInterface $caster = null
33
    ) {
34 20
        $this->reader = $reader;
35 20
        $this->caster = $caster ?: new Caster();
0 ignored issues
show
Documentation Bug introduced by
$caster ?: new \Wandu\Caster\Caster() is of type object<Wandu\Caster\CastManagerInterface>, but the property $caster was declared to be of type object<Wandu\Caster\Caster>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
36 20
    }
37
38
    /**
39
     * @param \Wandu\Event\DispatcherInterface $dispatcher
40
     */
41 18
    public function setEventDispatcher(DispatcherInterface $dispatcher)
42
    {
43 18
        $this->dispatcher = $dispatcher;
44 18
    }
45
    
46
    /**
47
     * @param array|\Wandu\Database\Configuration|\Wandu\Database\Contracts\ConnectionInterface $connection
48
     * @param string $name
49
     * @return \Wandu\Database\Contracts\ConnectionInterface
50
     */
51 20
    public function connect($connection, $name = 'default')
52
    {
53 20
        if (!$connection instanceof Configuration) {
54 20
            $connection = new Configuration($connection);
0 ignored issues
show
Bug introduced by
It seems like $connection defined by new \Wandu\Database\Configuration($connection) on line 54 can also be of type object<Wandu\Database\Co...ts\ConnectionInterface>; however, Wandu\Database\Configuration::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55
        }
56 20
        if (!$connection instanceof ConnectionInterface) {
57 20
            switch ($connection->getDriver()) {
58 20
                case Configuration::DRIVER_MYSQL:
59 19
                    $connection = new MysqlConnection($connection, $this->dispatcher);
60 19
                    break;
61
                default:
62 1
                    throw new DriverNotFoundException($connection->getDriver());
63
            }
64
        }
65 19
        $connection->connect();
66 19
        return $this->connections[$name] = $connection;
67
    }
68
69
    /**
70
     * @param string $name
71
     * @return \Wandu\Database\Contracts\ConnectionInterface
72
     */
73 16
    public function connection($name = 'default')
74
    {
75 16
        return isset($this->connections[$name]) ? $this->connections[$name] : null;
76
    }
77
78
    /**
79
     * @param string $class
80
     * @param string $connection
81
     * @return \Wandu\Database\Repository\Repository
82
     */
83 16
    public function repository(string $class, string $connection = 'default'): Repository
84
    {
85 16
        $repoName = "{$class}@{$connection}";
86 16
        if (!isset($this->repositories[$repoName])) {
87 16
            $this->repositories[$repoName] = new Repository($this, $this->reader->getMetadataFrom($class), $this->caster);
88
        }
89 16
        return $this->repositories[$repoName];
90
    }
91
}
92