Completed
Push — master ( 77cb3d...eaae4c )
by
unknown
24s queued 11s
created

DepositBilletValidator::validatePayer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace WeDevBr\Bankly\Validators\Billet;
4
5
use WeDevBr\Bankly\Types\Billet\BankAccount;
6
use WeDevBr\Bankly\Types\Billet\DepositBillet;
7
use WeDevBr\Bankly\Types\Billet\Payer;
8
9
/**
10
 * DepositBilletValidator class
11
 *
12
 * PHP version 7.3|7.4|8.0
13
 *
14
 * @author    WeDev Brasil Team <[email protected]>
15
 * @author    Rafael Teixeira <[email protected]>
16
 * @copyright 2021 We Dev Tecnologia Ltda
17
 * @link      https://github.com/wedevBr/bankly-laravel/
18
 */
19
class DepositBilletValidator
20
{
21
    /** @var DepositBillet */
22
    private $depositBillet;
23
24
    /**
25
     * @param DepositBillet $depositBillet
26
     */
27
    public function __construct(DepositBillet $depositBillet)
28
    {
29
        $this->depositBillet = $depositBillet;
30
    }
31
32
    /**
33
     * Validate the attributes of the deposit billet class
34
     *
35
     * @return void
36
     */
37
    public function validate(): void
38
    {
39
        $this->validateAlias();
40
        $this->validateDocumentNumber();
41
        $this->validateAmount();
42
        $this->validateDueDate();
43
        $this->validateEmissionFee();
44
        $this->validateType();
45
        $this->validateBankAccount();
46
        $this->validatePayer();
47
    }
48
49
    /**
50
     * This validates the alias
51
     *
52
     * @return void
53
     * @throws InvalidArgumentException
54
     */
55 View Code Duplication
    private function validateAlias()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $alias = $this->depositBillet->alias;
58
        if (empty($alias) || !is_string($alias)) {
59
            throw new \InvalidArgumentException('alias should be a string');
60
        }
61
    }
62
63
    /**
64
     * This validates the document number
65
     *
66
     * @return void
67
     * @throws InvalidArgumentException
68
     */
69
    private function validateDocumentNumber()
70
    {
71
        $documentNumber = $this->depositBillet->documentNumber;
72
        if (empty($documentNumber) || !is_string($documentNumber) || !is_numeric($documentNumber)) {
73
            throw new \InvalidArgumentException('document number should be a numeric string');
74
        }
75
    }
76
77
    /**
78
     * This validates the amount
79
     *
80
     * @return void
81
     * @throws InvalidArgumentException
82
     */
83
    private function validateAmount()
84
    {
85
        $amount = $this->depositBillet->amount;
86
        if (empty($amount) || !is_string($amount) || !is_numeric($amount) || $amount <= 0) {
87
            throw new \InvalidArgumentException('amount should be a numeric string and greater than zero');
88
        }
89
    }
90
91
    /**
92
     * This validates the due date
93
     *
94
     * @return void
95
     * @throws InvalidArgumentException
96
     */
97
    private function validateDueDate()
98
    {
99
        $dueDate = $this->depositBillet->dueDate;
100
101
        try {
102
            $date = now()->createFromFormat('Y-m-d', $dueDate);
103
            if (!$date->gt(now())) {
104
                throw new \InvalidArgumentException('due date must be greater than the current date');
105
            }
106
        } catch (\Throwable $th) {
107
            throw new \InvalidArgumentException('due date should be a valid date');
108
        }
109
    }
110
111
    /**
112
     * This validates the emission fee
113
     *
114
     * @return void
115
     * @throws InvalidArgumentException
116
     */
117
    private function validateEmissionFee()
118
    {
119
        $emissionFee = $this->depositBillet->emissionFee;
120
121
        if (!is_bool($emissionFee)) {
122
            throw new \InvalidArgumentException('emission fee should be a boolean');
123
        }
124
    }
125
126
    /**
127
     * This validates a type
128
     *
129
     * @return void
130
     * @throws InvalidArgumentException
131
     */
132
    private function validateType()
133
    {
134
        $type = $this->depositBillet->type;
135
        if (empty($type) || !is_string($type)) {
136
            throw new \InvalidArgumentException('type should be a string');
137
        }
138
139
        $types = ['Deposit', 'Levy'];
140
        if (!in_array($this->depositBillet->type, $types)) {
141
            throw new \InvalidArgumentException('this type is not valid');
142
        }
143
    }
144
145
    /**
146
     * This validates a bank account
147
     *
148
     * @return void
149
     * @throws InvalidArgumentException
150
     */
151
    private function validateBankAccount()
152
    {
153
        if (!$this->depositBillet->account instanceof BankAccount) {
154
            throw new \InvalidArgumentException('account should be a BankAccount type');
155
        }
156
157
        $this->depositBillet
158
            ->account
159
            ->validate();
160
    }
161
162
    /**
163
     * This validates a bank account
164
     *
165
     * @return void
166
     * @throws InvalidArgumentException
167
     */
168
    private function validatePayer()
169
    {
170
        if (!$this->depositBillet->payer instanceof Payer) {
171
            throw new \InvalidArgumentException('payer should be a Payer type');
172
        }
173
174
        $this->depositBillet
175
            ->payer
176
            ->validate();
177
    }
178
}
179