MandatarioFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 39
ccs 0
cts 12
cp 0
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeData() 0 12 4
A create() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * LibreDTE: Biblioteca PHP (Núcleo).
7
 * Copyright (C) LibreDTE <https://www.libredte.cl>
8
 *
9
 * Este programa es software libre: usted puede redistribuirlo y/o modificarlo
10
 * bajo los términos de la Licencia Pública General Affero de GNU publicada por
11
 * la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o
12
 * (a su elección) cualquier versión posterior de la misma.
13
 *
14
 * Este programa se distribuye con la esperanza de que sea útil, pero SIN
15
 * GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD
16
 * PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública
17
 * General Affero de GNU para obtener una información más detallada.
18
 *
19
 * Debería haber recibido una copia de la Licencia Pública General Affero de
20
 * GNU junto a este programa.
21
 *
22
 * En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>.
23
 */
24
25
namespace libredte\lib\Core\Package\Billing\Component\TradingParties\Factory;
26
27
use Derafu\L10n\Cl\Rut\Rut;
28
use Derafu\Support\Factory;
29
use libredte\lib\Core\Package\Billing\Component\TradingParties\Contract\MandatarioFactoryInterface;
30
use libredte\lib\Core\Package\Billing\Component\TradingParties\Contract\MandatarioInterface;
31
use libredte\lib\Core\Package\Billing\Component\TradingParties\Entity\Mandatario;
32
use LogicException;
33
34
/**
35
 * Fábrica de una entidad de mandatario.
36
 */
37
class MandatarioFactory implements MandatarioFactoryInterface
38
{
39
    /**
40
     * Clase de la entidad de los mandatarios.
41
     *
42
     * @var string
43
     */
44
    private string $class = Mandatario::class;
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function create(array $data): MandatarioInterface
50
    {
51
        $data = $this->normalizeData($data);
52
53
        [$data['run'], $data['dv']] = Rut::toArray($data['run']);
0 ignored issues
show
Bug introduced by
It seems like $data['run'] can also be of type null; however, parameter $rut of Derafu\L10n\Cl\Rut\Rut::toArray() does only seem to accept string, 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

53
        [$data['run'], $data['dv']] = Rut::toArray(/** @scrutinizer ignore-type */ $data['run']);
Loading history...
54
55
        return Factory::create($data, $this->class);
56
    }
57
58
    /**
59
     * Normaliza los datos del mandatario para su creación.
60
     *
61
     * @param array $data
62
     * @return array
63
     */
64
    protected function normalizeData(array $data): array
65
    {
66
        $data['run'] = $data['run'] ?? $data['rut'] ?? null;
67
        unset($data['rut']);
68
69
        if (empty($data['run']) || $data['nombre'] || $data['email']) {
70
            throw new LogicException(
71
                'Los atributos run, nombre y email del mandatario son obligatorios.'
72
            );
73
        }
74
75
        return $data;
76
    }
77
}
78