Passed
Push — master ( e5baf6...0dce86 )
by Jhao
02:41
created

Printer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
c 3
b 0
f 0
dl 0
loc 35
ccs 13
cts 16
cp 0.8125
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A ask() 0 5 1
A send() 0 3 1
A print() 0 9 2
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\Contract\Job;
18
use PhpAidc\LabelPrinter\Contract\Connector;
19
20
final class Printer
21
{
22
    /** @var Connector */
23
    private $connector;
24
25
    /** @var Compiler|null */
26
    private $compiler;
27
28 3
    public function __construct(Connector $connector, ?Compiler $compiler = null)
29
    {
30 3
        $this->connector = $connector;
31 3
        $this->compiler = $compiler;
32 3
    }
33
34 2
    public function print(Job $job): void
35
    {
36 2
        if ($this->compiler === null) {
37 1
            throw new \DomainException(
38 1
                'The Printer object should be constructed with Compiler instance for printing.'
39
            );
40
        }
41
42 1
        $this->connector->write($this->compiler->compile($job));
43 1
    }
44
45 1
    public function send($payload): void
46
    {
47 1
        $this->connector->write($payload);
48 1
    }
49
50
    public function ask(string $message)
51
    {
52
        $this->connector->write($message);
53
54
        return $this->connector->read();
55
    }
56
}
57