ConfiguratorimportInsertTrait   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 110
ccs 75
cts 78
cp 0.9615
rs 10
c 0
b 0
f 0
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A executeInsert() 0 20 4
B insert() 0 61 10
A changeRecordId() 0 23 3
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Command;
4
5
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils;
6
7
trait ConfiguratorimportInsertTrait
8
{
9 2
    private function executeInsert($entityclass, $record)
10
    {
11 2
        $objrecord = new $entityclass();
12 2
        foreach ($record as $key => $value) {
13 2
            if ('id' !== $key) {
14 2
                if (!$this->insert($objrecord, $key, $value, $record['id'], $entityclass)) {
15 2
                    $propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord);
16 2
                    $setfieldname = $propertyEntity['set'];
17 2
                    $objrecord->$setfieldname($value);
18
                }
19
            }
20
        }
21 2
        $this->em->persist($objrecord);
22 2
        $this->em->flush();
23
24 2
        $infomsg = '<info>'.$entityclass.' con id '.$objrecord->getId().' aggiunta</info>';
25 2
        $this->output->writeln($infomsg);
26 2
        $checkid = $this->changeRecordId($entityclass, $record, $objrecord);
27
28 2
        return $checkid;
29
    }
30
31 2
    private function insert(&$objrecord, $key, $value, $recordid, $entityclass)
32
    {
33 2
        $propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord);
34 2
        $getfieldname = $propertyEntity['get'];
35 2
        $setfieldname = $propertyEntity['set'];
36 2
        if ('discr' == $key) {
37 1
            return true;
38
        }
39 2
        $fieldtype = $this->dbutility->getFieldType($objrecord, $key);
40 2
        if ('boolean' === $fieldtype) {
41 2
            $newval = FieldTypeUtils::getBooleanValue($value);
42 2
            $msgok = '<info>Inserimento '.$entityclass.' con id '.$recordid
43 2
                    .' per campo '.$key.' con valore '
44 2
                    .var_export($newval, true).' in formato Boolean</info>';
45 2
            $this->output->writeln($msgok);
46 2
            $objrecord->$setfieldname($newval);
47
48 2
            return true;
49
        }
50
        //Si prende in considerazione solo il null del boolean, gli altri non si toccano
51 2
        if (!$value) {
52 2
            return true;
53
        }
54 2
        if ('datetime' === $fieldtype || 'date' === $fieldtype) {
55 1
            $date = FieldTypeUtils::getDateTimeValueFromTimestamp($value);
56 1
            $msgok = '<info>Inserimento '.$entityclass.' con id '.$recordid
57 1
                    .' per campo '.$key.' cambio valore da '
58 1
                    .($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format('Y-m-d H:i:s') : 'NULL')
59 1
                    .' a '.$date->format('Y-m-d H:i:s').' in formato DateTime</info>';
60 1
            $this->output->writeln($msgok);
61 1
            $objrecord->$setfieldname($date);
62
63 1
            return true;
64
        }
65 2
        if (is_array($value)) {
66 2
            $msgarray = '<info>Inserimento '.$entityclass.' con id '.$recordid
67 2
                    .' per campo '.$key.' cambio valore da '
68 2
                    .json_encode($objrecord->$getfieldname()).' a '
69 2
                    .json_encode($value).' in formato array'.'</info>';
70 2
            $this->output->writeln($msgarray);
71 2
            $objrecord->$setfieldname($value);
72
73 2
            return true;
74
        }
75
76 2
        $joincolumn = $this->entityutility->getJoinTableField($entityclass, $key);
77 2
        $joincolumnproperty = $this->entityutility->getJoinTableFieldProperty($entityclass, $key);
78 2
        if ($joincolumn && $joincolumnproperty) {
79 2
            $joincolumnobj = $this->em->getRepository($joincolumn)->find($value);
80 2
            $msgok = '<info>Inserimento '.$entityclass.' con id '.$recordid
81 2
                    .' per campo '.$key
82 2
                    .' con valore '.print_r($value, true).' tramite entity find</info>';
0 ignored issues
show
Bug introduced by
Are you sure print_r($value, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

82
                    .' con valore './** @scrutinizer ignore-type */ print_r($value, true).' tramite entity find</info>';
Loading history...
83 2
            $this->output->writeln($msgok);
84 2
            $joinobj = $this->entityutility->getEntityProperties($joincolumnproperty, new $entityclass());
85 2
            $setfieldname = $joinobj['set'];
86 2
            $objrecord->$setfieldname($joincolumnobj);
87
88 2
            return true;
89
        }
90
91 2
        return false;
92
    }
93
94 2
    private function changeRecordId($entityclass, $record, $objrecord)
95
    {
96 2
        if ($record['id'] !== $objrecord->getId()) {
97
            try {
98 1
                $qb = $this->em->createQueryBuilder();
99 1
                $q = $qb->update($entityclass, 'u')
100 1
                        ->set('u.id', ':newid')
101 1
                        ->where('u.id = :oldid')
102 1
                        ->setParameter('newid', $record['id'])
103 1
                        ->setParameter('oldid', $objrecord->getId())
104 1
                        ->getQuery();
105 1
                $q->execute();
106 1
                $msgok = '<info>'.$entityclass.' con id '.$objrecord->getId().' sistemata</info>';
107 1
                $this->output->writeln($msgok);
108
            } catch (\Exception $exc) {
109
                echo $exc->getMessage();
110
111
                return 1;
112
            }
113 1
            $this->em->flush();
114
        }
115
116 2
        return 0;
117
    }
118
}
119