Test Setup Failed
Pull Request — master (#1)
by Claudson
09:10 queued 02:23
created

BoletoWinner::toWritableLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Claudsonm\BoletoWinner;
4
5
use BadMethodCallException;
6
use Claudsonm\BoletoWinner\Exceptions\BoletoWinnerException;
7
use Claudsonm\BoletoWinner\Factories\BillFactory;
8
9
/**
10
 * @method static bool isValidBoleto(string $barcodeOrWritableLine)
11
 * @method static bool isValidConvenio(string $barcodeOrWritableLine)
12
 *
13
 * @see BillFactory
14
 */
15
class BoletoWinner
16
{
17
    /**
18
     * Handle dynamic, static calls to the object.
19
     *
20
     * @param string $method
21
     * @param array  $arguments
22
     *
23
     * @throws BadMethodCallException
24
     * @throws BoletoWinnerException
25
     *
26
     * @return bool
27
     */
28
    public static function __callStatic($method, $arguments)
29
    {
30
        if (starts_with('isValid', $method)) {
31
            return self::handleIsValidTypeCall($method, $arguments);
32
        }
33
34
        throw new BadMethodCallException("Method `{$method}` does not exist.");
35
    }
36
37
    /**
38
     * @throws BoletoWinnerException
39
     */
40
    public static function makeBill(string $barcodeOrWritableLine): Bill
41
    {
42
        return BillFactory::getInstance()
43
            ->createFromBarcodeOrWritableLine($barcodeOrWritableLine);
44
    }
45
46
    /**
47
     * @throws BoletoWinnerException
48
     */
49
    public static function toWritableLine(string $barcode): string
50
    {
51
        return BillFactory::getInstance()
52
            ->createFromBarcode($barcode)
53
            ->getWritableLine();
54
    }
55
56
    /**
57
     * @throws BoletoWinnerException
58
     */
59
    public static function toBarcode(string $writableLine): string
60
    {
61
        return BillFactory::getInstance()
62
            ->createFromWritableLine($writableLine)
63
            ->getBarcode();
64
    }
65
66
    public static function isValid(string $barcodeOrWritableLine): bool
67
    {
68
        try {
69
            BillFactory::getInstance()->createFromBarcodeOrWritableLine($barcodeOrWritableLine);
70
71
            return true;
72
        } catch (BoletoWinnerException $exception) {
73
            return false;
74
        }
75
    }
76
77
    public static function isValidWritableLine(string $writableLine): bool
78
    {
79
        try {
80
            BillFactory::getInstance()->createFromWritableLine($writableLine);
81
82
            return true;
83
        } catch (BoletoWinnerException $exception) {
84
            return false;
85
        }
86
    }
87
88
    public static function isValidBarcode(string $barcode): bool
89
    {
90
        try {
91
            BillFactory::getInstance()->createFromBarcode($barcode);
92
93
            return true;
94
        } catch (BoletoWinnerException $exception) {
95
            return false;
96
        }
97
    }
98
99
    /**
100
     * @throws BoletoWinnerException
101
     */
102
    private static function handleIsValidTypeCall(string $method, array $arguments): bool
103
    {
104
        $type = strtolower(substr($method, 7));
105
        $input = self::sanitizeInput(...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $input of Claudsonm\BoletoWinner\B...Winner::sanitizeInput() does not expect variable arguments. ( Ignorable by Annotation )

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

105
        $input = self::sanitizeInput(/** @scrutinizer ignore-type */ ...$arguments);
Loading history...
106
        $billClass = BillFactory::getInstance()->createBillInstance($type);
107
        $billClass->setBarcode($input)->setWritableLine($input);
108
109
        return $billClass->isBarcodeValid() || $billClass->isWritableLineValid();
110
    }
111
112
    private static function sanitizeInput(string $input): string
113
    {
114
        return preg_replace('/[^0-9]/', '', $input);
115
    }
116
}
117