Completed
Push — master ( 92b268...adee3c )
by Bartko
779:57 queued 778:22
created

Doctrine2DBAL   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 91
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConnection() 0 4 1
A getOptions() 0 4 1
A beginTransaction() 0 5 1
A commitTransaction() 0 5 1
A rollbackTransaction() 0 5 1
A isInTransaction() 0 4 2
A canHandleNestedTransaction() 0 4 1
A quoteIdentifier() 0 5 1
A executeSQL() 0 5 1
A executeSelectSQL() 0 6 1
A executeInsertSQL() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet\Adapter;
6
7
use Doctrine\DBAL\Connection as DbConnection;
8
use StefanoTree\NestedSet\Options;
9
10
class Doctrine2DBAL implements AdapterInterface
11
{
12
    private $connection;
13
    private $options;
14
15
    /**
16
     * @param Options      $options
17
     * @param DbConnection $connection
18
     */
19 120
    public function __construct(Options $options, DbConnection $connection)
20
    {
21 120
        $this->connection = $connection;
22 120
        $this->options = $options;
23 120
    }
24
25
    /**
26
     * @return DbConnection
27
     */
28 115
    private function getConnection(): DbConnection
29
    {
30 115
        return $this->connection;
31
    }
32
33
    /**
34
     * @return Options
35
     */
36 15
    public function getOptions(): Options
37
    {
38 15
        return $this->options;
39
    }
40
41 36
    public function beginTransaction(): void
42
    {
43 36
        $this->getConnection()
44 36
            ->beginTransaction();
45 36
    }
46
47 19
    public function commitTransaction(): void
48
    {
49 19
        $this->getConnection()
50 19
            ->commit();
51 19
    }
52
53 18
    public function rollbackTransaction(): void
54
    {
55 18
        $this->getConnection()
56 18
            ->rollBack();
57 18
    }
58
59 4
    public function isInTransaction(): bool
60
    {
61 4
        return 0 === $this->getConnection()->getTransactionNestingLevel() ? false : true;
62
    }
63
64 32
    public function canHandleNestedTransaction(): bool
65
    {
66 32
        return true;
67
    }
68
69 108
    public function quoteIdentifier(string $columnName): string
70
    {
71 108
        return $this->getConnection()
72 108
            ->quoteIdentifier($columnName);
73
    }
74
75 15
    public function executeInsertSQL(string $sql, array $params = array())
76
    {
77 15
        $options = $this->getOptions();
78 15
        $this->executeSQL($sql, $params);
79
80 15
        if (array_key_exists($options->getIdColumnName(), $params)) {
81 2
            return $params[$options->getIdColumnName()];
82
        } else {
83 13
            return $this->getConnection()
84 13
                        ->lastInsertId($options->getSequenceName());
85
        }
86
    }
87
88 60
    public function executeSQL(string $sql, array $params = array()): void
89
    {
90 60
        $this->getConnection()
91 60
             ->executeQuery($sql, $params);
92 60
    }
93
94 87
    public function executeSelectSQL(string $sql, array $params = array()): array
95
    {
96 87
        return $this->getConnection()
97 87
             ->executeQuery($sql, $params)
98 87
            ->fetchAll();
99
    }
100
}
101