Connector::createConnection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 16
c 2
b 1
f 1
nc 2
nop 3
dl 0
loc 21
rs 9.7333
1
<?php
2
3
namespace Nip\Database\Connectors;
4
5
use PDO;
6
7
/**
8
 * Class Connector
9
 * @package Nip\Database\Connectors
10
 */
11
class Connector
12
{
13
    /**
14
     * The default PDO connection options.
15
     *
16
     * @var array
17
     */
18
    protected $options = [
19
        PDO::ATTR_CASE => PDO::CASE_NATURAL,
20
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
21
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
22
        PDO::ATTR_STRINGIFY_FETCHES => false,
23
        PDO::ATTR_EMULATE_PREPARES => false,
24
    ];
25
26
    /**
27
     * Create a new PDO connection.
28
     *
29
     * @param string $dsn
30
     * @param array $config
31
     * @param array $options
32
     * @return \PDO
33
     *
34
     * @throws \Exception
35
     */
36
    public function createConnection($dsn, array $config, array $options)
37
    {
38
        [$username, $password] = [
39
            $config['username'] ?? null,
40
            $config['password'] ?? null,
41
        ];
42
43
        try {
44
            return $this->createPdoConnection(
45
                $dsn,
46
                $username,
47
                $password,
48
                $options
49
            );
50
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type Nip\Database\Connectors\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
51
            return $this->tryAgainIfCausedByLostConnection(
52
                $e,
53
                $dsn,
54
                $username,
55
                $password,
56
                $options
57
            );
58
        }
59
    }
60
61
    /**
62
     * Create a new PDO connection instance.
63
     *
64
     * @param string $dsn
65
     * @param string $username
66
     * @param string $password
67
     * @param array $options
68
     * @return \PDO
69
     */
70
    protected function createPdoConnection($dsn, $username, $password, $options)
71
    {
72
        if (class_exists(PDOConnection::class) && !$this->isPersistentConnection($options)) {
0 ignored issues
show
Bug introduced by
The type Nip\Database\Connectors\PDOConnection 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...
73
            return new PDOConnection($dsn, $username, $password, $options);
74
        }
75
76
        return new PDO($dsn, $username, $password, $options);
77
    }
78
79
    /**
80
     * Determine if the connection is persistent.
81
     *
82
     * @param array $options
83
     * @return bool
84
     */
85
    protected function isPersistentConnection($options)
86
    {
87
        return isset($options[PDO::ATTR_PERSISTENT]) &&
88
            $options[PDO::ATTR_PERSISTENT];
89
    }
90
91
    /**
92
     * Handle an exception that occurred during connect execution.
93
     *
94
     * @param \Throwable $e
95
     * @param string $dsn
96
     * @param string $username
97
     * @param string $password
98
     * @param array $options
99
     * @return \PDO
100
     *
101
     * @throws \Exception
102
     */
103
    protected function tryAgainIfCausedByLostConnection(Throwable $e, $dsn, $username, $password, $options)
0 ignored issues
show
Bug introduced by
The type Nip\Database\Connectors\Throwable was not found. Did you mean Throwable? If so, make sure to prefix the type with \.
Loading history...
104
    {
105
        if ($this->causedByLostConnection($e)) {
0 ignored issues
show
Bug introduced by
The method causedByLostConnection() does not exist on Nip\Database\Connectors\Connector. ( Ignorable by Annotation )

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

105
        if ($this->/** @scrutinizer ignore-call */ causedByLostConnection($e)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
            return $this->createPdoConnection($dsn, $username, $password, $options);
107
        }
108
109
        throw $e;
110
    }
111
112
    /**
113
     * Get the PDO options based on the configuration.
114
     *
115
     * @param array $config
116
     * @return array
117
     */
118
    public function getOptions(array $config)
119
    {
120
        $options = $config['options'] ?? [];
121
122
        return array_diff_key($this->options, $options) + $options;
123
    }
124
125
    /**
126
     * Get the default PDO connection options.
127
     *
128
     * @return array
129
     */
130
    public function getDefaultOptions()
131
    {
132
        return $this->options;
133
    }
134
135
    /**
136
     * Set the default PDO connection options.
137
     *
138
     * @param array $options
139
     * @return void
140
     */
141
    public function setDefaultOptions(array $options)
142
    {
143
        $this->options = $options;
144
    }
145
}
146