GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DatabaseManager::initNewConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 2
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Database;
4
5
use InvalidArgumentException;
6
use Nip\Application;
7
use Nip\Database\Connections\Connection;
8
use Nip\Database\Connections\ConnectionFactory;
9
use Nip_Helper_Arrays;
10
11
/**
12
 * Class DatabaseManager
13
 * @package Nip\Database
14
 */
15
class DatabaseManager
16
{
17
18
    /**
19
     * @var Application
20
     */
21
    protected $application;
22
23
    /**
24
     * The database connection factory instance.
25
     *
26
     * @var ConnectionFactory
27
     */
28
    protected $factory;
29
30
    protected $connections = [];
31
32
    /**
33
     * DatabaseManager constructor.
34
     * @param Application $application
35
     * @param ConnectionFactory $factory
36
     */
37
    public function __construct(Application $application, ConnectionFactory $factory)
38
    {
39
        $this->application = $application;
40
        $this->factory = $factory;
41
    }
42
43
    /**
44
     * Get a database connection instance.
45
     *
46
     * @param  string $name
47
     * @return Connection
48
     */
49
    public function connection($name = null)
50
    {
51
        list($database, $type) = $this->parseConnectionName($name);
52
        $name = $name ?: $database;
53
54
        // If we haven't created this connection, we'll create it based on the config
55
        // provided in the application. Once we've created the connections we will
56
        // set the "fetch mode" for PDO which determines the query return types.
57
        if (!isset($this->connections[$name])) {
58
            $this->connections[$name] = $this->configure(
59
                $connection = $this->makeConnection($name),
60
                $type
61
            );
62
        }
63
        return $this->connections[$name];
64
    }
65
66
    /**
67
     * Parse the connection into an array of the name and read / write type.
68
     *
69
     * @param  string $name
70
     * @return string
71
     */
72
    protected function parseConnectionName($name)
73
    {
74
        $name = $name ?: $this->getDefaultConnection();
75
        return $name;
76
    }
77
78
    /**
79
     * Get the default connection name.
80
     *
81
     * @return string
82
     */
83
    public function getDefaultConnection()
84
    {
85
        return $this->application->get('config')->get('database.default');
86
    }
87
88
    /**
89
     * Prepare the database connection instance.
90
     *
91
     * @param  Connection $connection
92
     * @param  string $type
93
     * @return Connection
94
     */
95
    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...
96
    {
97
//        $connection = $this->setPdoForType($connection, $type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
        // First we'll set the fetch mode and a few other dependencies of the database
99
        // connection. This method basically just configures and prepares it to get
100
        // used by the application. Once we're finished we'll return it back out.
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
//        if ($this->app->bound('events')) {
102
//            $connection->setEventDispatcher($this->app['events']);
103
//        }
104
        // Here we'll set a reconnector callback. This reconnector can be any callable
105
        // so we will set a Closure to reconnect from this manager with the name of
106
        // the connection, which will allow us to reconnect from the connections.
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
//        $connection->setReconnector(function ($connection) {
108
//            $this->reconnect($connection->getName());
109
//        });
110
        return $connection;
111
    }
112
113
    /**
114
     * Make the database connection instance.
115
     *
116
     * @param  string $name
117
     * @return Connection
118
     */
119
    protected function makeConnection($name)
120
    {
121
        $config = $this->configuration($name);
122
123
        // First we will check by the connection name to see if an extension has been
124
        // registered specifically for that connection. If it has we will call the
125
        // Closure and pass it the config allowing it to resolve the connection.
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
126
//        if (isset($this->extensions[$name])) {
127
//            return call_user_func($this->extensions[$name], $config, $name);
128
//        }
129
130
        // Next we will check to see if an extension has been registered for a driver
131
        // and will call the Closure if so, which allows us to have a more generic
132
        // resolver for the drivers themselves which applies to all connections.
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
133
//        if (isset($this->extensions[$driver = $config['driver']])) {
134
//            return call_user_func($this->extensions[$driver], $config, $name);
135
//        }
136
        return $this->factory->make($config, $name);
137
    }
138
139
    /**
140
     * Get the configuration for a connection.
141
     *
142
     * @param  string $name
143
     * @return array
144
     *
145
     * @throws \InvalidArgumentException
146
     */
147
    protected function configuration($name)
148
    {
149
        $name = $name ?: $this->getDefaultConnection();
150
151
        // To get the database connection configuration, we will just pull each of the
152
        // connection configurations and get the configurations for the given name.
153
        // If the configuration doesn't exist, we'll throw an exception and bail.
154
155
        $connections = config('database.connections');
156
        if (is_null($config = Nip_Helper_Arrays::get($connections, $name))) {
157
            throw new InvalidArgumentException("Database [$name] not configured.");
158
        }
159
        return $config;
160
    }
161
162
    /**
163
     * @param $config
164
     * @return Connection
165
     */
166
    public function newConnectionFromConfig($config)
167
    {
168
        $connection = $this->createNewConnection(
0 ignored issues
show
Bug introduced by
The method createNewConnection() does not exist on Nip\Database\DatabaseManager. Did you maybe mean connection()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
169
            $config->adapter,
170
            $config->host,
171
            $config->user,
172
            $config->password,
173
            $config->name
174
        );
175
        return $connection;
176
    }
177
178
    /**
179
     * @return array
180
     */
181
    public function getConnections(): array
182
    {
183
        return $this->connections;
184
    }
185
186
    /**
187
     * @return Connection
188
     */
189
    public function newConnection()
190
    {
191
        return new \Nip\Database\Connection();
192
    }
193
194
    /**
195
     * @param $connection
196
     */
197
    public function initNewConnection($connection)
0 ignored issues
show
Unused Code introduced by
The parameter $connection 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...
198
    {
199
//        if ($this->getBootstrap()->getDebugBar()->isEnabled()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
200
//            $this->getBootstrap()->getDebugBar()->initDatabaseAdapter($connection->getAdapter());
201
//        }
202
    }
203
204
//    /**
205
//     * @return Bootstrap
206
//     */
207
//    public function getBootstrap()
208
//    {
209
//        return $this->bootstrap;
210
//    }
211
//
212
//    /**
213
//     * @param Bootstrap $bootstrap
214
//     */
215
//    public function setBootstrap($bootstrap)
216
//    {
217
//        $this->bootstrap = $bootstrap;
218
//    }
219
}
220