HasConnections::connection()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 11
c 2
b 0
f 1
nc 4
nop 1
dl 0
loc 20
rs 9.9
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)) {
0 ignored issues
show
introduced by
The condition is_array($connectionName) is always false.
Loading history...
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() || !Container::getInstance()->has('config')) {
75
            return 'main';
76
        }
77
78
        if (Container::getInstance() && config()->has('database.default')) {
79
            return config()->get('database.default');
80
        }
81
82
        return 'main';
83
    }
84
85
    /**
86
     * Parse the connection into an array of the name and read / write type.
87
     *
88
     * @param string $name
89
     * @return string
90
     */
91
    protected function parseConnectionName($name)
92
    {
93
        $name = $name ?: $this->getDefaultConnection();
94
95
        return $name;
96
    }
97
98
    /**
99
     * Prepare the database connection instance.
100
     *
101
     * @param  Connection $connection
102
     * @param  string $type
103
     * @return Connection
104
     */
105
    protected function configure(Connection $connection, $type)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
    protected function configure(Connection $connection, /** @scrutinizer ignore-unused */ $type)

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

Loading history...
106
    {
107
//        $connection = $this->setPdoForType($connection, $type);
108
        // First we'll set the fetch mode and a few other dependencies of the database
109
        // connection. This method basically just configures and prepares it to get
110
        // used by the application. Once we're finished we'll return it back out.
111
//        if ($this->app->bound('events')) {
112
//            $connection->setEventDispatcher($this->app['events']);
113
//        }
114
        // Here we'll set a reconnector callback. This reconnector can be any callable
115
        // so we will set a Closure to reconnect from this manager with the name of
116
        // the connection, which will allow us to reconnect from the connections.
117
//        $connection->setReconnector(function ($connection) {
118
//            $this->reconnect($connection->getName());
119
//        });
120
        return $connection;
121
    }
122
123
    /**
124
     * Make the database connection instance.
125
     *
126
     * @param  string $name
127
     * @return Connection
128
     */
129
    protected function makeConnection($name)
130
    {
131
        $config = $this->configuration($name);
132
133
        // First we will check by the connection name to see if an extension has been
134
        // registered specifically for that connection. If it has we will call the
135
        // Closure and pass it the config allowing it to resolve the connection.
136
//        if (isset($this->extensions[$name])) {
137
//            return call_user_func($this->extensions[$name], $config, $name);
138
//        }
139
140
        // Next we will check to see if an extension has been registered for a driver
141
        // and will call the Closure if so, which allows us to have a more generic
142
        // resolver for the drivers themselves which applies to all connections.
143
//        if (isset($this->extensions[$driver = $config['driver']])) {
144
//            return call_user_func($this->extensions[$driver], $config, $name);
145
//        }
146
        return $this->factory->make($config, $name);
147
    }
148
149
    /**
150
     * Get the configuration for a connection.
151
     *
152
     * @param  string $name
153
     * @return array
154
     *
155
     * @throws \InvalidArgumentException
156
     */
157
    protected function configuration($name)
158
    {
159
        $name = $name ?: $this->getDefaultConnection();
160
161
        // To get the database connection configuration, we will just pull each of the
162
        // connection configurations and get the configurations for the given name.
163
        // If the configuration doesn't exist, we'll throw an exception and bail.
164
165
        $connections = config('database.connections');
166
        if (is_null($config = Arr::get($connections, $name))) {
167
            throw new InvalidArgumentException("Database [$name] not configured.");
168
        }
169
170
        if ($config['user']) {
171
            $config['username'] = $config['user'];
172
        }
173
174
        if ($config['name']) {
175
            $config['database'] = $config['name'];
176
        }
177
178
        return $config;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $config also could return the type Nip\Config\Config which is incompatible with the documented return type array.
Loading history...
179
    }
180
}
181