Tspl::compileCommand()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 12
rs 10
c 0
b 0
f 0
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\InternalImage;
24
use PhpAidc\LabelPrinter\Contract\Label;
25
use PhpAidc\LabelPrinter\Contract\Media;
26
use PhpAidc\LabelPrinter\Contract\Command;
27
use PhpAidc\LabelPrinter\Contract\Language;
28
use PhpAidc\LabelPrinter\Enum\Unit;
29
use PhpAidc\LabelPrinter\Enum\Charset;
30
use PhpAidc\LabelPrinter\Language\Tspl as Handlers;
31
32
final class Tspl implements Language
33
{
34
    private const EOC = "\n";
35
36
    private const HANDLERS = [
37
        Raw::class           => Handlers\TsplRaw::class,
38
        Clear::class         => Handlers\TsplClear::class,
39
        Bitmap::class        => Handlers\TsplBitmap::class,
40
        Barcode::class       => Handlers\TsplBarcode::class,
41
        TextLine::class      => Handlers\TsplTextLine::class,
42
        TextBlock::class     => Handlers\TsplTextBlock::class,
43
        InternalImage::class => Handlers\TsplInternalImage::class,
44
    ];
45
46
    public function compileDeclaration(Label $label): iterable
47
    {
48
        yield from $this->translateMedia($label->getMedia());
49
        yield from $this->translateCharset($label->getCharset());
50
    }
51
52
    public function isSupport(Command $command): bool
53
    {
54
        return isset(self::HANDLERS[\get_class($command)]);
55
    }
56
57
    public function compilePrint(int $copies): iterable
58
    {
59
        if ($copies <= 0) {
60
            throw new \InvalidArgumentException('Number of copies must be greather than 0.');
61
        }
62
63
        yield "PRINT 1,{$copies}".self::EOC;
64
    }
65
66
    public function compileCommand(Command $command): iterable
67
    {
68
        if ($this->isSupport($command)) {
69
            $handler = self::HANDLERS[\get_class($command)];
70
71
            foreach ((new $handler())->translate($command) as $instruction) {
72
                yield $instruction.self::EOC;
73
            }
74
        } else {
75
            // throw exception
76
            yield null;
77
        }
78
    }
79
80
    private function translateCharset(?Charset $charset): iterable
81
    {
82
        if ($charset) {
83
            yield \sprintf('CODEPAGE %s', $charset->getValue()).self::EOC;
84
        }
85
    }
86
87
    private function translateMedia(Media $media): iterable
88
    {
89
        if ($media->getWidth() && $media->getHeight() === null) {
90
            yield \sprintf('SIZE %s'.self::EOC, $this->valueWithUnit($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...e\Tspl::valueWithUnit() 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

90
            yield \sprintf('SIZE %s'.self::EOC, $this->valueWithUnit($media->getWidth(), /** @scrutinizer ignore-type */ $media->getUnit()));
Loading history...
91
92
            return;
93
        }
94
95
        if ($media->getWidth() && $media->getHeight()) {
96
            yield \vsprintf('SIZE %s,%s'.self::EOC, [
97
                $this->valueWithUnit($media->getWidth(), $media->getUnit()),
98
                $this->valueWithUnit($media->getHeight(), $media->getUnit()),
99
            ]);
100
        }
101
    }
102
103
    private function valueWithUnit(float $value, Unit $unit): string
104
    {
105
        if ($unit->equals(Unit::IN())) {
106
            return (string) $value;
107
        }
108
109
        return "{$value} {$unit->getValue()}";
110
    }
111
}
112