Completed
Push — master ( 83fda4...fe95dc )
by
unknown
05:27 queued 02:26
created

DaCommon::setCancelFlag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NFePHP\DA\Common;
4
5
/**
6
 * Classe comum para a impressão em PDF do Documento Auxiliar de Bilhete de Passagem eletronico
7
 *
8
 * @category  Library
9
 * @package   nfephp-org/sped-da
10
 * @copyright 2009-2020 NFePHP
11
 * @license   http://www.gnu.org/licenses/lesser.html LGPL v3 or MIT
12
 * @link      http://github.com/nfephp-org/sped-da for the canonical source repository
13
 * @author    Roberto L. Machado <linux at rlm dot gmail dot com>
14
 */
15
16
use NFePHP\DA\Legacy\Common;
17
18
class DaCommon extends Common
19
{
20
    /**
21
     * @var bool
22
     */
23
    protected $debugmode;
24
    /**
25
     * @var string
26
     */
27
    protected $orientacao;
28
    /**
29
     * @var string|array
30
     */
31
    protected $papel = 'A4';
32
    /**
33
     * @var int
34
     */
35
    protected $margsup = 2;
36
    /**
37
     * @var int
38
     */
39
    protected $marginf = 2;
40
    /**
41
     * @var int
42
     */
43
    protected $margesq = 2;
44
    /**
45
     * @var float
46
     */
47
    protected $wPrint;
48
    /**
49
     * @var float
50
     */
51
    protected $hPrint;
52
    /**
53
     * @var float
54
     */
55
    protected $xIni;
56
    /**
57
     * @var float
58
     */
59
    protected $yIni;
60
    /**
61
     * @var float
62
     */
63
    protected $maxH;
64
    /**
65
     * @var float
66
     */
67
    protected $maxW;
68
    /**
69
     * @var string
70
     */
71
    protected $fontePadrao = 'times';
72
    /**
73
     * @var array
74
     */
75
    protected $aFont = ['font' => 'times', 'size' => 8, 'style' => ''];
76
    /**
77
     * @var string
78
     */
79
    protected $creditos;
80
    /**
81
     * @var bool
82
     */
83
    protected $powered = true;
84
    /**
85
     * @var string
86
     */
87
    protected $logomarca;
88
    /**
89
     * @var string
90
     */
91
    protected $logoAlign = 'C';
92
    /**
93
     * @var \NFePHP\DA\Legacy\Pdf
94
     */
95
    protected $pdf;
96
    /**
97
     * @var string
98
     */
99
    protected $numdepec;
100
    /**
101
     * @var int
102
     */
103
    protected $decimalPlaces;
104
    /**
105
     * @var bool
106
     */
107
    protected $cancelFlag = false;
108
109
    /**
110
     * Ativa ou desativa o modo debug
111
     * @param bool $activate Ativa ou desativa o modo debug
112
     * @return bool
113
     * @throws \Exception
114
     */
115
    public function debugMode($activate = null)
116
    {
117
        if (isset($activate) && is_bool($activate)) {
118
            $this->debugmode = $activate;
119
        }
120
        if ($this->debugmode) {
121
            //ativar modo debug
122
            error_reporting(E_ALL);
123
            ini_set('display_errors', 'On');
124
            set_error_handler(function (int $errnum, string $errmsg, string $errfile, int $errline) {
125
                throw new \Exception("Erro identificado $errnum: '$errmsg' $errfile [linha:" . $errline . "]");
126
            });
127
        } else {
128
            //desativar modo debug
129
            error_reporting(0);
130
            ini_set('display_errors', 'Off');
131
        }
132
        return $this->debugmode;
133
    }
134
135
    /**
136
     * Define parametros de impressão
137
     * @param string $orientacao
138
     * @param string $papel
139
     * @param int $margSup
140
     * @param int $margEsq
141
     * @return void
142
     */
143
    public function printParameters(
144
        $orientacao = '',
145
        $papel = 'A4',
146
        $margSup = 2,
147
        $margEsq = 2
148
    ) {
149
        if ($orientacao === 'P' || $orientacao === 'L') {
150
            $this->orientacao = $orientacao;
151
        }
152
        $p = strtoupper($papel);
153
        if ($p == 'A4' || $p == 'LEGAL') {
154
            $this->papel = $papel;
155
        }
156
        $this->margsup = $margSup ?? 2;
157
        $this->margesq = $margEsq ?? 2;
158
        $this->marginf = $margSup ?? 2;
159
        if (strtoupper($this->papel) == 'A4') {
160
            $this->maxW = 210;
0 ignored issues
show
Documentation Bug introduced by
The property $maxW was declared of type double, but 210 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
161
            $this->maxH = 297;
0 ignored issues
show
Documentation Bug introduced by
The property $maxH was declared of type double, but 297 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
162
        } else {
163
            $this->papel = 'legal';
164
            $this->maxW = 216;
0 ignored issues
show
Documentation Bug introduced by
The property $maxW was declared of type double, but 216 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
165
            $this->maxH = 355;
0 ignored issues
show
Documentation Bug introduced by
The property $maxH was declared of type double, but 355 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
166
        }
167
        if ($this->orientacao == 'L') {
168
            if (strtoupper($this->papel) == 'A4') {
169
                $this->maxH = 210;
0 ignored issues
show
Documentation Bug introduced by
The property $maxH was declared of type double, but 210 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
170
                $this->maxW = 297;
0 ignored issues
show
Documentation Bug introduced by
The property $maxW was declared of type double, but 297 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
171
            } else {
172
                $this->papel = 'legal';
173
                $this->maxH = 216;
0 ignored issues
show
Documentation Bug introduced by
The property $maxH was declared of type double, but 216 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
174
                $this->maxW = 355;
0 ignored issues
show
Documentation Bug introduced by
The property $maxW was declared of type double, but 355 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
175
            }
176
        }
177
        $this->wPrint = $this->maxW - $this->margesq * 2;
0 ignored issues
show
Documentation Bug introduced by
The property $wPrint was declared of type double, but $this->maxW - $this->margesq * 2 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
178
        $this->hPrint = $this->maxH - $this->margsup - 5;
0 ignored issues
show
Documentation Bug introduced by
The property $hPrint was declared of type double, but $this->maxH - $this->margsup - 5 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
179
    }
180
181
    /**
182
     * Set logo e sua posição
183
     * @param string $logo
184
     * @param string $logoAlign
185
     * @param bool $mode_bw se true converte a imagem em branco e preto
186
     * @return void
187
     */
188
    public function logoParameters($logo, $logoAlign = null, $mode_bw = false)
189
    {
190
        if (!empty($logoAlign)) {
191
            $this->logoAlign = $logoAlign;
192
        }
193
        $this->logomarca = $this->adjustImage($logo, $mode_bw);
194
    }
195
196
    /**
197
     * Numero DPEC
198
     * @param string $numdepec
199
     * @return void
200
     */
201
    public function depecNumber($numdepec)
202
    {
203
        $this->numdepec = $numdepec;
204
    }
205
206
    /**
207
     * Renderiza o pdf e retorna como raw
208
     * @param string $logo
209
     * @return string
210
     */
211
    public function render(
212
        $logo = ''
213
    ) {
214
        if (empty($this->pdf)) {
215
            $this->monta($logo);
216
        }
217
        return $this->pdf->getPdf();
218
    }
219
220
    /**
221
     * Add the credits to the integrator in the footer message
222
     * @param string $message Mensagem do integrador a ser impressa no rodapé das paginas
223
     * @return void
224
     */
225
    public function creditsIntegratorFooter($message = '', $powered = true)
226
    {
227
        $this->creditos = trim($message);
228
        $this->powered = $powered;
229
    }
230
231
    /**
232
     * Seta a fonte padrão é times
233
     * @param string $font
234
     */
235
    public function setDefaultFont(string $font = 'times')
236
    {
237
        $this->fontePadrao = $font;
238
    }
239
240
    /**
241
     * Seta o numero de casas decimais a serem usadas como padrão
242
     * @param int $dec
243
     */
244
    public function setDefaultDecimalPlaces($dec)
245
    {
246
        if ($dec > 4) {
247
            $dec = 4;
248
        }
249
        if ($dec < 2) {
250
            $dec = 2;
251
        }
252
        $this->decimalPlaces = $dec;
253
    }
254
255
    /**
256
     * Metodo de montagem do PDF
257
     * @param string $logo
258
     */
259
    protected function monta($logo = null)
260
    {
261
        //todo replaced in other classes
262
    }
263
264
    /**
265
     * Ajusta a imagem do logo
266
     * @param string $logo
267
     * @param bool $turn_bw
268
     * @return string
269
     * @throws \Exception
270
     */
271
    protected function adjustImage($logo, $turn_bw = false)
272
    {
273
        if (empty($logo)) {
274
            return null;
275
        }
276
        if (substr($logo, 0, 24) !== 'data://text/plain;base64') {
277
            if (is_file($logo)) {
278
                $logo = 'data://text/plain;base64,'. base64_encode(file_get_contents($logo));
279
            } else {
280
                //se não é uma string e nem um file retorna nulo
281
                return null;
282
            }
283
        }
284
        $logoInfo = getimagesize($logo);
285
        //1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order),
286
        //8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC,
287
        //14 = IFF, 15 = WBMP, 16 = XBM
288
        $type = $logoInfo[2];
289
        if ($type != '2' && $type != '3') {
290
            throw new \Exception('O formato da imagem não é aceitável! Somente PNG ou JPG podem ser usados.');
291
        }
292
        if ($type == '3') { //3 = PNG
293
            $image = imagecreatefrompng($logo);
294
            if ($turn_bw) {
295
                imagefilter($image, IMG_FILTER_GRAYSCALE);
296
                //imagefilter($image, IMG_FILTER_CONTRAST, -100);
297
            }
298
            return $this->getImageStringFromObject($image);
299
        } elseif ($type == '2' && $turn_bw) {
300
            $image = imagecreatefromjpeg($logo);
301
            imagefilter($image, IMG_FILTER_GRAYSCALE);
302
            //imagefilter($image, IMG_FILTER_CONTRAST, -100);
303
            return $this->getImageStringFromObject($image);
304
        }
305
        return $logo;
306
    }
307
308
    /**
309
     * Cria uma imagem JPEG com o objeto GD
310
     * @param resource $image
311
     * @return string
312
     */
313
    private function getImageStringFromObject($image)
314
    {
315
        ob_start();
316
        imagejpeg($image, null, 100);
317
        imagedestroy($image);
318
        $logo = ob_get_contents(); // read from buffer
319
        ob_end_clean();
320
        return 'data://text/plain;base64,'.base64_encode($logo);
321
    }
322
323
    /**
324
     * Atribui uma sinalização de cancelamento externa
325
     * @param bool $cancelFlag
326
     */
327
    public function setCancelFlag($cancelFlag = true)
328
    {
329
        $this->cancelFlag = filter_var($cancelFlag, FILTER_VALIDATE_BOOLEAN);
330
    }
331
}
332