ConvenioValidator::getLastCharacter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
        $blocks = str_split($writableLine, 12);
15 15
        $isModule10 = in_array($writableLine[2], Convenio::USES_MODULE_10);
16
17 15
        $blocksValidated = 0;
18 15
        foreach ($blocks as $block) {
19 15
            $blockLength = strlen($block) - 1;
20 15
            $blockInputNumbers = substr($block, 0, $blockLength);
21 15
            $blockVerificationDigit = $this->getLastCharacter($block);
22 15
            $digitCalculated = $isModule10 ? module10($blockInputNumbers) : module11($blockInputNumbers);
23 15
            if ($digitCalculated === $blockVerificationDigit) {
24 9
                $blocksValidated++;
25
            }
26
        }
27
28 15
        return 4 === $blocksValidated;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 17
    public function verifyBarcode(string $barcode): bool
35
    {
36 17
        $effectiveValueOrReferenceIdentifier = $barcode[2];
37 17
        if (! in_array($effectiveValueOrReferenceIdentifier, Convenio::AVAILABLE_CODES)) {
38 3
            return false;
39
        }
40
41 14
        $barcodeVerificationDigit = $barcode[3];
42 14
        $blockInputNumbers = substr($barcode, 0, 3);
43 14
        $blockInputNumbers .= substr($barcode, 4);
44 14
        $digitCalculated = in_array($effectiveValueOrReferenceIdentifier, Convenio::USES_MODULE_10)
45 12
            ? module10($blockInputNumbers)
46 14
            : module11($blockInputNumbers);
47
48 14
        return $digitCalculated === $barcodeVerificationDigit;
49
    }
50
51
    /**
52
     * Returns the last character of the input string.
53
     */
54 15
    private function getLastCharacter(string $input): string
55
    {
56 15
        return substr($input, -1);
57
    }
58
}
59