Passed
Push — master ( b76e34...38225e )
by Andrea
21:41
created

DatabaseUtility::truncateTable()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 12
c 2
b 1
f 0
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 9.8666
cc 4
nc 8
nop 2
crap 4
1
<?php
2
3
namespace Fi\CoreBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
6
use Symfony\Bundle\FrameworkBundle\Console\Application;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Output\BufferedOutput;
9
10
class DatabaseUtility
11
{
12
13
    private $container;
14
    /* @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...
15
    private $em;
16
17 2
    public function __construct(Container $container)
18
    {
19 2
        $this->container = $container;
20 2
        $this->em = $this->container->get("doctrine")->getManager();
21 2
    }
22
23 2
    public function getFieldType($entity, $field)
24
    {
25 2
        $metadata = $this->em->getClassMetadata(get_class($entity));
26 2
        $fieldMetadata = $metadata->fieldMappings[$field];
27
28 2
        $fieldType = $fieldMetadata['type'];
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 ($fieldtype === 'boolean') {
36 1
            return $oldvalue !== $newvalue;
37
        }
38 1
        if ($fieldtype === 'datetime' || $fieldtype === 'date') {
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 1
            return true;
59
        }
60 1
        $changed = ($oldvalue != $datenewvalue);
61 1
        return $changed;
62
    }
63
64 1
    public function isArrayChanged($oldvalue, $newvalue)
65
    {
66 1
        $twoboth = !$oldvalue && !$newvalue;
67 1
        if ($twoboth) {
68
            return false;
69
        }
70 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...
71 1
        if ($onlyonenull) {
72 1
            return true;
73
        }
74 1
        $numdiff = array_diff($oldvalue, $newvalue);
75 1
        return count($numdiff) > 0;
76
    }
77
78 1
    public function truncateTable($entityclass, $cascade = false)
79
    {
80 1
        $cmd = $this->em->getClassMetadata($entityclass);
81 1
        $connection = $this->em->getConnection();
82 1
        $dbPlatform = $connection->getDatabasePlatform();
83 1
        $dbtype = $connection->getDriver()->getDatabasePlatform()->getName();
84 1
        $cascademysql = $cascade && $dbtype === 'mysql';
85 1
        if ($cascademysql) {
86 1
            $connection->query('SET FOREIGN_KEY_CHECKS=0');
87
        }
88 1
        $q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade);
89 1
        $connection->executeUpdate($q);
90 1
        if ($cascademysql) {
91 1
            $connection->query('SET FOREIGN_KEY_CHECKS=1');
92
        }
93 1
        $this->em->clear();
94 1
    }
95
96 2
    public function isSchemaChanged()
97
    {
98 2
        $kernel = $this->container->get("kernel");
99 2
        $application = new Application($kernel);
100 2
        $application->setAutoExit(false);
101
102 2
        $input = new ArrayInput(array(
103 2
            'command' => 'doctrine:schema:update',
104
            '--dump-sql' => true,
105
            '--no-debug' => true,
106 2
            '--env' => $kernel->getEnvironment(),
107
        ));
108
109
        // You can use NullOutput() if you don't need the output
110 2
        $output = new BufferedOutput();
111 2
        $application->run($input, $output);
112
113
        // return the output, don't use if you used NullOutput()
114 2
        $content = $output->fetch();
115 2
        $changed = strpos($content, 'Nothing to update');
116 2
        return ($changed!==0);
117
    }
118
}
119