Completed
Branch 1.1 (23eb45)
by David
02:38
created

DbalTableDiffService::createOrUpdateTable()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
rs 8.439
cc 5
eloc 28
nc 5
nop 1
1
<?php
2
3
4
namespace Wabel\Zoho\CRM\Copy;
5
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Schema\Comparator;
9
use Doctrine\DBAL\Schema\SchemaDiff;
10
use Doctrine\DBAL\Schema\Table;
11
use Psr\Log\LoggerInterface;
12
13
class DbalTableDiffService
14
{
15
    /**
16
     * @var Connection
17
     */
18
    private $connection;
19
20
    /**
21
     * @var LoggerInterface
22
     */
23
    private $logger;
24
25
    /**
26
     * @param Connection $connection
27
     */
28
    public function __construct(Connection $connection, LoggerInterface $logger)
29
    {
30
        $this->connection = $connection;
31
        $this->logger = $logger;
32
    }
33
34
    /**
35
     * @param Table $table
36
     * @return bool Returns true if changes where applied. False otherwise.
37
     */
38
    public function createOrUpdateTable(Table $table)
39
    {
40
        $tableName = $table->getName();
41
        $dbSchema = $this->connection->getSchemaManager()->createSchema();
42
        if ($this->connection->getSchemaManager()->tablesExist($tableName)) {
0 ignored issues
show
Documentation introduced by
$tableName is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
            $dbTable = $dbSchema->getTable($tableName);
44
45
            $comparator = new Comparator();
46
            $tableDiff = $comparator->diffTable($dbTable, $table);
47
48
            if ($tableDiff !== false) {
49
                $this->logger->notice("Changes detected in table structure for ".$tableName.". Applying patch.");
50
                $diff = new SchemaDiff();
51
                $diff->fromSchema = $dbSchema;
52
                $diff->changedTables[$tableName] = $tableDiff;
53
                $statements = $diff->toSaveSql($this->connection->getDatabasePlatform());
54
                foreach ($statements as $sql) {
55
                    $this->connection->exec($sql);
56
                }
57
                return true;
58
            } else {
59
                $this->logger->info("No changes detected in table structure for ".$tableName);
60
                return false;
61
            }
62
        } else {
63
            $this->logger->notice("Creating new table '$tableName'.");
64
            $diff = new SchemaDiff();
65
            $diff->fromSchema = $dbSchema;
66
            $diff->newTables[$tableName] = $table;
67
            $statements = $diff->toSaveSql($this->connection->getDatabasePlatform());
68
            foreach ($statements as $sql) {
69
                $this->connection->exec($sql);
70
            }
71
            return true;
72
        }
73
    }
74
}