Fingerprint::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of PhpAidc LabelPrinter package.
5
 *
6
 * © Appwilio (https://appwilio.com)
7
 * © JhaoDa (https://github.com/jhaoda)
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace PhpAidc\LabelPrinter\Language;
16
17
use PhpAidc\LabelPrinter\Command\Raw;
18
use PhpAidc\LabelPrinter\Command\Clear;
19
use PhpAidc\LabelPrinter\Command\Bitmap;
20
use PhpAidc\LabelPrinter\Command\Barcode;
21
use PhpAidc\LabelPrinter\Command\TextLine;
22
use PhpAidc\LabelPrinter\Command\TextBlock;
23
use PhpAidc\LabelPrinter\Command\ExternalImage;
24
use PhpAidc\LabelPrinter\Command\InternalImage;
25
use PhpAidc\LabelPrinter\Contract\Label;
26
use PhpAidc\LabelPrinter\Contract\Media;
27
use PhpAidc\LabelPrinter\Contract\Command;
28
use PhpAidc\LabelPrinter\Contract\Language;
29
use PhpAidc\LabelPrinter\Enum\Unit;
30
use PhpAidc\LabelPrinter\Enum\Charset;
31
use PhpAidc\LabelPrinter\Language\Fingerprint as Handlers;
32
33
final class Fingerprint implements Language
34
{
35
    public const DEFAULT_ANCHOR = 1;
36
    public const DEFAULT_DIRECTION = 1;
37
    public const DEFAULT_BARCODE_HEIGHT = 100;
38
    public const DEFAULT_BARCODE_MAGNIFICATION = 2;
39
40
    private const EOC = "\n";
41
42
    private const HANDLERS = [
43
        Raw::class           => Handlers\FingerprintRaw::class,
44
        Clear::class         => Handlers\FingerprintClear::class,
45
        Bitmap::class        => Handlers\FingerprintBitmap::class,
46
        Barcode::class       => Handlers\FingerprintBarcode::class,
47
        TextLine::class      => Handlers\FingerprintTextLine::class,
48
        TextBlock::class     => Handlers\FingerprintTextBlock::class,
49
        ExternalImage::class => Handlers\FingerprintExternalImage::class,
50
        InternalImage::class => Handlers\FingerprintInternalImage::class,
51
    ];
52
53
    /** @var float */
54
    private $density;
55
56
    public function __construct(float $density)
57
    {
58
        $this->density = $density;
59
    }
60
61
    public function compileDeclaration(Label $label): iterable
62
    {
63
        yield from $this->translateMedia($label->getMedia());
64
        yield from $this->translateCharset($label->getCharset());
65
    }
66
67
    public function isSupport(Command $command): bool
68
    {
69
        return isset(self::HANDLERS[\get_class($command)]);
70
    }
71
72
    public function compileCommand(Command $command): iterable
73
    {
74
        if ($this->isSupport($command)) {
75
            $class = self::HANDLERS[\get_class($command)];
76
77
            $handler = new $class();
78
79
            foreach ((new $handler())->translate($command) as $instruction) {
80
                yield $instruction.self::EOC;
81
            }
82
        } else {
83
            // throw exception
84
            yield null;
85
        }
86
    }
87
88
    public function compilePrint(int $copies): iterable
89
    {
90
        if ($copies <= 0) {
91
            throw new \InvalidArgumentException('Number of copies must be greather than 0.');
92
        }
93
94
        yield "PF {$copies}".self::EOC;
95
    }
96
97
    private function translateCharset(?Charset $charset): iterable
98
    {
99
        if ($charset) {
100
            yield \sprintf('NASC "%s"', $charset->getValue()).self::EOC;
101
        }
102
    }
103
104
    private function translateMedia(Media $media): iterable
105
    {
106
        if ($media->getWidth()) {
107
            yield \sprintf('SETUP "MEDIA,MEDIA SIZE,WIDTH,%d'.self::EOC, $this->valueToDots($media->getWidth(), $media->getUnit()));
0 ignored issues
show
Bug introduced by
It seems like $media->getUnit() can also be of type null; however, parameter $unit of PhpAidc\LabelPrinter\Lan...gerprint::valueToDots() does only seem to accept PhpAidc\LabelPrinter\Enum\Unit, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
            yield \sprintf('SETUP "MEDIA,MEDIA SIZE,WIDTH,%d'.self::EOC, $this->valueToDots($media->getWidth(), /** @scrutinizer ignore-type */ $media->getUnit()));
Loading history...
108
        }
109
110
        if ($media->getHeight()) {
111
            yield \sprintf('SETUP "MEDIA,MEDIA SIZE,HEIGHT,%d'.self::EOC, $this->valueToDots($media->getHeight(), $media->getUnit()));
112
        }
113
    }
114
115
    private function valueToDots(float $value, Unit $unit): int
116
    {
117
        if ($unit->equals(Unit::DOT())) {
118
            return (int) $value;
119
        }
120
121
        if ($unit->equals(Unit::IN())) {
122
            return (int) \round($this->density * $value);
123
        }
124
125
        return (int) \round(($this->density / 2.54 / 10) * $value);
126
    }
127
}
128