Passed
Push — master ( c78efc...84ef81 )
by Darío
01:42
created

AbstractTableGateway::getCurrentConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Db\TableGateway;
12
13
use Drone\Db\Driver\DriverFactory;
14
use Drone\Db\Entity;
15
16
/**
17
 * AbstractTableGateway class
18
 *
19
 * This class stores statically different connections from a TableGateway
20
 */
21
abstract class AbstractTableGateway
22
{
23
    /**
24
     * Entity instance
25
     *
26
     * @var Entity
27
     */
28
    protected $entity;
29
30
    /**
31
     * Driver collector
32
     *
33
     * @var AbstractDriver[]
34
     */
35
    private static $drivers;
36
37
    /**
38
     * Current connection
39
     *
40
     * @var string
41
     */
42
    protected $currentConnection;
43
44
    /**
45
     * Returns the entity
46
     *
47
     * @return Entity
48
     */
49
    public function getEntity()
50
    {
51
        return $this->entity;
52
    }
53
54
    /**
55
     * Returns all registered drivers
56
     *
57
     * @return AbstractDriver[]
58
     */
59
    public static function getDrivers()
60
    {
61
        return self::$drivers;
62
    }
63
64
    /**
65
     * Returns the current connection identifier
66
     *
67
     * @return string
68
     */
69
    public function getCurrentConnection()
70
    {
71
        return $this->currentConnection;
72
    }
73
74
    /**
75
     * Constructor
76
     *
77
     * @param Entity       $entity
78
     * @param array|string $connection
79
     *
80
     * @throws \RuntimeException
81
     */
82
    public function __construct(Entity $entity, $connection)
83
    {
84
        $this->entity = $entity;
85
86
        if (is_string($connection))
87
        {
88
            $this->currentConnection = $connection;
89
            $this->getDriver($connection);
90
        }
91
        else if (is_array($connection))
0 ignored issues
show
introduced by
The condition is_array($connection) is always true.
Loading history...
92
        {
93
            $identifier = key($connection);
94
            $connection_options = $connection[$identifier];
95
96
            $this->currentConnection = $identifier;
97
98
            if (!array_key_exists('driver', $connection_options))
99
                throw new \RuntimeException("The database driver key has not been declared");
100
101
            if (!isset(self::$drivers[$identifier]))
102
                self::$drivers[$identifier] = DriverFactory::create($connection_options);
103
            else
104
                throw new \RuntimeException("The database connection already exists");
105
        }
106
        else
107
            throw new \InvalidArgumentException("Invalid type given. Array or string expected");
108
    }
109
110
    /**
111
     * Returns the a particular connection
112
     *
113
     * @param string
114
     *
115
     * @return AbstractDriver
0 ignored issues
show
Bug introduced by
The type Drone\Db\TableGateway\AbstractDriver was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
116
     *
117
     * @throws \RuntimeException
118
     */
119
    public static function getDriver($identifier)
120
    {
121
        if (!array_key_exists($identifier, self::$drivers))
122
            throw new \RuntimeException("The database connection does not exists");
123
124
        return self::$drivers[$identifier];
125
    }
126
127
    /**
128
     * Returns true if the connection exists
129
     *
130
     * @param string
131
     *
132
     * @return boolean
133
     *
134
     * @throws \RuntimeException
135
     */
136
    public static function hasDriver($identifier)
137
    {
138
        if (!array_key_exists($identifier, self::$drivers))
139
            return false;
140
141
        return true;
142
    }
143
144
    /**
145
     * Returns the current connection driver
146
     *
147
     * @return AbstractDriver
148
     *
149
     * @throws \RuntimeException
150
     */
151
    public function getCurrentDriver()
152
    {
153
        return self::$drivers[$this->currentConnection];
154
    }
155
156
    /**
157
     * Alias for getCurrentDriver()
158
     *
159
     * @return AbstractDriver
160
     *
161
     * @throws \RuntimeException
162
     */
163
    public function getDb()
164
    {
165
        return $this->getCurrentDriver();
166
    }
167
}