Passed
Push — master ( 5bd05b...9a2fe3 )
by
unknown
48s queued 11s
created

DoctrineDbalTableCreator::createDataBaseTable()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
nc 4
nop 0
dl 0
loc 29
ccs 21
cts 21
cp 1
crap 4
rs 9.6
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Simple\Queue\Store;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Types\Types;
9
use Doctrine\DBAL\Schema\Table;
10
11
/**
12
 * Class DoctrineDbalTableCreator
13
 * @package Simple\Queue\Store
14
 */
15
class DoctrineDbalTableCreator
16
{
17
    /** @var Connection */
18
    protected Connection $connection;
19
20
    /** @var string */
21
    protected static string $tableName = 'queue';
22
23
    /**
24
     * QueueTableCreator constructor.
25
     * @param Connection $connection
26
     */
27 3
    public function __construct(Connection $connection)
28
    {
29 3
        $this->connection = $connection;
30 3
    }
31
32
    /**
33
     * @param string $tableName
34
     */
35 1
    public static function changeTableName(string $tableName): void
36
    {
37 1
        self::$tableName = $tableName;
38 1
    }
39
40
    /**
41
     * @return string
42
     */
43 12
    public static function getTableName(): string
44
    {
45 12
        return self::$tableName;
46
    }
47
48
    /**
49
     * Creating a queue table
50
     *
51
     * @throws \Doctrine\DBAL\Exception
52
     * @throws \Doctrine\DBAL\Schema\SchemaException
53
     */
54 3
    public function createDataBaseTable(): void
55
    {
56 3
        $schemaManager = $this->connection->getSchemaManager();
57
58 3
        $tableExists = $schemaManager ? $schemaManager->tablesExist([self::getTableName()]) : false;
0 ignored issues
show
introduced by
$schemaManager is of type Doctrine\DBAL\Schema\AbstractSchemaManager, thus it always evaluated to true.
Loading history...
59
60 3
        if ($schemaManager === null || $tableExists === true) {
61 1
            return;
62
        }
63
64 2
        $table = new Table(self::getTableName());
65
66 2
        $table->addColumn('id', Types::GUID, ['length' => 16, 'fixed' => true]);
67 2
        $table->addColumn('status', Types::STRING);
68 2
        $table->addColumn('attempts', Types::SMALLINT);
69 2
        $table->addColumn('queue', Types::STRING);
70 2
        $table->addColumn('event', Types::STRING, ['notnull' => false]);
71 2
        $table->addColumn('is_job', Types::BOOLEAN, ['default' => false]);
72 2
        $table->addColumn('body', Types::TEXT, ['notnull' => false]);
73 2
        $table->addColumn('priority', Types::SMALLINT, ['notnull' => false]);
74 2
        $table->addColumn('error', Types::TEXT, ['notnull' => false]);
75 2
        $table->addColumn('redelivered_at', Types::DATETIME_IMMUTABLE, ['notnull' => false]);
76 2
        $table->addColumn('created_at', Types::DATETIME_IMMUTABLE);
77 2
        $table->addColumn('exact_time', Types::BIGINT);
78
79 2
        $table->setPrimaryKey(['id']);
80 2
        $table->addIndex(['priority', 'created_at', 'queue', 'status', 'event', 'id']);
81
82 2
        $schemaManager->createTable($table);
83 2
    }
84
}
85