Test Failed
Push — master ( 8cbe19...3a660b )
by Esteban De La Fuente
06:47
created

AbstractRendererStrategy::createData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2.0026

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 32
ccs 21
cts 23
cp 0.913
rs 9.6
cc 2
nc 2
nop 1
crap 2.0026
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\Document\Abstract;
26
27
use Derafu\Backbone\Abstract\AbstractStrategy;
28
use Derafu\Renderer\Contract\RendererInterface;
29
use libredte\lib\Core\Package\Billing\Component\Document\Contract\DocumentBagInterface;
30
use libredte\lib\Core\Package\Billing\Component\Document\Contract\RendererStrategyInterface;
31
32
/**
33
 * Clase abstracta (base) para las estrategias de renderizado de documentos
34
 * tributarios utilizando plantillas.
35
 */
36
abstract class AbstractRendererStrategy extends AbstractStrategy implements RendererStrategyInterface
37
{
38 53
    public function __construct(
39
        private RendererInterface $renderer
40
    ) {
41 53
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46 53
    public function render(DocumentBagInterface $bag): string
47
    {
48 53
        [$data, $options] = $this->createDataAndOptions($bag);
49
50 53
        return $this->renderer->render(
51 53
            $options['filepath'],
52 53
            $data,
53 53
            $options
54 53
        );
55
    }
56
57
    /**
58
     * Crea los datos que se pasarán a la plantilla que se renderizará.
59
     *
60
     * @param DocumentBagInterface $bag Bolsa con los datos del documento a
61
     * renderizar.
62
     * @return array Datos que se pasarán a la plantilla al renderizar.
63
     */
64 53
    protected function createDataAndOptions(DocumentBagInterface $bag): array
65
    {
66 53
        $options = $this->resolveOptions($bag->getRendererOptions());
67
68
        // Preparar datos que se usarán para renderizar.
69 53
        $data = [
70 53
            'document' => $bag->getDocumentData(),
71 53
            'document_extra' => $bag->getDocumentExtra(),
72 53
            'document_stamp' => $bag->getDocumentStamp(),
73 53
            'document_auth' => $bag->getDocumentAuth(),
74 53
        ];
75
76 53
        $options = [
77 53
            'template' => $options->get('template'),
78 53
            'filepath' => null,
79 53
            'format' => $options->get('format'),
80 53
            'config' => [
81 53
                'html' => $options->get('html', [])->all(),
82 53
                'pdf' => $options->get('pdf', [])->all(),
83 53
            ],
84 53
        ];
85
86
        // Asignar la ubicación de la plantilla.
87 53
        if ($options['template'][0] === '/') {
88
            $options['filepath'] = $options['template'];
89
            $options['template'] = basename($options['template']);
90
        } else {
91 53
            $base = 'billing/document/renderer/';
92 53
            $options['filepath'] = $base . $options['template'];
93
        }
94
95
        // Entregar los datos que se pasarán a la plantilla y las opciones.
96 53
        return [$data, $options];
97
    }
98
}
99