Completed
Push — master ( 1ed55c...1adee7 )
by Giancarlos
03:47
created

FeBuilder::getRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: Giansalex
6
 * Date: 15/07/2017
7
 * Time: 22:15
8
 */
9
namespace Greenter\Xml\Builder;
10
11
use Greenter\Model\Company\Company;
12
use Greenter\Model\Sale\Invoice;
13
use Greenter\Model\Sale\Note;
14
use Greenter\Model\Summary\Summary;
15
use Greenter\Model\Voided\Voided;
16
use Greenter\Xml\Exception\ValidationException;
17
use Symfony\Component\Validator\Validation;
18
use Twig_Environment;
19
use Twig_Loader_Filesystem;
20
21
/**
22
 * Class FeBuilder
23
 * @package Greenter\Xml\Builder
24
 */
25
final class FeBuilder implements FeBuilderInteface
26
{
27
    /**
28
     * Directorio de Cache para las template de Documentos.
29
     * @var string
30
     */
31
    private $dirCache;
32
33
    /**
34
     * Datos de la Compañia.
35
     *
36
     * @var Company
37
     */
38
    private $company;
39
40
    /**
41
     * FeBuilder constructor.
42
     */
43 50
    public function __construct()
44
    {
45 50
        $this->dirCache = sys_get_temp_dir();
46 50
    }
47
48
    /**
49
     * Genera un invoice (Factura o Boleta).
50
     *
51
     * @param Invoice $invoice
52
     * @throws ValidationException
53
     * @return string
54
     */
55 10
    public function buildInvoice(Invoice $invoice)
56
    {
57 10
        $this->validate($invoice);
58
59 6
        return $this->render('invoice.html.twig', $invoice);
60
    }
61
62
    /**
63
     * Genera una Nota Electrónica(Credito o Debito).
64
     *
65
     * @param Note $note
66
     * @throws ValidationException
67
     * @return string
68
     */
69 18
    public function buildNote(Note $note)
70
    {
71 18
        $this->validate($note);
72
73 12
        $template = $note->getTipoDoc() === '07'
74 12
            ? 'notacr.html.twig' : 'notadb.html.twig';
75
76 12
        return $this->render($template, $note);
77
    }
78
79
    /**
80
     * Genera una Resumen Diario de Boletas.
81
     *
82
     * @param Summary $summary
83
     * @throws ValidationException
84
     * @return string
85
     */
86 10
    public function buildSummary(Summary $summary)
87
    {
88 10
        $this->validate($summary);
89
90 6
        return $this->render('summary.html.twig', $summary);
91
    }
92
93
    /**
94
     * Genera una comunicacion de Baja.
95
     *
96
     * @param Voided $voided
97
     * @throws ValidationException
98
     * @return string
99
     */
100 10
    public function buildVoided(Voided $voided)
101
    {
102 10
        $this->validate($voided);
103
104 6
        return $this->render('voided.html.twig', $voided);
105
    }
106
107
    /**
108
     * @param Company $company
109
     * @return FeBuilder
110
     */
111 50
    public function setCompany(Company $company)
112
    {
113 50
        $this->company = $company;
114 50
        return $this;
115
    }
116
117
    /**
118
     * Set argumentos.
119
     *
120
     * @param array $params
121
     * @throws \Exception
122
     */
123 50 View Code Duplication
    public function setParameters($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125 50
        if (!$params['cache']) {
126 2
            return;
127 2
        }
128
129 50
        if (!is_dir($params['cache'])) {
130
            throw new \Exception('No is a directory valid');
131
        }
132
133 50
        $this->dirCache = $params['cache'];
134 50
    }
135
136
    /**
137
     * Get Content XML from template.
138
     *
139
     * @param string $template
140
     * @param object $doc
141
     * @return string
142
     */
143 30 View Code Duplication
    private function render($template, $doc)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145 30
        $twig = $this->getRender();
146 30
        return $twig->render($template, [
147 30
            'doc' => $doc,
148 30
            'emp' => $this->company,
149 30
        ]);
150
    }
151
152 30 View Code Duplication
    private function getRender()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154 30
        $loader = new Twig_Loader_Filesystem(__DIR__ . '/../Templates');
155 30
        $twig = new Twig_Environment($loader, array(
156 30
            'cache' => $this->dirCache,
157 30
        ));
158
159 30
        return $twig;
160
    }
161
162
    /**
163
     * Validate Entity.
164
     *
165
     * @param object $entity
166
     * @throws ValidationException
167
     */
168 48 View Code Duplication
    private function validate($entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170 48
        $validator = Validation::createValidatorBuilder()
171 48
            ->addMethodMapping('loadValidatorMetadata')
172 48
            ->getValidator();
173
174 48
        $errs = $validator->validate($entity);
175 48
        if ($errs->count() > 0) {
176 18
            throw new ValidationException($errs);
177
        }
178
    }
179
}