MandatarioFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 0
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\Lib\Core\Foundation\Abstract\AbstractFactory;
28
use Derafu\Lib\Core\Helper\Factory;
29
use Derafu\Lib\Core\Helper\Rut;
30
use libredte\lib\Core\Package\Billing\Component\TradingParties\Contract\MandatarioFactoryInterface;
31
use libredte\lib\Core\Package\Billing\Component\TradingParties\Contract\MandatarioInterface;
32
use libredte\lib\Core\Package\Billing\Component\TradingParties\Entity\Mandatario;
33
use LogicException;
34
35
/**
36
 * Fábrica de una entidad de mandatario.
37
 */
38
class MandatarioFactory extends AbstractFactory implements MandatarioFactoryInterface
39
{
40
    /**
41
     * Clase de la entidad de los mandatarios.
42
     *
43
     * @var string
44
     */
45
    private string $class = Mandatario::class;
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function create(array $data): MandatarioInterface
51
    {
52
        $data = $this->normalizeData($data);
53
54
        [$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\Lib\Core\Helper\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

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