SchemaBuilder::executeQueries()   A
last analyzed

Complexity

Conditions 4
Paths 9

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.4042

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
ccs 5
cts 9
cp 0.5556
rs 9.9332
c 0
b 0
f 0
cc 4
nc 9
nop 2
crap 5.4042
1
<?php
2
3
namespace Norsys\LogsBundle\Model;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Schema\Schema;
7
8
/**
9
 * Class SchemaBuilder
10
 */
11
class SchemaBuilder
12
{
13
    /**
14
     * @var Connection $conn
15
     */
16
    protected $conn;
17
18
    /**
19
     * @var string
20
     */
21
    protected $tableName;
22
23
    /**
24
     * @var Schema $schema
25
     */
26
    protected $schema;
27
28
    /**
29
     * @var SchemaDiffFactory
30
     */
31
    private $schemaDiffFactory;
32
33
    /**
34
     * SchemaBuilder constructor.
35
     *
36
     * @param Connection        $conn
37
     * @param string            $tableName
38
     * @param Schema            $schema
39
     * @param SchemaDiffFactory $schemaDiffFactory
40
     */
41
    public function __construct(
42
        Connection $conn,
43
        string $tableName,
44
        Schema $schema,
45
        SchemaDiffFactory $schemaDiffFactory
46
    ) {
47 1
        $this->conn      = $conn;
48 1
        $this->tableName = $tableName;
49
50 1
        $this->schema = $schema;
51
52 1
        $entryTable = $this->schema->createTable($this->tableName);
53 1
        $entryTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
54 1
        $entryTable->addColumn('channel', 'string', array('length' => 255, 'notNull' => true));
55 1
        $entryTable->addColumn('level', 'integer', array('notNull' => true));
56 1
        $entryTable->addColumn('level_name', 'string', array('length' => 255, 'notNull' => true));
57 1
        $entryTable->addColumn('message', 'text', array('notNull' => true));
58 1
        $entryTable->addColumn('datetime', 'datetime', array('notNull' => true));
59 1
        $entryTable->addColumn('context', 'text');
60 1
        $entryTable->addColumn('extra', 'text');
61 1
        $entryTable->addColumn('http_server', 'text');
62 1
        $entryTable->addColumn('http_post', 'text');
63 1
        $entryTable->addColumn('http_get', 'text');
64 1
        $entryTable->setPrimaryKey(array('id'));
65
66 1
        $this->schemaDiffFactory = $schemaDiffFactory;
67 1
        $this->schemaDiffFactory->setTableName($this->tableName);
68 1
    }
69
70
    /**
71
     * @param \Closure|null $logger
72
     */
73
    public function drop(\Closure $logger = null)
74
    {
75 1
        $queries = $this->schema->toDropSql($this->conn->getDatabasePlatform());
76
77 1
        $this->executeQueries($queries, $logger);
78 1
    }
79
80
    /**
81
     * @param \Closure|null $logger
82
     */
83
    public function create(\Closure $logger = null)
84
    {
85 1
        $queries = $this->schema->toSql($this->conn->getDatabasePlatform());
86
87 1
        $this->executeQueries($queries, $logger);
88 1
    }
89
90
    /**
91
     * @param \Closure|null $logger
92
     */
93
    public function update(\Closure $logger = null)
94
    {
95 1
        $queries = $this->schemaDiffFactory->getSchemaDiff()->toSaveSql(
96 1
            $this->conn->getDatabasePlatform()
97
        );
98
99 1
        $this->executeQueries($queries, $logger);
100 1
    }
101
102
    /**
103
     * @param array         $queries
104
     * @param \Closure|null $logger
105
     *
106
     * @throws \Exception
107
     */
108
    protected function executeQueries(array $queries, \Closure $logger = null)
109
    {
110 1
        $this->conn->beginTransaction();
111
112
        try {
113 1
            foreach ($queries as $query) {
114 1
                if (null !== $logger) {
115
                    $logger($query);
116
                }
117
118 1
                $this->conn->query($query);
119
            }
120
121 1
            $this->conn->commit();
122
        } catch (\Exception $e) {
123
            $this->conn->rollback();
124
            throw $e;
125
        }
126 1
    }
127
}
128