Passed
Push — master ( cf6123...e152a2 )
by Roberto
02:59 queued 11s
created

EvtBasesTrab::toNode()   D

Complexity

Conditions 16
Paths 40

Size

Total Lines 210

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
dl 0
loc 210
ccs 0
cts 202
cp 0
rs 4.4532
c 0
b 0
f 0
cc 16
nc 40
nop 0
crap 272

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace NFePHP\eSocial\Factories;
4
5
/**
6
 * Class eSocial EvtBasesTrab Event S-5001 constructor
7
 * Read for 2.4.2 layout
8
 * Read for 2.5.0 layout
9
 *
10
 * @category  library
11
 * @package   NFePHP\eSocial
12
 * @copyright NFePHP Copyright (c) 2017
13
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
14
 * @license   https://opensource.org/licenses/MIT MIT
15
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
16
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
17
 * @link      http://github.com/nfephp-org/sped-esocial for the canonical source repository
18
 */
19
20
use NFePHP\Common\Certificate;
21
use NFePHP\eSocial\Common\Factory;
22
use NFePHP\eSocial\Common\FactoryInterface;
23
use stdClass;
24
25
class EvtBasesTrab extends Factory implements FactoryInterface
26
{
27
    /**
28
     * @var int
29
     */
30
    public $sequencial;
31
    /**
32
     * @var string
33
     */
34
    protected $evtName = 'evtBasesTrab';
35
    /**
36
     * @var string
37
     */
38
    protected $evtAlias = 'S-5001';
39
    /**
40
     * Parameters patterns
41
     *
42
     * @var array
43
     */
44
    protected $parameters = [];
45
46
    /**
47
     * Constructor
48
     *
49
     * @param string $config
50
     * @param stdClass $std
51
     * @param Certificate $certificate | null
52
     * @param string $date
53
     */
54
    public function __construct(
55
        $config,
56
        stdClass $std,
57
        Certificate $certificate = null,
58
        $date = ''
59
    ) {
60
        parent::__construct($config, $std, $certificate, $date);
61
    }
62
63
    /**
64
     * Node constructor
65
     */
66
    protected function toNode()
67
    {
68
        $ideEmpregador = $this->node->getElementsByTagName('ideEmpregador')->item(0);
69
        //o idEvento pode variar de evento para evento
70
        //então cada factory individualmente terá de construir o seu
71
        $ideEvento = $this->dom->createElement("ideEvento");
72
        $this->dom->addChild(
73
            $ideEvento,
74
            "nrRecArqBase",
75
            ! empty($this->std->nrrecarqbase) ? $this->std->nrrecarqbase : null,
76
            false
77
        );
78
        $this->dom->addChild(
79
            $ideEvento,
80
            "indApuracao",
81
            $this->std->indapuracao,
82
            true
83
        );
84
        $this->dom->addChild(
85
            $ideEvento,
86
            "perApur",
87
            $this->std->perapur,
88
            true
89
        );
90
        $this->node->insertBefore($ideEvento, $ideEmpregador);
91
92
        $ideTrabalhador = $this->dom->createElement("ideTrabalhador");
93
        $this->dom->addChild(
94
            $ideTrabalhador,
95
            "cpfTrab",
96
            $this->std->cpftrab,
97
            true
98
        );
99
        if (isset($this->std->procjudtrab)) {
100
            foreach ($this->std->procjudtrab as $proc) {
101
                $procJudTrab = $this->dom->createElement("procJudTrab");
102
                $this->dom->addChild(
103
                    $procJudTrab,
104
                    "nrProcJud",
105
                    $proc->nrprocjud,
106
                    true
107
                );
108
                $this->dom->addChild(
109
                    $procJudTrab,
110
                    "codSusp",
111
                    $proc->codsusp,
112
                    true
113
                );
114
                $ideTrabalhador->appendChild($procJudTrab);
115
            }
116
        }
117
        $this->node->appendChild($ideTrabalhador);
118
119
        if (isset($this->std->infocpcalc)) {
120
            foreach ($this->std->infocpcalc as $info) {
121
                $infoCpCalc = $this->dom->createElement("infoCpCalc");
122
                $this->dom->addChild(
123
                    $infoCpCalc,
124
                    "tpCR",
125
                    $info->tpcr,
126
                    true
127
                );
128
                $this->dom->addChild(
129
                    $infoCpCalc,
130
                    "vrCpSeg",
131
                    $info->vrcpseg,
132
                    true
133
                );
134
                $this->dom->addChild(
135
                    $infoCpCalc,
136
                    "vrDescSeg",
137
                    $info->vrdescseg,
138
                    true
139
                );
140
                $this->node->appendChild($infoCpCalc);
141
            }
142
        }
143
144
        $infoCp = $this->dom->createElement("infoCp");
145
        foreach ($this->std->ideestablot as $ideest) {
146
            $ideEstabLot = $this->dom->createElement("ideEstabLot");
147
            $this->dom->addChild(
148
                $ideEstabLot,
149
                "tpInsc",
150
                $ideest->tpinsc,
151
                true
152
            );
153
            $this->dom->addChild(
154
                $ideEstabLot,
155
                "nrInsc",
156
                $ideest->nrinsc,
157
                true
158
            );
159
            $this->dom->addChild(
160
                $ideEstabLot,
161
                "codLotacao",
162
                $ideest->codlotacao,
163
                true
164
            );
165
            foreach ($ideest->infocategincid as $infocat) {
166
                $infoCategIncid = $this->dom->createElement("infoCategIncid");
167
                $this->dom->addChild(
168
                    $infoCategIncid,
169
                    "matricula",
170
                    ! empty($infocat->matricula) ? $infocat->matricula : null,
171
                    false
172
                );
173
                $this->dom->addChild(
174
                    $infoCategIncid,
175
                    "codCateg",
176
                    $infocat->codcateg,
177
                    true
178
                );
179
                $this->dom->addChild(
180
                    $infoCategIncid,
181
                    "indSimples",
182
                    ! empty($infocat->indsimples) ? $infocat->indsimples : null,
183
                    false
184
                );
185
                foreach ($infocat->infobasecs as $infobase) {
186
                    $infoBaseCS = $this->dom->createElement("infoBaseCS");
187
                    $this->dom->addChild(
188
                        $infoBaseCS,
189
                        "ind13",
190
                        $infobase->ind13,
191
                        true
192
                    );
193
                    $this->dom->addChild(
194
                        $infoBaseCS,
195
                        "tpValor",
196
                        $infobase->tpvalor,
197
                        true
198
                    );
199
                    $this->dom->addChild(
200
                        $infoBaseCS,
201
                        "valor",
202
                        $infobase->valor,
203
                        true
204
                    );
205
                    $infoCategIncid->appendChild($infoBaseCS);
206
                }
207
208
                if (isset($infocat->calcterc)) {
209
                    foreach ($infocat->calcterc as $cT) {
210
                        $calcTerc = $this->dom->createElement("calcTerc");
211
                        $this->dom->addChild(
212
                            $calcTerc,
213
                            "tpCR",
214
                            $cT->tpcr,
215
                            true
216
                        );
217
                        $this->dom->addChild(
218
                            $calcTerc,
219
                            "vrCsSegTerc",
220
                            $cT->vrcssegterc,
221
                            true
222
                        );
223
                        $this->dom->addChild(
224
                            $calcTerc,
225
                            "vrDescTerc",
226
                            $cT->vrdescterc,
227
                            true
228
                        );
229
                        $infoCategIncid->appendChild($calcTerc);
230
                    }
231
                }
232
                if (isset($infocat->infoperref)) {
233
                    foreach ($infocat->infoperref as $iref) {
234
                        $inforef = $this->dom->createElement("infoPerRef");
235
                        $this->dom->addChild(
236
                            $inforef,
237
                            "perRef",
238
                            $iref->perRef,
239
                            true
240
                        );
241
                        foreach($iref->detinfoperref as $dref) {
242
                            $detref = $this->dom->createElement("detInfoPerRef");
243
                            $this->dom->addChild(
244
                                $detref,
245
                                "ind13",
246
                                $dref->ind13,
247
                                true
248
                            );
249
                            $this->dom->addChild(
250
                                $detref,
251
                                "tpValor",
252
                                $dref->tpvalor,
253
                                true
254
                            );
255
                            $this->dom->addChild(
256
                                $detref,
257
                                "vrPerRef",
258
                                $dref->vrperref,
259
                                true
260
                            );
261
                            $inforef->appendChild($detref);
262
                        }
263
                        $infoCategIncid->appendChild($inforef);
264
                    }    
265
                    
266
                }
267
                $ideEstabLot->appendChild($infoCategIncid);
268
            }
269
            $infoCp->appendChild($ideEstabLot);
270
        }
271
        $this->node->appendChild($infoCp);
272
        $this->eSocial->appendChild($this->node);
273
        //$this->xml = $this->dom->saveXML($this->node);
274
        $this->sign();
275
    }
276
}
277