Passed
Pull Request — master (#3)
by
unknown
07:47
created

BoletoWinner::handleIsValidTypeCall()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Claudsonm\BoletoWinner;
4
5
use BadMethodCallException;
6
use Claudsonm\BoletoWinner\Exceptions\BoletoWinnerException;
7
use Claudsonm\BoletoWinner\Factories\BillFactory;
8
use Claudsonm\BoletoWinner\Validators\BoletoValidator;
9
use Claudsonm\BoletoWinner\Validators\ConvenioValidator;
10
use Throwable;
11
12
/**
13
 * @method static bool isValidBoleto(string $barcodeOrWritableLine)
14
 * @method static bool isValidConvenio(string $barcodeOrWritableLine)
15
 *
16
 * @see BillFactory
17
 */
18
class BoletoWinner
19
{
20
    /**
21
     * Handle dynamic, static calls to the object.
22
     *
23
     * @param string $method
24
     * @param array  $arguments
25
     *
26
     * @throws BadMethodCallException
27
     * @throws BoletoWinnerException
28
     *
29
     * @return bool
30
     */
31 3
    public static function __callStatic($method, $arguments)
32
    {
33 3
        if (starts_with('isValid', $method)) {
34 2
            return self::handleIsValidTypeCall($method, $arguments);
35
        }
36
37 1
        throw new BadMethodCallException("Method `{$method}` does not exist.");
38
    }
39
40
    /**
41
     * @throws BoletoWinnerException
42
     */
43 1
    public static function makeBill(string $barcodeOrWritableLine): Bill
44
    {
45 1
        return BillFactory::getInstance()
46 1
            ->createFromBarcodeOrWritableLine($barcodeOrWritableLine);
47
    }
48
49
    /**
50
     * @throws BoletoWinnerException
51
     */
52 5
    public static function toWritableLine(string $barcode): string
53
    {
54 5
        return BillFactory::getInstance()
55 5
            ->createFromBarcode($barcode)
56 5
            ->getWritableLine();
57
    }
58
59
    /**
60
     * @throws BoletoWinnerException
61
     */
62 1
    public static function toBarcode(string $writableLine): string
63
    {
64 1
        return BillFactory::getInstance()
65 1
            ->createFromWritableLine($writableLine)
66 1
            ->getBarcode();
67
    }
68
69 8
    public static function isValid(string $barcodeOrWritableLine): bool
70
    {
71
        try {
72 8
            BillFactory::getInstance()->createFromBarcodeOrWritableLine($barcodeOrWritableLine);
73
74 4
            return true;
75 4
        } catch (BoletoWinnerException $exception) {
76 4
            return false;
77
        }
78
    }
79
80 1
    public static function isValidWritableLine(string $writableLine): bool
81
    {
82
        try {
83 1
            BillFactory::getInstance()->createFromWritableLine($writableLine);
84
85 1
            return true;
86 1
        } catch (BoletoWinnerException $exception) {
87 1
            return false;
88
        }
89
    }
90
91 1
    public static function isValidBarcode(string $barcode): bool
92
    {
93
        try {
94 1
            BillFactory::getInstance()->createFromBarcode($barcode);
95
96 1
            return true;
97 1
        } catch (BoletoWinnerException $exception) {
98 1
            return false;
99
        }
100
    }
101
102 2
    public static function isBoletoByBarcode(string $barcode): bool
103
    {
104
        try {
105 2
            $writableLine = self::toWritableLine($barcode);
106
107 2
            $isBoleto = (new BoletoValidator())->verifyWritableLine($writableLine);
108
109 2
            return $isBoleto;
110
111
        } catch (Throwable $e) {
112
            return false;
113
        }
114
    }
115
116 2
    public static function isBoletoByWritableLine(string $writableLine): bool
117
    {
118
        try {
119 2
            $writableLineClean = preg_replace('/[^0-9]/', '', $writableLine);
120
121 2
            if (empty($writableLineClean)) {
122
                throw BoletoWinnerException::inputRequired();
123
            }
124
125 2
            $isBoleto = (new BoletoValidator())->verifyWritableLine($writableLineClean);
126
127 2
            return $isBoleto;
128
129
        } catch (Throwable $e) {
130
            return false;
131
        }
132
    }
133
134 2
    public static function isConvenioByBarcode(string $barcode): bool
135
    {
136
        try {
137 2
            $writableLine = self::toWritableLine($barcode);
138
139 2
            $isConvenio = (new ConvenioValidator())->verifyWritableLine($writableLine);
140
141 2
            return $isConvenio;
142
143
        } catch (Throwable $e) {
144
            return false;
145
        }
146
    }
147
148 2
    public static function isConvenioByWritableLine(string $writableLine): bool
149
    {
150
        try {
151 2
            $writableLineClean = preg_replace('/[^0-9]/', '', $writableLine);
152
153 2
            if (empty($writableLineClean)) {
154
                throw BoletoWinnerException::inputRequired();
155
            }
156
157 2
            $isConvenio = (new ConvenioValidator())->verifyWritableLine($writableLineClean);
158
159 2
            return $isConvenio;
160
161
        } catch (Throwable $e) {
162
            return false;
163
        }
164
    }
165
166
    /**
167
     * @throws BoletoWinnerException
168
     */
169 2
    private static function handleIsValidTypeCall(string $method, array $arguments): bool
170
    {
171 2
        $type = strtolower(substr($method, 7));
172 2
        $input = self::sanitizeInput(array_shift($arguments));
173 2
        $billClass = BillFactory::getInstance()->createBillInstance($type);
174 2
        $billClass->setBarcode($input)->setWritableLine($input);
175
176 2
        return $billClass->isBarcodeValid() || $billClass->isWritableLineValid();
177
    }
178
179 2
    private static function sanitizeInput(string $input): string
180
    {
181 2
        return preg_replace('/[^0-9]/', '', $input);
182
    }
183
}
184