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

CeBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 154
Duplicated Lines 25.97 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 5
dl 40
loc 154
ccs 35
cts 45
cp 0.7778
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildRetention() 0 6 1
A buildPerception() 0 6 1
A buildDespatch() 0 6 1
A buildReversion() 0 6 1
A setCompany() 0 5 1
A setParameters() 12 12 3
A render() 8 8 1
A getRender() 9 9 1
A validate() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Administrador
5
 * Date: 09/08/2017
6
 * Time: 01:30 PM
7
 */
8
9
namespace Greenter\Xml\Builder;
10
11
use Greenter\Model\Company\Company;
12
use Greenter\Model\Despatch\Despatch;
13
use Greenter\Model\Perception\Perception;
14
use Greenter\Model\Retention\Retention;
15
use Greenter\Model\Voided\Reversion;
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 CeBuilder
23
 * @package Greenter\Xml\Builder
24
 */
25
class CeBuilder implements CeBuilderInterface
26
{
27
28
    /**
29
     * Directorio de Cache para las template de Documentos.
30
     * @var string
31
     */
32
    private $dirCache;
33
34
    /**
35
     * Datos de la Compañia.
36
     *
37
     * @var Company
38
     */
39
    private $company;
40
41
42
    /**
43
     * CeBuilder constructor.
44
     */
45 12
    public function __construct()
46
    {
47 12
        $this->dirCache = sys_get_temp_dir();
48 12
    }
49
50
    /**
51
     * Genera un comprobante de retencion.
52
     *
53
     * @param Retention $retention
54
     * @throws ValidationException
55
     * @return string
56
     */
57 4
    public function buildRetention(Retention $retention)
58
    {
59 4
        $this->validate($retention);
60
61 2
        return $this->render('retention.html.twig', $retention);
62
    }
63
64
    /**
65
     * Genera un comprobante de percepcion.
66
     *
67
     * @param Perception $perception
68
     * @throws ValidationException
69
     * @return string
70
     */
71 4
    public function buildPerception(Perception $perception)
72
    {
73 4
        $this->validate($perception);
74
75 2
        return $this->render('perception.html.twig', $perception);
76
    }
77
78
    /**
79
     * Genera una guia de remision.
80
     *
81
     * @param Despatch $despatch
82
     * @throws ValidationException
83
     * @return string
84
     */
85 4
    public function buildDespatch(Despatch $despatch)
86
    {
87 4
        $this->validate($despatch);
88
89 2
        return $this->render('despatch.html.twig', $despatch);
90
    }
91
92
    /**
93
     * Genera una resumen de reversiones.
94
     *
95
     * @param Reversion $reversion
96
     * @throws ValidationException
97
     * @return string
98
     */
99
    public function buildReversion(Reversion $reversion)
100
    {
101
        $this->validate($reversion);
102
103
        return $this->render('voided.html.twig', $reversion);
104
    }
105
106
    /**
107
     * @param Company $company
108
     * @return CeBuilder
109
     */
110 12
    public function setCompany(Company $company)
111
    {
112 12
        $this->company = $company;
113 12
        return $this;
114
    }
115
116
    /**
117
     * Set argumentos.
118
     *
119
     * @param array $params
120
     * @throws \Exception
121
     */
122 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...
123
    {
124
        if (!$params['cache']) {
125
            return;
126
        }
127
128
        if (!is_dir($params['cache'])) {
129
            throw new \Exception('No is a directory valid');
130
        }
131
132
        $this->dirCache = $params['cache'];
133
    }
134
135
    /**
136
     * Get Content XML from template.
137
     *
138
     * @param string $template
139
     * @param object $doc
140
     * @return string
141
     */
142 6 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...
143
    {
144 6
        $twig = $this->getRender();
145 6
        return $twig->render($template, [
146 6
            'doc' => $doc,
147 6
            'emp' => $this->company,
148 6
        ]);
149
    }
150
151 6 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...
152
    {
153 6
        $loader = new Twig_Loader_Filesystem(__DIR__ . '/../Templates');
154 6
        $twig = new Twig_Environment($loader, array(
155 6
            'cache' => $this->dirCache,
156 6
        ));
157
158 6
        return $twig;
159
    }
160
161
    /**
162
     * Validate Entity.
163
     *
164
     * @param object $entity
165
     * @throws ValidationException
166
     */
167 12 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...
168
    {
169 12
        $validator = Validation::createValidatorBuilder()
170 12
            ->addMethodMapping('loadValidatorMetadata')
171 12
            ->getValidator();
172
173 12
        $errs = $validator->validate($entity);
174 12
        if ($errs->count() > 0) {
175 6
            throw new ValidationException($errs);
176
        }
177
    }
178
}