ConfiguratorimportUpdateTrait::update()   B
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 70
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 49
nc 8
nop 4
dl 0
loc 70
ccs 51
cts 51
cp 1
crap 10
rs 7.246
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Command;
4
5
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils;
6
7
trait ConfiguratorimportUpdateTrait
8
{
9 1
    private function executeUpdate($entityclass, $record, $objrecord)
10
    {
11 1
        unset($record['id']);
12 1
        foreach ($record as $key => $value) {
13 1
            if ('discr' == $key) {
14 1
                continue;
15
            }
16 1
            $propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord);
17 1
            $getfieldname = $propertyEntity['get'];
18 1
            $cambiato = $this->dbutility->isRecordChanged($entityclass, $key, $objrecord->$getfieldname(), $value);
19 1
            if (!$cambiato) {
20 1
                if ($this->verboso) {
21 1
                    $msginfo = '<info>'.$entityclass.' con id '.$objrecord->getId()
22 1
                            .' per campo '.$key.' non modificato perchè già '
23 1
                            .var_export($value, true).'</info>';
24 1
                    $this->output->writeln($msginfo);
25
                }
26
            } else {
27
                try {
28 1
                    $this->update($objrecord, $key, $value, $entityclass);
29
                } catch (\Exception $exc) {
30
                    $msgerr = '<error>Modifica '.$entityclass.' con id '.$objrecord->getId()
31
                            .' per campo '.$key.', ERRORE: '.$exc->getMessage()
32
                            .' alla riga '.$exc->getLine().'</error>';
33
                    $this->output->writeln($msgerr);
34
                    //dump($exc);
35
                    return 1;
36
                }
37
            }
38
        }
39 1
        $this->em->persist($objrecord);
40 1
        $this->em->flush();
41
42 1
        return 0;
43
    }
44
45 1
    private function update(&$objrecord, $key, $value, $entityclass)
46
    {
47 1
        $fieldtype = $this->dbutility->getFieldType($objrecord, $key);
48 1
        $propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord);
49 1
        $getfieldname = $propertyEntity['get'];
50 1
        $setfieldname = $propertyEntity['set'];
51
52 1
        if ('boolean' === $fieldtype) {
53 1
            $newval = FieldTypeUtils::getBooleanValue($value);
54
55 1
            $msgok = '<info>Modifica '.$entityclass.' con id '.$objrecord->getId()
56 1
                    .' per campo '.$key.' cambio valore da '
57 1
                    .var_export($objrecord->$getfieldname(), true)
58 1
                    .' a '.var_export($newval, true).' in formato Boolean</info>';
59 1
            $this->output->writeln($msgok);
60 1
            $objrecord->$setfieldname($newval);
61
62 1
            return true;
63
        }
64 1
        if ('datetime' === $fieldtype || 'date' === $fieldtype) {
65 1
            $date = FieldTypeUtils::getDateTimeValueFromTimestamp($value);
66 1
            $msgok = '<info>Modifica '.$entityclass.' con id '.$objrecord->getId()
67 1
                    .' per campo '.$key.' cambio valore da '
68 1
                    //. (!is_null($objrecord->$getfieldname())) ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "(null)"
69 1
                    .($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format('Y-m-d H:i:s') : var_export(null, true))
70 1
                    .' a '.($value ? $date->format('Y-m-d H:i:s') : var_export(null, true)).' in formato DateTime</info>';
71 1
            $this->output->writeln($msgok);
72 1
            $objrecord->$setfieldname($date);
73
74 1
            return true;
75
        }
76 1
        if (is_array($value) || is_array($objrecord->$getfieldname())) {
77 1
            $newval = FieldTypeUtils::getArrayValue($value);
78 1
            $msgarray = '<info>Modifica '.$entityclass.' con id '.$objrecord->getId()
79 1
                    .' per campo '.$key.' cambio valore da '
80 1
                    .json_encode($objrecord->$getfieldname()).' a '
81 1
                    .json_encode($newval).' in formato array'.'</info>';
82 1
            $this->output->writeln($msgarray);
83 1
            $objrecord->$setfieldname($newval);
84
85 1
            return true;
86
        }
87
88 1
        $joincolumn = $this->entityutility->getJoinTableField($entityclass, $key);
89 1
        $joincolumnproperty = $this->entityutility->getJoinTableFieldProperty($entityclass, $key);
90 1
        if ($joincolumn && $joincolumnproperty) {
91 1
            $joincolumnobj = $this->em->getRepository($joincolumn)->find($value);
92 1
            $msgok = '<info>Modifica '.$entityclass.' con id '.$objrecord->getId()
93 1
                    .' per campo '.$key.' cambio valore da '.var_export($objrecord->$getfieldname(), true)
94 1
                    .' a '.var_export($value, true).' tramite entity find</info>';
95 1
            $this->output->writeln($msgok);
96 1
            $joinobj = $this->entityutility->getEntityProperties($joincolumnproperty, new $entityclass());
97 1
            $setfieldname = $joinobj['set'];
98 1
            $objrecord->$setfieldname($joincolumnobj);
99
100 1
            return true;
101
        }
102
103
//        //Si prende in considerazione solo il null del boolean, gli altri non si toccano
104
//        if (!is_null($value)) {
105
//            return true;
106
//        }
107
108 1
        $msgok = '<info>Modifica '.$entityclass.' con id '.$objrecord->getId()
109 1
                .' per campo '.$key.' cambio valore da '.var_export($objrecord->$getfieldname(), true)
110 1
                .' a '.var_export($value, true).'</info>';
111 1
        $this->output->writeln($msgok);
112 1
        $objrecord->$setfieldname($value);
113
114 1
        return false;
115
    }
116
}
117