Completed
Push — master ( 4934e2...fe7b53 )
by Sébastien
01:56
created

PdoMysqlAdapter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 75
ccs 24
cts 32
cp 0.75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A quoteValue() 0 4 1
A execute() 0 14 4
B query() 0 19 5
1
<?php
2
3
namespace Soluble\DbWrapper\Adapter;
4
5
use Soluble\DbWrapper\Exception;
6
use ArrayObject;
7
use PDO;
8
9
class PdoMysqlAdapter implements AdapterInterface
10
{
11
    use Mysql\MysqlCommonTrait;
12
13
    /**
14
     *
15
     * @var PDO
16
     */
17
    protected $connection;
18
19
20
    /**
21
     * Constructor
22
     *
23
     * @throws Exception\InvalidArgumentException
24
     * @param PDO $connection
25
     */
26 5
    public function __construct(PDO $connection)
27
    {
28 5
        if ($connection->getAttribute(\PDO::ATTR_DRIVER_NAME) != 'mysql') {
29
            $msg = __CLASS__ . " requires pdo connection to be 'mysql'";
0 ignored issues
show
Unused Code introduced by
$msg is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
        }
31 5
        $this->connection = $connection;
32 5
    }
33
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function quoteValue($value)
39
    {
40 1
        return $this->connection->quote($value);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 2
    public function execute($query)
47
    {
48
        try {
49 2
            $ret = $this->connection->exec($query);
50 2
            if ($ret === false) {
51 1
                throw new Exception\InvalidArgumentException("Cannot execute [$query].");
52
            }
53 2
        } catch (Exception\InvalidArgumentException $e) {
54 1
            throw $e;
55
        } catch (\Exception $e) {
56
            $msg = "PDOException : {$e->getMessage()} [$query]";
57
            throw new Exception\InvalidArgumentException($msg);
58
        }
59 2
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 2
    public function query($query)
65
    {
66
        try {
67 2
            $stmt = $this->connection->query($query, \PDO::FETCH_ASSOC);
68 2
            if ($stmt === false) {
69 1
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
70
            }
71 2
            $results = new ArrayObject();
72 2
            foreach ($stmt as $row) {
73 2
                $results->append($row);
74 2
            }
75 2
        } catch (Exception\InvalidArgumentException $e) {
76 1
            throw $e;
77
        } catch (\Exception $e) {
78
            $msg = "PDOException : {$e->getMessage()} [$query]";
79
            throw new Exception\InvalidArgumentException($msg);
80
        }
81 2
        return $results;
82
    }
83
}
84