DoctrineDbalTableCreator::getTableName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Simple\Queue\Transport;
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\Transport
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 17
    public static function getTableName(): string
44
    {
45 17
        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();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@link createSchemaManager()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

56
        $schemaManager = /** @scrutinizer ignore-deprecated */ $this->connection->getSchemaManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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