QrGenerator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 54
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getQrString() 0 9 1
A image() 0 27 4
A getQrContent() 0 11 1
1
<?php
2
3
/**
4
 * MÓDULO DE EMISIÓN ELECTRÓNICA F72X
5
 * UBL 2.1
6
 * Version 1.0
7
 * 
8
 * Copyright 2019, Jaime Cruz
9
 */
10
11
namespace F72X\Tools;
12
13
use F72X\F72X;
14
use F72X\Sunat\DataMap;
15
use F72X\Sunat\Operations;
16
use F72X\Company;
17
use Codelint\QRCode\QRCode;
18
19
class QrGenerator {
20
21
    public static function getQrString(DataMap $inv) {
22
        $documentName = $inv->getDocumentName();
23
        $qr = new QRCode();
24
        $qrContent = self::getQrContent($inv);
25
        $qrTempPath = F72X::getTempDir() . "/QR-$documentName.png";
26
        $qr->png($qrContent, $qrTempPath, 'Q', 8, 2);
27
        $qrs = base64_encode(file_get_contents($qrTempPath));
28
        unlink($qrTempPath);
29
        return $qrs;
30
    }
31
32
    //----------------------------------------------------------------------
33
    private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) {
0 ignored issues
show
Unused Code introduced by
The method image() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
34
        $h = count($frame);
35
        $w = strlen($frame[0]);
36
37
        $imgW = $w + 2 * $outerFrame;
38
        $imgH = $h + 2 * $outerFrame;
39
40
        $base_image = ImageCreate($imgW, $imgH);
41
42
        $col[0] = ImageColorAllocate($base_image, 255, 255, 255);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$col was never initialized. Although not strictly required by PHP, it is generally a good practice to add $col = array(); before regardless.
Loading history...
43
        $col[1] = ImageColorAllocate($base_image, 0, 0, 0);
44
45
        imagefill($base_image, 0, 0, $col[0]);
46
47
        for ($y = 0; $y < $h; $y++) {
48
            for ($x = 0; $x < $w; $x++) {
49
                if ($frame[$y][$x] == '1') {
50
                    ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]);
51
                }
52
            }
53
        }
54
55
        $target_image = ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
56
        ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
57
        ImageDestroy($base_image);
58
59
        return $target_image;
60
    }
61
62
    private static function getQrContent(DataMap $inv) {
63
        $ruc = Company::getRUC();
64
        $invoiveType = $inv->getDocumentType();
65
        $documentSeries = $inv->getDocumentSeries();
66
        $seriesNumber = $inv->getDocumentNumber();
67
        $igv = Operations::formatAmount($inv->getIGV());
68
        $payableAmount = Operations::formatAmount($inv->getPayableAmount());
69
        $issueDate = $inv->getIssueDate()->format('Y-m-d');
70
        $customerDocType = $inv->getCustomerDocType();
71
        $customerDocNumber = $inv->getCustomerDocNumber();
72
        return "$ruc|$invoiveType|$documentSeries|$seriesNumber|$igv|$payableAmount|$issueDate|$customerDocType|$customerDocNumber";
73
    }
74
75
}
76