Passed
Push — master ( 046444...56e09b )
by Andrea
28:16 queued 22:41
created

DatabaseUtils   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 86.49%

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 124
ccs 64
cts 74
cp 0.8649
rs 10
c 0
b 0
f 0
wmc 27

7 Methods

Rating   Name   Duplication   Size   Complexity  
B isDateChanged() 0 15 7
B isArrayChanged() 0 13 7
A truncateTable() 0 31 5
A isRecordChanged() 0 14 5
A __construct() 0 4 1
A getFieldType() 0 8 1
A isSchemaChanged() 0 21 1
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Database;
4
5
use DateTime;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\EntityManager;
8
use Symfony\Bundle\FrameworkBundle\Console\Application;
9
use Symfony\Component\Console\Input\ArrayInput;
10
use Symfony\Component\Console\Output\BufferedOutput;
11
use function count;
12
13
class DatabaseUtils
14
{
15
    /* @var $em EntityManager */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $em at position 0 could not be parsed: Unknown type name '$em' at position 0 in $em.
Loading history...
16
    private $em;
17
    private $kernel;
18
19 2
    public function __construct($kernel, EntityManagerInterface $em)
20
    {
21 2
        $this->kernel = $kernel;
22 2
        $this->em = $em;
23 2
    }
24
25 2
    public function getFieldType($entity, $field)
26
    {
27 2
        $metadata = $this->em->getClassMetadata(get_class($entity));
28 2
        $fieldMetadata = $metadata->fieldMappings[$field];
0 ignored issues
show
Bug introduced by
Accessing fieldMappings on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
29
30 2
        $fieldType = $fieldMetadata['type'];
31
32 2
        return $fieldType;
33
    }
34
35 1
    public function isRecordChanged($entity, $fieldname, $oldvalue, $newvalue)
36
    {
37 1
        $fieldtype = $this->getFieldType(new $entity(), $fieldname);
38 1
        if ('boolean' === $fieldtype) {
39 1
            return $oldvalue !== $newvalue;
40
        }
41 1
        if ('datetime' === $fieldtype || 'date' === $fieldtype) {
42 1
            return $this->isDateChanged($oldvalue, $newvalue);
43
        }
44 1
        if (is_array($oldvalue)) {
45 1
            return $this->isArrayChanged($oldvalue, $newvalue);
46
        }
47
48 1
        return $oldvalue !== $newvalue;
49
    }
50
51 1
    public function isDateChanged($oldvalue, $newvalue)
52
    {
53 1
        $datenewvalue = new DateTime();
54 1
        $datenewvalue->setTimestamp($newvalue);
55 1
        $twoboth = !$oldvalue && !$newvalue;
56 1
        if ($twoboth) {
57 1
            return false;
58
        }
59 1
        $onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue);
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: $onlyonenull = (! $oldva...ldvalue && ! $newvalue), Probably Intended Meaning: $onlyonenull = ! $oldval...ldvalue && ! $newvalue)
Loading history...
60 1
        if ($onlyonenull) {
61
            return true;
62
        }
63 1
        $changed = ($oldvalue != $datenewvalue);
64
65 1
        return $changed;
66
    }
67
68 1
    public function isArrayChanged($oldvalue, $newvalue)
69
    {
70 1
        $twoboth = !$oldvalue && !$newvalue;
71 1
        if ($twoboth) {
72
            return false;
73
        }
74 1
        $onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue);
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: $onlyonenull = (! $oldva...ldvalue && ! $newvalue), Probably Intended Meaning: $onlyonenull = ! $oldval...ldvalue && ! $newvalue)
Loading history...
75 1
        if ($onlyonenull) {
76 1
            return true;
77
        }
78 1
        $numdiff = array_diff($oldvalue, $newvalue);
79
80 1
        return count($numdiff) > 0;
81
    }
82
83 1
    public function truncateTable($entityclass, $cascade = false)
84
    {
85 1
        $cmd = $this->em->getClassMetadata($entityclass);
86 1
        $connection = $this->em->getConnection();
87 1
        $dbPlatform = $connection->getDatabasePlatform();
88 1
        $dbtype = $connection->getDriver()->getDatabasePlatform()->getName();
89 1
        $retval = false;
90
91 1
        switch ($dbtype) {
92 1
            case 'mysql':
93
                $connection->query('SET FOREIGN_KEY_CHECKS=0');
94
                $q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade);
95
                $retval = $connection->executeUpdate($q);
96
                $connection->query('SET FOREIGN_KEY_CHECKS=1');
97
                break;
98 1
            case 'postgresql':
99 1
                $cascadesql = $cascade ? 'CASCADE' : '';
100 1
                $tablename = $cmd->getTableName();
101 1
                if ($cmd->getSchemaName()) {
102 1
                    $tablename = $cmd->getSchemaName().'.'.$tablename;
103
                }
104 1
                $retval = $connection->executeQuery(sprintf('TRUNCATE TABLE %s '.$cascadesql, $tablename));
105 1
                break;
106
            default:
107
                $q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade);
108
                $retval = $connection->executeUpdate($q);
109
                break;
110
        }
111 1
        $this->em->clear();
112
113 1
        return $retval;
114
    }
115
116 2
    public function isSchemaChanged()
117
    {
118 2
        $application = new Application($this->kernel);
119 2
        $application->setAutoExit(false);
120
121 2
        $input = new ArrayInput(array(
122 2
            'command' => 'doctrine:schema:update',
123
            '--dump-sql' => true,
124
            '--no-debug' => true,
125 2
            '--env' => $this->kernel->getEnvironment(),
126
        ));
127
128
        // You can use NullOutput() if you don't need the output
129 2
        $output = new BufferedOutput();
130 2
        $application->run($input, $output);
131
132
        // return the output, don't use if you used NullOutput()
133 2
        $content = $output->fetch();
134 2
        $changed = strpos($content, 'Nothing to update');
135
136 2
        return 0 !== $changed;
137
    }
138
}
139