Passed
Pull Request — master (#4)
by
unknown
02:09
created

ConvenioValidator::verifyBarcode()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.0493

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 33
ccs 16
cts 18
cp 0.8889
rs 9.0777
cc 6
nc 6
nop 1
crap 6.0493
1
<?php
2
3
namespace Claudsonm\BoletoWinner\Validators;
4
5
use Claudsonm\BoletoWinner\Convenio;
6
7
class ConvenioValidator implements Validator
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12 15
    public function verifyWritableLine(string $writableLine): bool
13
    {
14 15
        $writableLine = preg_replace('/[^0-9]/', '', $writableLine);
15
16 15
        if (strlen($writableLine) != 48) {
17 9
            return false;
18
        }
19
20 7
        if ($writableLine[0] != '8') {
21 1
            return false;
22
        }
23
24 7
        if (! in_array($writableLine[1], Convenio::SEGMENT_IDENTIFICATION)) {
25
            return false;
26
        }
27
28 7
        $blocks = str_split($writableLine, 12);
29
30 7
        $isModule10 = in_array($writableLine[2], Convenio::USES_MODULE_10);
31
32 7
        $blocksValidated = 0;
33 7
        foreach ($blocks as $block) {
34
35 7
            $blockLength = strlen($block) - 1;
36
37 7
            $blockInputNumbers = substr($block, 0, $blockLength);
38
39 7
            $blockVerificationDigit = $this->getLastCharacter($block);
40
41 7
            $digitCalculated = $isModule10 ? module10($blockInputNumbers) : module11($blockInputNumbers);
42
43 7
            if ($digitCalculated === $blockVerificationDigit) {
44 7
                $blocksValidated++;
45
            }
46
        }
47
48 7
        return 4 === $blocksValidated;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 17
    public function verifyBarcode(string $barcode): bool
55
    {
56 17
        $barcode = preg_replace('/[^0-9]/', '', $barcode);
57
        
58 17
        if (strlen($barcode) != 44) {
59 8
            return false;
60
        }
61
62 9
        if ($barcode[0] != '8') {
63 5
            return false;
64
        }
65
66 5
        if (! in_array($barcode[1], Convenio::SEGMENT_IDENTIFICATION)) {
67
            return false;
68
        }
69
70 5
        $effectiveValueOrReferenceIdentifier = $barcode[2];
71
72 5
        if (! in_array($effectiveValueOrReferenceIdentifier, Convenio::AVAILABLE_CODES)) {
73
            return false;
74
        }
75
76 5
        $barcodeVerificationDigit = $barcode[3];
77
78 5
        $blockInputNumbers = substr($barcode, 0, 3);
79
80 5
        $blockInputNumbers .= substr($barcode, 4);
81
82 5
        $digitCalculated = in_array($effectiveValueOrReferenceIdentifier, Convenio::USES_MODULE_10)
83 5
            ? module10($blockInputNumbers)
84 5
            : module11($blockInputNumbers);
85
86 5
        return $digitCalculated === $barcodeVerificationDigit;
87
    }
88
89
    /**
90
     * Returns the last character of the input string.
91
     */
92 7
    private function getLastCharacter(string $input): string
93
    {
94 7
        return substr($input, -1);
95
    }
96
}
97