Passed
Push — master ( c65cd9...710cb4 )
by Esteban De La Fuente
10:47
created

AbstractRendererStrategy   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 93.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 25
c 1
b 0
f 0
dl 0
loc 59
ccs 29
cts 31
cp 0.9355
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 7 1
A __construct() 0 3 1
A createData() 0 32 2
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\Lib\Core\Foundation\Abstract\AbstractStrategy;
28
use Derafu\Lib\Core\Package\Prime\Component\Template\Contract\TemplateComponentInterface;
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 103
    public function __construct(
39
        private TemplateComponentInterface $templateComponent
40
    ) {
41 103
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46 103
    public function render(DocumentBagInterface $bag): string
47
    {
48 103
        $data = $this->createData($bag);
49
50 103
        return $this->templateComponent->render(
51 103
            $data['options']['filepath'],
52 103
            $data
53 103
        );
54
    }
55
56
    /**
57
     * Crea los datos que se pasarán a la plantilla que se renderizará.
58
     *
59
     * @param DocumentBagInterface $bag Bolsa con los datos del documento a
60
     * renderizar.
61
     * @return array Datos que se pasarán a la plantilla al renderizar.
62
     */
63 103
    protected function createData(DocumentBagInterface $bag): array
64
    {
65 103
        $options = $this->resolveOptions($bag->getRendererOptions());
66
67
        // Preparar datos que se usarán para renderizar.
68 103
        $data = [
69 103
            'document' => $bag->getDocumentData(),
70 103
            'document_extra' => $bag->getDocumentExtra(),
71 103
            'document_stamp' => $bag->getDocumentStamp(),
72 103
            'document_auth' => $bag->getDocumentAuth(),
73 103
            'options' => [
74 103
                'template' => $options->get('template'),
75 103
                'filepath' => null,
76 103
                'format' => $options->get('format'),
77 103
                'config' => [
78 103
                    'html' => $options->get('html', []),
79 103
                    'pdf' => $options->get('pdf', []),
80 103
                ],
81 103
            ],
82 103
        ];
83
84
        // Asignar la ubicación de la plantilla.
85 103
        if ($data['options']['template'][0] === '/') {
86
            $data['options']['filepath'] = $data['options']['template'];
87
            $data['options']['template'] = basename($data['options']['template']);
88
        } else {
89 103
            $base = 'billing/document/renderer/';
90 103
            $data['options']['filepath'] = $base . $data['options']['template'];
91
        }
92
93
        // Entregar los datos que se pasarán a la plantilla.
94 103
        return $data;
95
    }
96
}
97