Passed
Push — master ( 7ce13d...949b33 )
by Andrea
69:11 queued 34:49
created

DatabaseUtils::isDateChanged()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.0368

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
c 1
b 0
f 0
nc 22
nop 2
dl 0
loc 15
ccs 10
cts 11
cp 0.9091
crap 7.0368
rs 8.8333
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Database;
4
5
use Symfony\Bundle\FrameworkBundle\Console\Application;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Output\BufferedOutput;
8
use Doctrine\Common\Persistence\ObjectManager;
9
10
class DatabaseUtils
11
{
12
    /* @var $em \Doctrine\ORM\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...
13
    private $em;
14
    private $kernel;
15
16 2
    public function __construct($kernel, ObjectManager $em)
17
    {
18 2
        $this->kernel = $kernel;
19 2
        $this->em = $em;
20 2
    }
21
22 2
    public function getFieldType($entity, $field)
23
    {
24 2
        $metadata = $this->em->getClassMetadata(get_class($entity));
25 2
        $fieldMetadata = $metadata->fieldMappings[$field];
0 ignored issues
show
Bug introduced by
Accessing fieldMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
26
27 2
        $fieldType = $fieldMetadata['type'];
28
29 2
        return $fieldType;
30
    }
31
32 1
    public function isRecordChanged($entity, $fieldname, $oldvalue, $newvalue)
33
    {
34 1
        $fieldtype = $this->getFieldType(new $entity(), $fieldname);
35 1
        if ('boolean' === $fieldtype) {
36 1
            return $oldvalue !== $newvalue;
37
        }
38 1
        if ('datetime' === $fieldtype || 'date' === $fieldtype) {
39 1
            return $this->isDateChanged($oldvalue, $newvalue);
40
        }
41 1
        if (is_array($oldvalue)) {
42 1
            return $this->isArrayChanged($oldvalue, $newvalue);
43
        }
44
45 1
        return $oldvalue !== $newvalue;
46
    }
47
48 1
    public function isDateChanged($oldvalue, $newvalue)
49
    {
50 1
        $datenewvalue = new \DateTime();
51 1
        $datenewvalue->setTimestamp($newvalue);
52 1
        $twoboth = !$oldvalue && !$newvalue;
53 1
        if ($twoboth) {
54 1
            return false;
55
        }
56 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...
57 1
        if ($onlyonenull) {
58
            return true;
59
        }
60 1
        $changed = ($oldvalue != $datenewvalue);
61
62 1
        return $changed;
63
    }
64
65 1
    public function isArrayChanged($oldvalue, $newvalue)
66
    {
67 1
        $twoboth = !$oldvalue && !$newvalue;
68 1
        if ($twoboth) {
69
            return false;
70
        }
71 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...
72 1
        if ($onlyonenull) {
73 1
            return true;
74
        }
75 1
        $numdiff = array_diff($oldvalue, $newvalue);
76
77 1
        return count($numdiff) > 0;
78
    }
79
80 1
    public function truncateTable($entityclass, $cascade = false)
81
    {
82 1
        $cmd = $this->em->getClassMetadata($entityclass);
83 1
        $connection = $this->em->getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Doctrine\Common\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

83
        /** @scrutinizer ignore-call */ 
84
        $connection = $this->em->getConnection();
Loading history...
84 1
        $dbPlatform = $connection->getDatabasePlatform();
85 1
        $dbtype = $connection->getDriver()->getDatabasePlatform()->getName();
86 1
        $retval = false;
87
88 1
        switch ($dbtype) {
89 1
            case 'mysql':
90 1
                $connection->query('SET FOREIGN_KEY_CHECKS=0');
91 1
                $q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade);
92 1
                $retval = $connection->executeUpdate($q);
93 1
                $connection->query('SET FOREIGN_KEY_CHECKS=1');
94 1
                break;
95
            case 'postgresql':
96
                $cascadesql = $cascade ? 'CASCADE' : '';
97
                $tablename = $cmd->getTableName();
98
                if ($cmd->getSchemaName()) {
99
                    $tablename = $cmd->getSchemaName().'.'.$tablename;
100
                }
101
                $retval = $connection->executeQuery(sprintf('TRUNCATE TABLE %s '.$cascadesql, $tablename));
102
                break;
103
            default:
104
                $q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade);
105
                $retval = $connection->executeUpdate($q);
106
                break;
107
        }
108 1
        $this->em->clear();
109
110 1
        return $retval;
111
    }
112
113 2
    public function isSchemaChanged()
114
    {
115 2
        $application = new Application($this->kernel);
116 2
        $application->setAutoExit(false);
117
118 2
        $input = new ArrayInput(array(
119 2
            'command' => 'doctrine:schema:update',
120
            '--dump-sql' => true,
121
            '--no-debug' => true,
122 2
            '--env' => $this->kernel->getEnvironment(),
123
        ));
124
125
        // You can use NullOutput() if you don't need the output
126 2
        $output = new BufferedOutput();
127 2
        $application->run($input, $output);
128
129
        // return the output, don't use if you used NullOutput()
130 2
        $content = $output->fetch();
131 2
        $changed = strpos($content, 'Nothing to update');
132
133 2
        return 0 !== $changed;
134
    }
135
}
136