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

BarcodeContentGenerator::getURLContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
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\Barcodes;
22
23
24
use App\Entity\Base\AbstractDBElement;
25
use App\Entity\Parts\Manufacturer;
26
use App\Entity\Parts\Part;
27
use App\Entity\Parts\PartLot;
28
use App\Entity\Parts\Storelocation;
29
use App\Entity\Parts\Supplier;
30
use App\Exceptions\EntityNotSupportedException;
31
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
32
33
final class BarcodeContentGenerator
34
{
35
    private $urlGenerator;
36
37
    public const PREFIX_MAP = [
38
        Part::class => 'P',
39
        PartLot::class => 'L',
40
        Storelocation::class => 'S'
41
    ];
42
43
    private const URL_MAP = [
44
        Part::class => 'part',
45
        PartLot::class => 'lot',
46
        Storelocation::class => 'location',
47
    ];
48
49
    public function __construct(UrlGeneratorInterface $urlGenerator)
50
    {
51
        $this->urlGenerator = $urlGenerator;
52
    }
53
54
    /**
55
     * Generates a fixed URL to the given Element that can be embedded in a 2D code (e.g. QR code).
56
     * @param  AbstractDBElement  $target
57
     * @return string
58
     */
59
    public function getURLContent(AbstractDBElement $target): string
60
    {
61
        $type = $this->classToString(self::URL_MAP, $target);
62
63
        return  $this->urlGenerator->generate('scan_qr', [
64
            'type' => $type,
65
            'id' => $target->getID() ?? 0,
66
            '_locale' => null,
67
        ], UrlGeneratorInterface::ABSOLUTE_URL);
68
    }
69
70
    /**
71
     * Returns a Code that can be used in a 1D barcode.
72
     * The return value has a format of "L0123"
73
     * @param  AbstractDBElement  $target
74
     * @return string
75
     */
76
    public function get1DBarcodeContent(AbstractDBElement $target): string
77
    {
78
        $prefix = $this->classToString(self::PREFIX_MAP, $target);
79
        $id = sprintf('%04d', $target->getID() ?? 0);
80
        return $prefix  . $id;
81
    }
82
83
    private function classToString(array $map, object $target): string
84
    {
85
        $class = get_class($target);
86
        if (isset($map[$class])) {
87
            return $map[$class];
88
        }
89
90
        foreach($map as $class => $string) {
91
            if (is_a($target, $class)) {
92
                return $string;
93
            }
94
        }
95
96
        throw new \InvalidArgumentException('Unknown object class ' . get_class($target));
97
    }
98
}