DoctrineLayer   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 150
ccs 68
cts 68
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A connect() 0 7 1
A setTable() 0 15 3
A getTotalColumns() 0 4 1
A getColumnType() 0 6 1
A getColumnName() 0 4 1
A getColumnLength() 0 4 1
A isColumnAutoincrement() 0 4 1
A insert() 0 20 3
A isDate() 0 4 1
A getFakeType() 0 23 1
1
<?php
2
/**
3
 * This file is part of the Fakerino package.
4
 *
5
 * (c) Nicola Pietroluongo <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Fakerino\Core\Database;
12
13
use Doctrine\DBAL\ConnectionException;
14
use Doctrine\DBAL\Types\Type;
15
16
/**
17
 * Class DoctrineLayer,
18
 * provides an interface for the Doctrine DBAL.
19
 *
20
 * @author Nicola Pietroluongo <[email protected]>
21
 */
22
final class DoctrineLayer implements DbInterface
23
{
24
    /* @var \Doctrine\DBAL\DriverManager */
25
    public static $conn;
26
27
    private $tableName;
28
    private $columns;
29
    private $totalFields;
30
    private $databaseConfig;
31
32
    /**
33
     * @param array $databaseConfig
34
     */
35 70
    public function __construct($databaseConfig)
36
    {
37 70
        $this->databaseConfig = $databaseConfig;
38 70
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 25
    public function connect()
44
    {
45 25
        $config = new \Doctrine\DBAL\Configuration();
46 25
        self::$conn = \Doctrine\DBAL\DriverManager::getConnection($this->databaseConfig, $config);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Doctrine\DBAL\DriverMan...atabaseConfig, $config) of type object<Doctrine\DBAL\Connection> is incompatible with the declared type object<Doctrine\DBAL\DriverManager> of property $conn.

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...
47
48 25
        return true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 25
    public function setTable($tableName)
55
    {
56 25
        $this->tableName = $tableName;
57 25
        $schemaManager = self::$conn->getSchemaManager();
0 ignored issues
show
Bug introduced by
The method getSchemaManager() does not seem to exist on object<Doctrine\DBAL\DriverManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58 25
        self::$conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
0 ignored issues
show
Bug introduced by
The method getDatabasePlatform() does not seem to exist on object<Doctrine\DBAL\DriverManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
60 25
        $tableSchema = $schemaManager->listTableColumns($tableName);
61 25
        foreach ($tableSchema as $column) {
62 25
            $this->columns[] = $column;
63 25
        }
64 25
        $this->totalFields = count($this->columns);
65 25
        if (empty($tableSchema)) {
66 1
            throw new ConnectionException(sprintf('Error in database connection, please check the configuration parameter and the table name "%s"', $tableName));
67
        }
68 25
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function getTotalColumns()
74
    {
75 2
        return $this->totalFields;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 2
    public function getColumnType($num)
82
    {
83 2
        $doctrineType = $this->columns[$num]->getType()->getName();
84
85 2
        return $this->getFakeType($doctrineType);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    public function getColumnName($num)
92
    {
93 2
        return $this->columns[$num]->getName();
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function getColumnLength($num)
100
    {
101 1
        return $this->columns[$num]->getLength();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public function isColumnAutoincrement($num)
108
    {
109 1
        return $this->columns[$num]->getAutoincrement();
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 5
    public function insert(DbRowEntity $rows)
116
    {
117 5
        $queryBuilder = self::$conn->createQueryBuilder();
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not seem to exist on object<Doctrine\DBAL\DriverManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118 5
        $sql = $queryBuilder->insert($this->tableName);
119 5
        $values = array();
120 5
        $types = array();
121 5
        $rowsElement = $rows->getFields();
122 5
        foreach ($rowsElement as $field) {
123 5
            $sql->setValue($field->getName(), '?');
124 5
            if ($this->isDate($field->getType())) {
125 2
                $values[] = new \DateTime($field->getValue());
126 2
            } else {
127 3
                $values[] = $field->getValue();
128
            }
129 5
            $types[] = $field->getType();
130 5
        }
131 5
        self::$conn->executeQuery($sql, $values, $types);
0 ignored issues
show
Bug introduced by
The method executeQuery() does not seem to exist on object<Doctrine\DBAL\DriverManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133 5
        return true;
134
    }
135
136
    /**
137
     * @param $columnType
138
     * @return bool
139
     */
140 5
    private function isDate($columnType)
141 1
    {
142 5
        return in_array($columnType, array(Type::DATETIME, Type::DATETIMETZ, Type::DATE, Type::TIME));
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 18
    public function getFakeType($columnType)
149
    {
150
        $fakeType = array(
151 18
            Type::BIGINT => 'integer',
152 18
            Type::BOOLEAN => 'boolean',
153 18
            Type::DATETIME => 'datetime',
154 18
            Type::DATETIMETZ => 'datetimetz',
155 18
            Type::DATE => 'date',
156 18
            Type::TIME => 'time',
157 18
            Type::DECIMAL => 'integer',
158 18
            Type::INTEGER => 'integer',
159 18
            Type::SMALLINT => 'integer',
160 18
            Type::STRING => 'string',
161 18
            Type::TEXT => 'text',
162 18
            Type::BLOB => 'text',
163 18
            Type::FLOAT => 'integer',
164 18
            Type::GUID => 'integer',
165 18
            Type::SIMPLE_ARRAY => 'string',
166 18
            Type::BINARY => 'string',
167 18
        );
168
169 18
        return $fakeType[strtolower($columnType)];
170
    }
171
}