Completed
Push — master ( 699dc6...d8f60e )
by Changwan
05:50
created

Manager::__construct()   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\Database\Connection\MysqlConnection;
5
use Wandu\Database\Contracts\ConnectionInterface;
6
use Wandu\Database\Contracts\Entity\MetadataReaderInterface;
7
use Wandu\Database\Exception\DriverNotFoundException;
8
use Wandu\Database\Repository\Repository;
9
10
class Manager
11
{
12
    /** @var \Wandu\Database\Contracts\Entity\MetadataReaderInterface */
13
    protected $reader;
14
    
15
    /** @var \Wandu\Database\Contracts\ConnectionInterface[] */
16
    protected $connections = [];
17
    
18
    /** @var \Wandu\Database\Repository\Repository[] */
19
    protected $repositories = [];
20
21 18
    public function __construct(MetadataReaderInterface $reader)
22
    {
23 18
        $this->reader = $reader;
24 18
    }
25
26
    /**
27
     * @param array|\Wandu\Database\Configuration|\Wandu\Database\Contracts\ConnectionInterface $connection
28
     * @param string $name
29
     * @return \Wandu\Database\Contracts\ConnectionInterface
30
     */
31 18
    public function connect($connection, $name = 'default')
32
    {
33 18
        if (!$connection instanceof Configuration) {
34 18
            $connection = new Configuration($connection);
0 ignored issues
show
Bug introduced by
It seems like $connection defined by new \Wandu\Database\Configuration($connection) on line 34 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...
35
        }
36 18
        if (!$connection instanceof ConnectionInterface) {
37 18
            switch ($connection->getDriver()) {
38 18
                case Configuration::DRIVER_MYSQL:
39 17
                    $connection = new MysqlConnection($connection);
40 17
                    break;
41
                default:
42 1
                    throw new DriverNotFoundException($connection->getDriver());
43
            }
44
        }
45 17
        $connection->connect();
46 17
        return $this->connections[$name] = $connection;
47
    }
48
49
    /**
50
     * @param string $name
51
     * @return \Wandu\Database\Contracts\ConnectionInterface
52
     */
53 14
    public function connection($name = 'default')
54
    {
55 14
        return isset($this->connections[$name]) ? $this->connections[$name] : null;
56
    }
57
58
    /**
59
     * @param string $class
60
     * @param string $connection
61
     * @return \Wandu\Database\Repository\Repository
62
     */
63 14
    public function repository(string $class, string $connection = 'default'): Repository
64
    {
65 14
        $repoName = "{$class}@{$connection}";
66 14
        if (!isset($this->repositories[$repoName])) {
67 14
            $this->repositories[$repoName] = new Repository($this, $this->reader->getMetadataFrom($class));
68
        }
69 14
        return $this->repositories[$repoName];
70
    }
71
}
72