Completed
Push — master ( b463b0...ee9a04 )
by Giancarlos
02:48
created

FeGenerator::buildNote()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3.0261
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\Generator;
10
11
use Greenter\Xml\Model\Company\Company;
12
use Greenter\Xml\Model\Sale\Invoice;
13
use Greenter\Xml\Model\Sale\Note;
14
use Greenter\Xml\Model\Summary\Summary;
15
use Greenter\Xml\Model\Voided\Voided;
16
use Symfony\Component\Validator\Validation;
17
use Twig_Environment;
18
use Twig_Loader_Filesystem;
19
20
21
/**
22
 * Class FeGenerator
23
 * @package Greenter\Xml\Generator
24
 */
25
final class FeGenerator implements XmlGenerator
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
     * Genera un invoice (Factura o Boleta).
42
     *
43
     * @param Invoice $invoice
44
     * @return string
45
     */
46 2
    public function buildInvoice(Invoice $invoice)
47
    {
48 2
        $errors = $this->validate($invoice);
49
50 2
        if ($errors->count() > 0) {
51
            return '';
52
        }
53
54 2
        return $this->render('invoice.html.twig', $invoice);
55
    }
56
57
    /**
58
     * Genera una Nota Electrónica(Credito o Debito).
59
     *
60
     * @param Note $note
61
     * @return string
62
     */
63 4
    public function buildNote(Note $note)
64
    {
65 4
        $errors = $this->validate($note);
66
67 4
        if ($errors->count() > 0) {
68
            return '';
69
        }
70
71 4
        $template = $note->getTipoDoc() === '07'
72 4
            ? 'notacr.html.twig' : 'notadb.html.twig';
73
74 4
        return $this->render($template, $note);
75
    }
76
77
    /**
78
     * Genera una Resumen Diario de Boletas.
79
     *
80
     * @param Summary $summary
81
     * @return string
82
     */
83 2
    public function buildSummary(Summary $summary)
84
    {
85 2
        $errors = $this->validate($summary);
86
87 2
        if ($errors->count() > 0) {
88
            return '';
89
        }
90
91 2
        return $this->render('summary.html.twig', $summary);
92
    }
93
94
    /**
95
     * Genera una comunicacion de Baja.
96
     *
97
     * @param Voided $voided
98
     * @return string
99
     */
100 2
    public function buildVoided(Voided $voided)
101
    {
102 2
        $errors = $this->validate($voided);
103
104 2
        if ($errors->count() > 0) {
105
            return '';
106
        }
107
108 2
        $twig = $this->getRender();
109 2
        return $twig->render('voided.html.twig', [
110 2
            'doc' => $voided,
111 2
            'emp' => $this->company,
112 2
        ]);
113
    }
114
115
    /**
116
     * @param string $dirCache
117
     * @return FeGenerator
118
     */
119 10
    public function setDirCache($dirCache)
120
    {
121 10
        $this->dirCache = $dirCache;
122 10
        return $this;
123
    }
124
125
    /**
126
     * @param Company $company
127
     * @return FeGenerator
128
     */
129 10
    public function setCompany(Company $company)
130
    {
131 10
        $this->company = $company;
132 10
        return $this;
133
    }
134
135
    /**
136
     * Get Content XML from template.
137
     *
138
     * @param string $template
139
     * @param object $doc
140
     * @return string
141
     */
142 8
    private function render($template, $doc)
143
    {
144 8
        $twig = $this->getRender();
145 8
        return $twig->render($template, [
146 8
            'doc' => $doc,
147 8
            'emp' => $this->company,
148 8
        ]);
149
    }
150
151 10
    private function getRender()
152
    {
153 10
        $loader = new Twig_Loader_Filesystem(__DIR__ . '/../Templates');
154 10
        $twig = new Twig_Environment($loader, array(
155 10
            'cache' => $this->dirCache,
156 10
        ));
157
158 10
        return $twig;
159
    }
160
161 10
    private function validate($entity)
162
    {
163 10
        $validator = Validation::createValidatorBuilder()
164 10
            ->addMethodMapping('loadValidatorMetadata')
165 10
            ->getValidator();
166
167 10
        return $validator->validate($entity);
168
    }
169
}