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

BillFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 17
eloc 41
c 4
b 0
f 0
dl 0
loc 118
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A createFromBarcode() 0 12 3
A sanitizeInput() 0 8 2
A getInstance() 0 9 2
A createFromBarcodeOrWritableLine() 0 18 4
A createFromWritableLine() 0 12 3
A createBillInstance() 0 7 2
1
<?php
2
3
namespace Claudsonm\BoletoWinner\Factories;
4
5
use Claudsonm\BoletoWinner\Bill;
6
use Claudsonm\BoletoWinner\Boleto;
7
use Claudsonm\BoletoWinner\Convenio;
8
use Claudsonm\BoletoWinner\Exceptions\BoletoWinnerException;
9
10
class BillFactory
11
{
12
    /**
13
     * Bills supported.
14
     *
15
     * @var array|string[]
16
     */
17
    protected array $bills = [
18
        'boleto' => Boleto::class,
19
        'convenio' => Convenio::class,
20
    ];
21
22
    /**
23
     * The singleton instance.
24
     */
25
    private static ?BillFactory $instance = null;
26
27
    protected function __construct()
28
    {
29
        // Preventing direct construction calls with the `new` operator.
30
    }
31
32
    /**
33
     * Get the singleton class instance or creates one if it's not set yet.
34
     */
35
    public static function getInstance(): self
36
    {
37
        if (self::$instance) {
38
            return self::$instance;
39
        }
40
41
        self::$instance = new static();
42
43
        return self::$instance;
44
    }
45
46
    /**
47
     * Returns an instance of the Bill class for the given type.
48
     *
49
     * @throws BoletoWinnerException
50
     */
51
    public function createBillInstance(string $type): Bill
52
    {
53
        if (! isset($this->bills[$type])) {
54
            throw BoletoWinnerException::unsupportedType($type);
55
        }
56
57
        return new $this->bills[$type]();
58
    }
59
60
    /**
61
     * @throws BoletoWinnerException
62
     */
63
    public function createFromBarcodeOrWritableLine(string $barcodeOrWritableLine): Bill
64
    {
65
        $input = $this->sanitizeInput($barcodeOrWritableLine);
66
        foreach ($this->bills as $billClass) {
67
            $bill = new $billClass();
68
            $bill->setBarcode($input);
69
            if ($bill->isBarcodeValid()) {
70
                return $bill;
71
            }
72
73
            $bill = new $billClass();
74
            $bill->setWritableLine($input);
75
            if ($bill->isWritableLineValid()) {
76
                return $bill;
77
            }
78
        }
79
80
        throw BoletoWinnerException::invalidInput($barcodeOrWritableLine);
81
    }
82
83
    /**
84
     * @throws BoletoWinnerException
85
     */
86
    public function createFromWritableLine(string $writableLine): Bill
87
    {
88
        $input = $this->sanitizeInput($writableLine);
89
        foreach ($this->bills as $billClass) {
90
            $bill = new $billClass();
91
            $bill->setWritableLine($input);
92
            if ($bill->isWritableLineValid()) {
93
                return $bill;
94
            }
95
        }
96
97
        throw BoletoWinnerException::invalidInput($writableLine);
98
    }
99
100
    /**
101
     * @throws BoletoWinnerException
102
     */
103
    public function createFromBarcode(string $barcode): Bill
104
    {
105
        $input = $this->sanitizeInput($barcode);
106
        foreach ($this->bills as $billClass) {
107
            $bill = new $billClass();
108
            $bill->setBarcode($input);
109
            if ($bill->isBarcodeValid()) {
110
                return $bill;
111
            }
112
        }
113
114
        throw BoletoWinnerException::invalidInput($barcode);
115
    }
116
117
    /**
118
     * @throws BoletoWinnerException
119
     */
120
    private function sanitizeInput(string $barcodeOrWritableLine): string
121
    {
122
        $input = preg_replace('/[^0-9]/', '', $barcodeOrWritableLine);
123
        if (empty($input)) {
124
            throw BoletoWinnerException::inputRequired();
125
        }
126
127
        return $input;
128
    }
129
}
130