SchemaDiffFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 20
dl 0
loc 74
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchemaDiff() 0 16 3
A setTableName() 0 5 1
A __construct() 0 10 1
1
<?php
2
3
namespace Norsys\LogsBundle\Model;
4
5
use Doctrine\DBAL\Schema\Comparator;
6
use Doctrine\DBAL\Schema\Schema;
7
use Doctrine\DBAL\Schema\SchemaDiff;
8
use Doctrine\DBAL\Connection;
9
10
/**
11
 * This service give you the difference between a schema A And B for a given table.
12
 */
13
class SchemaDiffFactory
14
{
15
    /**
16
     * @var null|string
17
     */
18
    private $tableName;
19
    /**
20
     * @var SchemaDiff
21
     */
22
    private $schemaDiff;
23
    /**
24
     * @var Comparator
25
     */
26
    private $comparator;
27
    /**
28
     * @var Connection
29
     */
30
    private $connection;
31
    /**
32
     * @var Schema
33
     */
34
    private $schema;
35
36
    /**
37
     * SchemaDiffFactory constructor.
38
     *
39
     * @param SchemaDiff $schemaDiff
40
     * @param Comparator $comparator
41
     * @param Connection $connection
42
     * @param Schema     $schema
43
     */
44
    public function __construct(
45
        SchemaDiff $schemaDiff,
46
        Comparator $comparator,
47
        Connection $connection,
48
        Schema $schema
49
    ) {
50 1
        $this->schemaDiff = $schemaDiff;
51 1
        $this->comparator = $comparator;
52 1
        $this->connection = $connection;
53 1
        $this->schema = $schema;
54 1
    }
55
56
    /**
57
     * @param string $tableName
58
     * @return $this
59
     */
60
    public function setTableName(string $tableName)
61
    {
62 1
        $this->tableName = $tableName;
63
64 1
        return $this;
65
    }
66
67
    /**
68
     * @return SchemaDiff
69
     * @throws \Exception
70
     */
71
    public function getSchemaDiff()
72
    {
73 1
        if (null === $this->tableName) {
74 1
            throw new \Exception('You need first to inform the object about the table name.');
75
        }
76
77 1
        $tableDiff = $this->comparator->diffTable(
78 1
            $this->connection->getSchemaManager()->createSchema()->getTable($this->tableName),
79 1
            $this->schema->getTable($this->tableName)
80
        );
81
82 1
        if (false !== $tableDiff) {
83 1
            $this->schemaDiff->changedTables[$this->tableName] = $tableDiff;
84
        }
85
86 1
        return $this->schemaDiff;
87
    }
88
}
89