Test Failed
Push — master ( b6dff7...455f41 )
by Gabriel
12:43 queued 11s
created

HasConnections   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 163
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A connection() 0 21 4
A setConnection() 0 4 1
A getConnections() 0 4 1
A getDefaultConnection() 0 12 4
A parseConnectionName() 0 6 2
A configure() 0 17 1
A makeConnection() 0 19 1
A configuration() 0 23 5
1
<?php
2
3
namespace Nip\Database\Manager;
4
5
use InvalidArgumentException;
6
use Nip\Container\Container;
7
use Nip\Database\Connections\Connection;
8
use Nip\Utility\Arr;
9
10
/**
11
 * Trait HasConnections
12
 * @package Nip\Database\Manager
13
 */
14
trait HasConnections
15
{
16
    protected $connections = [];
17
18
    /**
19
     * Get a database connection instance.
20
     *
21
     * @param string $name
22
     * @return Connection
23
     */
24
    public function connection($name = null)
25
    {
26
        $connectionName = $this->parseConnectionName($name);
27
        if (is_array($connectionName)) {
28
            list($database, $type) = $connectionName;
29
        } else {
30
            $database = $connectionName;
31
            $type = null;
32
        }
33
        $name = $name ?: $database;
34
35
        // If we haven't created this connection, we'll create it based on the config
36
        // provided in the application. Once we've created the connections we will
37
        // set the "fetch mode" for PDO which determines the query return types.
38
        if (!isset($this->connections[$name])) {
39
            $connection = $this->configure($this->makeConnection($name), $type);
40
            $this->setConnection($connection, $name);
41
        }
42
43
        return $this->connections[$name];
44
    }
45
46
    /**
47
     * @param Connection $connection
48
     * @param string $name
49
     */
50
    public function setConnection($connection, $name)
51
    {
52
        $this->connections[$name] = $connection;
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getConnections(): array
59
    {
60
        return $this->connections;
61
    }
62
63
    /**
64
     * Get the default connection name.
65
     *
66
     * @return string
67
     */
68
    public function getDefaultConnection()
69
    {
70
        if (!function_exists('config')) {
71
            return 'main';
72
        }
73
74
        if (Container::getInstance() && config()->has('database.default')) {
75
            return config()->get('database.default');
76
        }
77
78
        return 'main';
79
    }
80
81
    /**
82
     * Parse the connection into an array of the name and read / write type.
83
     *
84
     * @param string $name
85
     * @return string
86
     */
87
    protected function parseConnectionName($name)
88
    {
89
        $name = $name ?: $this->getDefaultConnection();
90
91
        return $name;
92
    }
93
94
    /**
95
     * Prepare the database connection instance.
96
     *
97
     * @param  Connection $connection
98
     * @param  string $type
99
     * @return Connection
100
     */
101
    protected function configure(Connection $connection, $type)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
//        $connection = $this->setPdoForType($connection, $type);
104
        // First we'll set the fetch mode and a few other dependencies of the database
105
        // connection. This method basically just configures and prepares it to get
106
        // used by the application. Once we're finished we'll return it back out.
107
//        if ($this->app->bound('events')) {
108
//            $connection->setEventDispatcher($this->app['events']);
109
//        }
110
        // Here we'll set a reconnector callback. This reconnector can be any callable
111
        // so we will set a Closure to reconnect from this manager with the name of
112
        // the connection, which will allow us to reconnect from the connections.
113
//        $connection->setReconnector(function ($connection) {
114
//            $this->reconnect($connection->getName());
115
//        });
116
        return $connection;
117
    }
118
119
    /**
120
     * Make the database connection instance.
121
     *
122
     * @param  string $name
123
     * @return Connection
124
     */
125
    protected function makeConnection($name)
126
    {
127
        $config = $this->configuration($name);
128
129
        // First we will check by the connection name to see if an extension has been
130
        // registered specifically for that connection. If it has we will call the
131
        // Closure and pass it the config allowing it to resolve the connection.
132
//        if (isset($this->extensions[$name])) {
133
//            return call_user_func($this->extensions[$name], $config, $name);
134
//        }
135
136
        // Next we will check to see if an extension has been registered for a driver
137
        // and will call the Closure if so, which allows us to have a more generic
138
        // resolver for the drivers themselves which applies to all connections.
139
//        if (isset($this->extensions[$driver = $config['driver']])) {
140
//            return call_user_func($this->extensions[$driver], $config, $name);
141
//        }
142
        return $this->factory->make($config, $name);
0 ignored issues
show
Bug introduced by
The property factory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
143
    }
144
145
    /**
146
     * Get the configuration for a connection.
147
     *
148
     * @param  string $name
149
     * @return array
150
     *
151
     * @throws \InvalidArgumentException
152
     */
153
    protected function configuration($name)
154
    {
155
        $name = $name ?: $this->getDefaultConnection();
156
157
        // To get the database connection configuration, we will just pull each of the
158
        // connection configurations and get the configurations for the given name.
159
        // If the configuration doesn't exist, we'll throw an exception and bail.
160
161
        $connections = config('database.connections');
162
        if (is_null($config = Arr::get($connections, $name))) {
163
            throw new InvalidArgumentException("Database [$name] not configured.");
164
        }
165
166
        if ($config['user']) {
167
            $config['username'] = $config['user'];
168
        }
169
170
        if ($config['name']) {
171
            $config['database'] = $config['name'];
172
        }
173
174
        return $config;
175
    }
176
}
177