Passed
Push — master ( e7d3e1...34e870 )
by Jhao
01:48
created

Compiler::compileBatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
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;
16
17
use PhpAidc\LabelPrinter\Label\Batch;
18
use PhpAidc\LabelPrinter\Contract\Job;
19
use PhpAidc\LabelPrinter\Contract\Label;
20
use PhpAidc\LabelPrinter\Contract\Language;
21
22
final class Compiler
23
{
24
    /** @var Language */
25
    private $language;
26
27 4
    public static function create(Language $language): self
28
    {
29 4
        return new self($language);
30
    }
31
32 4
    public function __construct(Language $language)
33
    {
34 4
        $this->language = $language;
35 4
    }
36
37 3
    public function compile(Job $job): string
38
    {
39 3
        $instructions = [];
40
41 3
        if ($job instanceof Label) {
42 3
            $instructions[] = $this->compileLabel($job);
43
        }
44
45 3
        if ($job instanceof Batch) {
46
            $instructions[] = $this->compileBatch($job);
47
        }
48
49
        $payload = \array_reduce($instructions, static function ($carry, $item) {
50 3
            return $item instanceof \Generator
51 3
                ? \array_merge($carry, \iterator_to_array($item, false))
52 3
                : \array_merge($carry, $item);
53 3
        }, []);
54
55 3
        return \implode($payload);
56
    }
57
58 3
    private function compileLabel(Label $label): iterable
59
    {
60 3
        yield from $this->language->compileDeclaration($label);
61
62 3
        foreach ($label->getCommands(\get_class($this->language)) as $command) {
63 3
            if ($this->language->isSupport($command)) {
64 3
                yield from $this->language->compileCommand($command);
65
            }
66
        }
67
68 3
        yield from $this->language->compilePrint($label->getCopies());
0 ignored issues
show
Bug introduced by
The method getCopies() does not exist on PhpAidc\LabelPrinter\Contract\Label. Since it exists in all sub-types, consider adding an abstract or default implementation to PhpAidc\LabelPrinter\Contract\Label. ( Ignorable by Annotation )

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

68
        yield from $this->language->compilePrint($label->/** @scrutinizer ignore-call */ getCopies());
Loading history...
69 3
    }
70
71
    private function compileBatch(Batch $batch): iterable
72
    {
73
        foreach ($batch->getLabels() as $label) {
74
            yield from $this->compileLabel($label);
75
        }
76
    }
77
}
78