Passed
Push — master ( acbcc2...152ef5 )
by Andrea
17:16 queued 11s
created

DatabaseUtility   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 95.31%

Importance

Changes 9
Bugs 2 Features 0
Metric Value
wmc 26
eloc 60
c 9
b 2
f 0
dl 0
loc 107
ccs 61
cts 64
cp 0.9531
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B isDateChanged() 0 14 7
A __construct() 0 4 1
A getFieldType() 0 7 1
A isSchemaChanged() 0 21 1
B isArrayChanged() 0 12 7
A isRecordChanged() 0 14 5
A truncateTable() 0 16 4
1
<?php
2
3
namespace Fi\CoreBundle\DependencyInjection;
4
5
use DateTime;
6
use Doctrine\ORM\EntityManager;
7
use Symfony\Bundle\FrameworkBundle\Console\Application;
8
use Symfony\Component\Console\Input\ArrayInput;
9
use Symfony\Component\Console\Output\BufferedOutput;
10
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
11
12
class DatabaseUtility
13
{
14
15
    private $container;
16
    /* @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...
17
    private $em;
18
19 2
    public function __construct(Container $container)
20
    {
21 2
        $this->container = $container;
22 2
        $this->em = $this->container->get("doctrine")->getManager();
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];
29
30 2
        $fieldType = $fieldMetadata['type'];
31 2
        return $fieldType;
32
    }
33
34 1
    public function isRecordChanged($entity, $fieldname, $oldvalue, $newvalue)
35
    {
36 1
        $fieldtype = $this->getFieldType(new $entity(), $fieldname);
37 1
        if ($fieldtype === 'boolean') {
38 1
            return $oldvalue !== $newvalue;
39
        }
40 1
        if ($fieldtype === 'datetime' || $fieldtype === 'date') {
41 1
            return $this->isDateChanged($oldvalue, $newvalue);
42
        }
43 1
        if (is_array($oldvalue)) {
44 1
            return $this->isArrayChanged($oldvalue, $newvalue);
45
        }
46
47 1
        return ($oldvalue !== $newvalue);
48
    }
49
50 1
    public function isDateChanged($oldvalue, $newvalue)
51
    {
52 1
        $datenewvalue = new DateTime();
53 1
        $datenewvalue->setTimestamp($newvalue);
54 1
        $twoboth = !$oldvalue && !$newvalue;
55 1
        if ($twoboth) {
56 1
            return false;
57
        }
58 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...
59 1
        if ($onlyonenull) {
60 1
            return true;
61
        }
62 1
        $changed = ($oldvalue != $datenewvalue);
63 1
        return $changed;
64
    }
65
66 1
    public function isArrayChanged($oldvalue, $newvalue)
67
    {
68 1
        $twoboth = !$oldvalue && !$newvalue;
69 1
        if ($twoboth) {
70
            return false;
71
        }
72 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...
73 1
        if ($onlyonenull) {
74 1
            return true;
75
        }
76 1
        $numdiff = array_diff($oldvalue, $newvalue);
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();
84 1
        $dbPlatform = $connection->getDatabasePlatform();
85 1
        $dbtype = $connection->getDriver()->getDatabasePlatform()->getName();
86 1
        $cascademysql = $cascade && $dbtype === 'mysql';
87 1
        if ($cascademysql) {
88
            $connection->query('SET FOREIGN_KEY_CHECKS=0');
89
        }
90 1
        $q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade);
91 1
        $connection->executeUpdate($q);
92 1
        if ($cascademysql) {
93
            $connection->query('SET FOREIGN_KEY_CHECKS=1');
94
        }
95 1
        $this->em->clear();
96 1
    }
97
98 2
    public function isSchemaChanged()
99
    {
100 2
        $kernel = $this->container->get("kernel");
101 2
        $application = new Application($kernel);
0 ignored issues
show
Bug introduced by
It seems like $kernel can also be of type null; however, parameter $kernel of Symfony\Bundle\Framework...lication::__construct() does only seem to accept Symfony\Component\HttpKernel\KernelInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

101
        $application = new Application(/** @scrutinizer ignore-type */ $kernel);
Loading history...
102 2
        $application->setAutoExit(false);
103
104 2
        $input = new ArrayInput(array(
105 2
            'command' => 'doctrine:schema:update',
106
            '--dump-sql' => true,
107
            '--no-debug' => true,
108 2
            '--env' => $kernel->getEnvironment(),
109
        ));
110
111
        // You can use NullOutput() if you don't need the output
112 2
        $output = new BufferedOutput();
113 2
        $application->run($input, $output);
114
115
        // return the output, don't use if you used NullOutput()
116 2
        $content = $output->fetch();
117 2
        $changed = strpos($content, 'Nothing to update');
118 2
        return ($changed!==0);
119
    }
120
}
121