Completed
Branch master (e3a0da)
by
unknown
15:36
created

MySqlConnection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
namespace Opeyemiabiodun\PotatoORM\Connections;
4
5
use PDO;
6
use PDOException;
7
use InvalidArgumentException;
8
use Opeyemiabiodun\PotatoORM\Connections\Connection;
9
use Opeyemiabiodun\PotatoORM\Connections\LoadEnvVariablesTrait;
10
use Opeyemiabiodun\PotatoORM\Connections\DatabaseTransactionsTrait;
11
12
final class MySqlConnection implements Connection
13
{
14
    use LoadEnvVariablesTrait, DatabaseTransactionsTrait;
15
16
    /**
17
     * The method called in the constructor.
18
     *
19
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
20
     */
21
    private function __construct()
22
    {
23
        $this->useDbEnv();
24
25
        $dsn = 'mysql:host='.$this->_host
26
                .';port='.$this->_port
27
                .';dbname='.$this->_database;
28
29
        try {
30
            $this->_pdo = new PDO($dsn, $this->_username, $this->_password);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \PDO($dsn, $this->_u...name, $this->_password) of type object<PDO> is incompatible with the declared type object<Opeyemiabiodun\PotatoORM\Connections\PDO> of property $_pdo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
        } catch (PDOException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
32
        }
33
    }
34
35
    /**
36
     * Returns the columns of a table.
37
     *
38
     * @param string $table The table inspected for its columns.
39
     *
40
     * @return array The columns of the table.        
41
     */
42 View Code Duplication
    public function getColumns($table)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        if (gettype($table) !== 'string') {
45
            throw new InvalidArgumentException("The parameter {$table} is not an string. A string is required instead.");
46
        }
47
48
        return $this->getPdo()->query("SELECT COLUMN_NAME
49
                                        FROM INFORMATION_SCHEMA.COLUMNS
50
                                        WHERE TABLE_NAME = N'{$table}' 
51
                                        AND TABLE_SCHEMA = N'{$this->_database}'")->fetchAll();
52
    }
53
54
    /**
55
     * Returns the Connection's PDO.
56
     *
57
     * @return PDO PHP Data Objects
58
     */
59
    public function getPdo()
60
    {
61
        return $this->_pdo;
62
    }
63
64
    /**
65
     * Returns the primary key of a table.
66
     *
67
     * @param string $table The table inspected for its primary key.
68
     *
69
     * @return string The primary key of the table.
70
     */
71 View Code Duplication
    public function getPrimaryKey($table)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        if (gettype($table) !== 'string') {
74
            throw new InvalidArgumentException("The parameter {$table} is not an string. A string is required instead.");
75
        }
76
77
        return $this->getPdo()->query("SHOW KEYS FROM {$table} WHERE Key_name = 'PRIMARY'")->fetchAll()[0]['Column_name'];
78
    }
79
80
    public static function load()
81
    {
82
        return new MySqlConnection();
83
    }
84
}
85