Passed
Branch master (0b4ab1)
by Esteban De La Fuente
74:02 queued 50:02
created

LoaderWorker::createFromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 3
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Derafu: Biblioteca PHP (Núcleo).
7
 * Copyright (C) Derafu <https://www.derafu.org>
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 GNU
20
 * junto a este programa.
21
 *
22
 * En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>.
23
 */
24
25
namespace Derafu\Lib\Core\Package\Prime\Component\Certificate\Worker;
26
27
use Derafu\Lib\Core\Foundation\Abstract\AbstractWorker;
28
use Derafu\Lib\Core\Package\Prime\Component\Certificate\Contract\LoaderWorkerInterface;
29
use Derafu\Lib\Core\Package\Prime\Component\Certificate\Entity\Certificate;
30
use Derafu\Lib\Core\Package\Prime\Component\Certificate\Exception\CertificateException;
31
32
/**
33
 * Clase que maneja la configuración y carga de certificados digitales para la
34
 * firma electrónica.
35
 */
36
class LoaderWorker extends AbstractWorker implements LoaderWorkerInterface
37
{
38
    /**
39
     * {@inheritdoc}
40
     */
41 2
    public function createFromFile(string $filepath, string $password): Certificate
42
    {
43 2
        if (!is_readable($filepath)) {
44 1
            throw new CertificateException(sprintf(
45 1
                'No fue posible leer el archivo del certificado digital desde %s',
46 1
                $filepath
47 1
            ));
48
        }
49
50 1
        $data = file_get_contents($filepath);
51
52 1
        return self::createFromData($data, $password);
0 ignored issues
show
Bug Best Practice introduced by
The method Derafu\Lib\Core\Package\...orker::createFromData() is not static, but was called statically. ( Ignorable by Annotation )

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

52
        return self::/** @scrutinizer ignore-call */ createFromData($data, $password);
Loading history...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 3
    public function createFromData(string $data, string $password): Certificate
59
    {
60 3
        $certs = [];
61
62 3
        if (openssl_pkcs12_read($data, $certs, $password) === false) {
63 1
            throw new CertificateException(sprintf(
64 1
                'No fue posible leer los datos del certificado digital.',
65 1
            ));
66
        }
67
68 2
        return self::createFromKeys($certs['cert'], $certs['pkey']);
0 ignored issues
show
Bug Best Practice introduced by
The method Derafu\Lib\Core\Package\...orker::createFromKeys() is not static, but was called statically. ( Ignorable by Annotation )

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

68
        return self::/** @scrutinizer ignore-call */ createFromKeys($certs['cert'], $certs['pkey']);
Loading history...
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 20
    public function createFromArray(array $data): Certificate
75
    {
76 20
        $publicKey = $data['publicKey'] ?? $data['cert'] ?? null;
77 20
        $privateKey = $data['privateKey'] ?? $data['pkey'] ?? null;
78
79 20
        if ($publicKey === null) {
80 1
            throw new CertificateException(
81 1
                'La clave pública del certificado no fue encontrada.'
82 1
            );
83
        }
84
85 19
        if ($privateKey === null) {
86 1
            throw new CertificateException(
87 1
                'La clave privada del certificado no fue encontrada.'
88 1
            );
89
        }
90
91 18
        return self::createFromKeys($publicKey, $privateKey);
0 ignored issues
show
Bug Best Practice introduced by
The method Derafu\Lib\Core\Package\...orker::createFromKeys() is not static, but was called statically. ( Ignorable by Annotation )

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

91
        return self::/** @scrutinizer ignore-call */ createFromKeys($publicKey, $privateKey);
Loading history...
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 20
    public function createFromKeys(string $publicKey, string $privateKey): Certificate
98
    {
99 20
        return new Certificate($publicKey, $privateKey);
100
    }
101
}
102