Completed
Pull Request — 2.0 (#7)
by Raphaël
11:08
created

DbalTableDiffService::createOrUpdateTable()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 39
rs 8.439
cc 5
eloc 28
nc 5
nop 1
1
<?php
2
3
namespace Wabel\Zoho\CRM\Copy;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Schema\Comparator;
7
use Doctrine\DBAL\Schema\SchemaDiff;
8
use Doctrine\DBAL\Schema\Table;
9
use Psr\Log\LoggerInterface;
10
11
class DbalTableDiffService
12
{
13
    /**
14
     * @var Connection
15
     */
16
    private $connection;
17
18
    /**
19
     * @var LoggerInterface
20
     */
21
    private $logger;
22
23
    /**
24
     * @param Connection $connection
25
     */
26
    public function __construct(Connection $connection, LoggerInterface $logger)
27
    {
28
        $this->connection = $connection;
29
        $this->logger = $logger;
30
    }
31
32
    /**
33
     * @param Table $table
34
     *
35
     * @return bool Returns true if changes where applied. False otherwise.
36
     */
37
    public function createOrUpdateTable(Table $table)
38
    {
39
        $tableName = $table->getName();
40
        $dbSchema = $this->connection->getSchemaManager()->createSchema();
41
        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...
42
            $dbTable = $dbSchema->getTable($tableName);
43
44
            $comparator = new Comparator();
45
            $tableDiff = $comparator->diffTable($dbTable, $table);
46
47
            if ($tableDiff !== false) {
48
                $this->logger->notice('Changes detected in table structure for '.$tableName.'. Applying patch.');
49
                $diff = new SchemaDiff();
50
                $diff->fromSchema = $dbSchema;
51
                $diff->changedTables[$tableName] = $tableDiff;
52
                $statements = $diff->toSaveSql($this->connection->getDatabasePlatform());
53
                foreach ($statements as $sql) {
54
                    $this->connection->exec($sql);
55
                }
56
57
                return true;
58
            } else {
59
                $this->logger->info('No changes detected in table structure for '.$tableName);
60
61
                return false;
62
            }
63
        } else {
64
            $this->logger->notice("Creating new table '$tableName'.");
65
            $diff = new SchemaDiff();
66
            $diff->fromSchema = $dbSchema;
67
            $diff->newTables[$tableName] = $table;
68
            $statements = $diff->toSaveSql($this->connection->getDatabasePlatform());
69
            foreach ($statements as $sql) {
70
                $this->connection->exec($sql);
71
            }
72
73
            return true;
74
        }
75
    }
76
}
77