StoricomodificheRepository::isDataChanged()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 5
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Repository;
4
5
use Cdf\BiCoreBundle\Entity\Storicomodifiche;
6
use Doctrine\ORM\EntityRepository;
7
use Cdf\BiCoreBundle\Entity\Colonnetabelle;
8
use Cdf\BiCoreBundle\Entity\Operatori;
9
use DateTime;
10
11
class StoricomodificheRepository extends EntityRepository
12
{
13
    /**
14
     * save field modification in history table.
15
     *
16
     * @param string $nometabella
17
     * @param array<mixed> $changes
18
     * @param int $id
19
     * @param Operatori $user
20
     * @return void
21
     */
22 2
    public function saveHistory(string $nometabella, array $changes, int $id, Operatori $user): void
23
    {
24 2
        $em = $this->getEntityManager();
25
26 2
        $adesso = new DateTime();
27 2
        foreach ($changes as $fieldName => $change) {
28 2
            $nuovamodifica = new Storicomodifiche();
29 2
            $nuovamodifica->setNometabella($nometabella);
30 2
            $nuovamodifica->setNomecampo($fieldName);
31 2
            $nuovamodifica->setIdtabella($id);
32 2
            $nuovamodifica->setGiorno($adesso);
33 2
            $nuovamodifica->setValoreprecedente($this->getValoreprecedenteImpostare($change));
34 2
            $nuovamodifica->setOperatori($user);
35 2
            $em->persist($nuovamodifica);
36
        }
37 2
        $em->flush();
38 2
        $em->clear();
39
    }
40
41
    /**
42
     *
43
     * @param mixed $change
44
     * @return mixed
45
     */
46 2
    private function getValoreprecedenteImpostare($change)
47
    {
48 2
        if (is_object($change)) {
49
            if ($change instanceof DateTime) {
50
                $risposta = $change->format('d/m/Y H:i:s');
51
            } else {
52
                /** @phpstan-ignore-next-line */
53
                $risposta = $change->__toString() . ' (' . $change->getId() . ')';
54
            }
55
        } else {
56 2
            $risposta = $change;
57
        }
58
59 2
        return $risposta;
60
    }
61
62
    /**
63
     * check if field is historicized.
64
     *
65
     * @string $entitclass
66
     * @string $indicedato fieldname
67
     *
68
     * return @bool
69
     */
70 7
    private function isHistoricized(string $nometabella, string $indiceDato): bool
71
    {
72 7
        $risposta = false;
73
74 7
        $em = $this->getEntityManager();
75 7
        $entity = $em->getRepository(Colonnetabelle::class)->findOneBy(
76 7
            array(
77 7
                    'nometabella' => $nometabella,
78 7
                    'nomecampo' => $indiceDato,
79 7
                )
80 7
        );
81
82 7
        if ($entity && $entity->isRegistrastorico()) {
83 2
            $risposta = true;
84
        }
85
86 7
        return $risposta;
87
    }
88
89
    /**
90
     * check if single data is  changed.
91
     *
92
     * @param string $nometabella
93
     * @param mixed $datooriginale
94
     * @param mixed $singoloDato
95
     * @param string $indiceDato
96
     * @param array<mixed> $changes
97
     * @return void
98
     */
99 7
    private function isDataChanged(string $nometabella, $datooriginale, $singoloDato, string $indiceDato, array &$changes): void
100
    {
101 7
        if (($datooriginale !== $singoloDato) && $this->isHistoricized($nometabella, $indiceDato)) {
102 2
            $changes[$indiceDato] = $datooriginale;
103
        }
104
    }
105
106
    /**
107
     * check if something changes.
108
     *
109
     * @param string $nometabella
110
     * @param array<mixed> $originalData
111
     * @param array<mixed> $newData
112
     * @return array<mixed>
113
     */
114 7
    public function isRecordChanged(string $nometabella, array $originalData, array $newData): array
115
    {
116 7
        $changes = array();
117 7
        foreach ($newData as $indiceDato => $singoloDato) {
118 7
            $this->isDataChanged($nometabella, $originalData[$indiceDato], $singoloDato, $indiceDato, $changes);
119
        }
120
121 7
        return $changes;
122
    }
123
}
124