Completed
Push — 7.4 ( abf332...310a44 )
by Nikolaos
05:20 queued 01:33
created

Connection::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4.002

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 19
cts 20
cp 0.95
c 0
b 0
f 0
rs 9.2
cc 4
nc 5
nop 6
crap 4.002
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AtlasPHP
12
 *
13
 * @link    https://github.com/atlasphp/Atlas.Pdo
14
 * @license https://github.com/atlasphp/Atlas.Pdo/blob/1.x/LICENSE.md
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\DataMapper\Pdo;
20
21
use InvalidArgumentException;
22
use PDO;
23
use Phalcon\DataMapper\Pdo\Connection\AbstractConnection;
24
use Phalcon\DataMapper\Pdo\Profiler\Profiler;
25
use Phalcon\DataMapper\Pdo\Profiler\ProfilerInterface;
26
27
use function explode;
28
29
/**
30
 * Provides array quoting, profiling, a new `perform()` method, new `fetch*()`
31
 * methods
32
 *
33
 * @property array             $arguments
34
 * @property PDO               $pdo
35
 * @property ProfilerInterface $profiler
36
 */
37
class Connection extends AbstractConnection
38
{
39
    /**
40
     * @var array
41
     */
42
    protected $arguments = [];
43
44
    /**
45
     * Constructor.
46
     *
47
     * This overrides the parent so that it can take connection attributes as a
48
     * constructor parameter, and set them after connection.
49
     *
50
     * @param string            $dsn
51
     * @param string            $username
52
     * @param string            $password
53
     * @param array             $options
54
     * @param array             $queries
55
     * @param ProfilerInterface $profiler
56
     */
57 191
    public function __construct(
58
        string $dsn,
59
        string $username = null,
60
        string $password = null,
61
        array $options = [],
62
        array $queries = [],
63
        ProfilerInterface $profiler = null
64
    ) {
65 191
        $parts     = explode(':', $dsn);
66
        $available = [
67 191
            "mysql"  => true,
68
            "pgsql"  => true,
69
            "sqlite" => true,
70
            "mssql"  => true,
71
        ];
72 191
        if (!isset($available[$parts[0]])) {
73 2
            throw new InvalidArgumentException(
74 2
                "Driver not supported [" . $parts[0] . "]"
75
            );
76
        }
77
78
79
        // if no error mode is specified, use exceptions
80 189
        if (!isset($options[PDO::ATTR_ERRMODE])) {
81 189
            $options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
82
        }
83
84
        // Arguments store
85 189
        $this->arguments = [
86 189
            $dsn,
87 189
            $username,
88 189
            $password,
89 189
            $options,
90 189
            $queries,
91
        ];
92
93
        // Create a new profiler if none has been passed
94 189
        if ($profiler === null) {
95 189
            $profiler = new Profiler();
96
        }
97 189
        $this->setProfiler($profiler);
98
99
        // Quotes
100 189
        $this->quote = $this->getQuoteNames($parts[0]);
0 ignored issues
show
Bug introduced by
The property quote 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...
101 189
    }
102
103
    /**
104
     * The purpose of this method is to hide sensitive data from stack traces.
105
     *
106
     * @return array
107
     */
108 2
    public function __debugInfo()
109
    {
110
        return [
111
            'arguments' => [
112 2
                $this->arguments[0],
113 2
                '****',
114 2
                '****',
115 2
                $this->arguments[3],
116 2
                $this->arguments[4],
117
            ],
118
        ];
119
    }
120
121
    /**
122
     * Connects to the database.
123
     */
124 135
    public function connect(): void
125
    {
126 135
        if (!$this->pdo) {
127
            // connect
128 133
            $this->profiler->start(__FUNCTION__);
129 133
            [$dsn, $username, $password, $options, $queries] = $this->arguments;
0 ignored issues
show
Bug introduced by
The variable $dsn does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $username does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $password does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $options does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $queries does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
130 133
            $this->pdo = new PDO($dsn, $username, $password, $options);
131 133
            $this->profiler->finish();
132
133 133
            foreach ($queries as $query) {
134 1
                $this->exec($query);
135
            }
136
        }
137 135
    }
138
139
    /**
140
     * Disconnects from the database.
141
     */
142 4
    public function disconnect(): void
143
    {
144 4
        $this->profiler->start(__FUNCTION__);
145 4
        $this->pdo = null;
146 4
        $this->profiler->finish();
147 4
    }
148
}
149