Completed
Push — master ( 527d7b...b463b0 )
by Giancarlos
07:13
created

FeGenerator::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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