Completed
Branch master (e4c723)
by
unknown
15:38
created

SqliteConnection::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Opeyemiabiodun\PotatoORM\Connections;
4
5
use PDO;
6
use PDOException;
7
use RuntimeException;
8
use InvalidArgumentException;
9
use Opeyemiabiodun\PotatoORM\Connections\Connection;
10
use Opeyemiabiodun\PotatoORM\Connections\DatabaseTransactionsTrait;
11
12
final class SqliteConnection implements Connection
13
{
14
    use DatabaseTransactionsTrait;
15
16
    /**
17
     * $_pdo The PDO instance of the connection.
18
     *
19
     * @var PDO
20
     */
21
    private $_pdo;
22
23
    /**
24
     * The method called in the constructor.
25
     *
26
     * @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...
27
     */
28
    private function __construct()
29
    {
30
        $dsn = "sqlite:../../testdb.sq3";
31
32
        try {
33
            $this->_pdo = new PDO($dsn);
34
        } catch (PDOException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
35
        }
36
    }
37
38
    /**
39
     * Returns the columns of a table.
40
     *
41
     * @param string $table The table inspected for its columns.
42
     *
43
     * @return array The columns of the table.        
44
     */
45 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...
46
    {
47
        if (gettype($table) !== 'string') {
48
            throw new InvalidArgumentException("The parameter {$table} is not an string. A string is required instead.");
49
        }
50
51
        var_dump($this->getPdo()->query("pragma table_info({$table})")->fetchAll());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->getPdo()...table})")->fetchAll()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
52
53
        //return $this->getPdo()->query("pragma table_info(table_name)")->fetchAll();
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
54
    }
55
56
    /**
57
     * Returns the Connection's PDO.
58
     *
59
     * @return PDO PHP Data Objects
60
     */
61
    public function getPdo()
62
    {
63
        return $this->_pdo;
64
    }
65
66
    /**
67
     * Returns the primary key of a table.
68
     *
69
     * @param string $table The table inspected for its primary key.
70
     *
71
     * @return string The primary key of the table.
72
     */
73
    public function getPrimaryKey($table)
74
    {
75
        if (gettype($table) !== 'string') {
76
            throw new InvalidArgumentException("The parameter {$table} is not an string. A string is required instead.");
77
        }
78
79
        //return $this->getPdo()->query("SHOW KEYS FROM {$table} WHERE Key_name = 'PRIMARY'")->fetchAll()[0]['Column_name'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
83% 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...
80
    }
81
82
    public static function load()
83
    {
84
        return new SqliteConnection();
85
    }
86
}
87