Passed
Pull Request — master (#1)
by Claudson
01:45
created

Bill::setBarcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Claudsonm\BoletoWinner;
4
5
use Claudsonm\BoletoWinner\Converters\Converter;
6
use Claudsonm\BoletoWinner\Validators\Validator;
7
8
abstract class Bill
9
{
10
    protected string $writableLine;
11
    protected string $barcode;
12
    protected Validator $validator;
13
    protected Converter $converter;
14
15 122
    public function __construct()
16
    {
17 122
        $this->converter = $this->useConverter();
18 122
        $this->validator = $this->useValidator();
19 122
    }
20
21 96
    public function setBarcode(string $barcode): self
22
    {
23 96
        $this->barcode = $barcode;
24
25 96
        return $this;
26
    }
27
28 95
    public function setWritableLine(string $writableLine): self
29
    {
30 95
        $this->writableLine = $writableLine;
31
32 95
        return $this;
33
    }
34
35 55
    public function getWritableLine(): string
36
    {
37 55
        if (empty($this->writableLine)) {
38 26
            $this->barcodeToWritableLine();
39
        }
40
41 55
        return $this->writableLine;
42
    }
43
44 55
    public function getBarcode(): string
45
    {
46 55
        if (empty($this->barcode)) {
47 28
            $this->writableLineToBarcode();
48
        }
49
50 55
        return $this->barcode;
51
    }
52
53
    /**
54
     * Determines via the delegated validator if the writable line is valid.
55
     */
56 46
    public function isWritableLineValid(): bool
57
    {
58 46
        return $this->validator->verifyWritableLine($this->writableLine);
59
    }
60
61
    /**
62
     * Determines via the delegated validator if the barcode is valid.
63
     */
64 48
    public function isBarcodeValid(): bool
65
    {
66 48
        return $this->validator->verifyBarcode($this->barcode);
67
    }
68
69
    /**
70
     * Fills the writable line property with the output of the delegated
71
     * converter strategy class.
72
     */
73 26
    protected function barcodeToWritableLine(): void
74
    {
75 26
        $this->setWritableLine($this->converter->toWritableLine($this));
76 26
    }
77
78
    /**
79
     * Fills the barcode property with the output of the delegated converter
80
     * strategy class.
81
     */
82 28
    protected function writableLineToBarcode(): void
83
    {
84 28
        $this->setBarcode($this->converter->toBarcode($this));
85 28
    }
86
87
    /**
88
     * Defines which converter strategy class is used with the bill instance.
89
     */
90
    abstract protected function useConverter(): Converter;
91
92
    /**
93
     * Defines which validator strategy class is used with the bill instance.
94
     */
95
    abstract protected function useValidator(): Validator;
96
}
97