Passed
Push — master ( ec0d02...c0f595 )
by Jan
06:31 queued 10s
created

BarcodeGenerator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getContent() 0 14 7
B generateSVG() 0 29 7
A __construct() 0 3 1
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Services\LabelSystem;
22
23
use App\Entity\LabelSystem\LabelOptions;
24
use App\Services\LabelSystem\Barcodes\BarcodeContentGenerator;
25
use Com\Tecnick\Barcode\Barcode;
26
27
final class BarcodeGenerator
28
{
29
    private $barcodeContentGenerator;
30
31
    public function __construct(BarcodeContentGenerator $barcodeContentGenerator)
32
    {
33
        $this->barcodeContentGenerator = $barcodeContentGenerator;
34
    }
35
36
    public function generateSVG(LabelOptions $options, object $target): ?string
37
    {
38
        $barcode = new Barcode();
39
40
        switch ($options->getBarcodeType()) {
41
            case 'qr':
42
                $type = 'QRCODE';
43
                break;
44
            case 'datamatrix':
45
                $type = 'DATAMATRIX';
46
                break;
47
            case 'code39':
48
                $type = 'C39';
49
                break;
50
            case 'code93':
51
                $type = 'C93';
52
                break;
53
            case 'code128':
54
                $type = 'C128A';
55
                break;
56
            case 'none':
57
                return null;
58
            default:
59
                throw new \InvalidArgumentException('Unknown label type!');
60
        }
61
62
        $bobj = $barcode->getBarcodeObj($type, $this->getContent($options, $target));
63
64
        return $bobj->getSvgCode();
65
    }
66
67
    public function getContent(LabelOptions $options, object $target): ?string
68
    {
69
        switch ($options->getBarcodeType()) {
70
            case 'qr':
71
            case 'datamatrix':
72
                return $this->barcodeContentGenerator->getURLContent($target);
73
            case 'code39':
74
            case 'code93':
75
            case 'code128':
76
                return $this->barcodeContentGenerator->get1DBarcodeContent($target);
77
            case 'none':
78
                return null;
79
            default:
80
                throw new \InvalidArgumentException('Unknown label type!');
81
        }
82
    }
83
84
}