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

PixCashoutManualValidator::validateAmount()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.6111
c 0
b 0
f 0
cc 5
nc 2
nop 0
1
<?php
2
3
namespace WeDevBr\Bankly\Validators\Pix;
4
5
use WeDevBr\Bankly\Types\Pix\BankAccount;
6
use WeDevBr\Bankly\Types\Pix\PixCashoutManual;
7
8
/**
9
 * PixCashoutValidator class
10
 *
11
 * PHP version 7.3|7.4|8.0
12
 *
13
 * @author    WeDev Brasil Team <[email protected]>
14
 * @author    Rafael Teixeira <[email protected]>
15
 * @copyright 2020 We Dev Tecnologia Ltda
16
 * @link      https://github.com/wedevBr/bankly-laravel/
17
 */
18
class PixCashoutManualValidator
19
{
20
    /** @var PixCashoutManual */
21
    private $pixCashoutManual;
22
23
    /**
24
     * @param PixCashoutManual $pixCashoutManual
25
     */
26
    public function __construct(PixCashoutManual $pixCashoutManual)
27
    {
28
        $this->pixCashoutManual = $pixCashoutManual;
29
    }
30
31
    /**
32
     * Validate the attributes of the PIX cashout class
33
     *
34
     * @return void
35
     */
36
    public function validate(): void
37
    {
38
        $this->validateAmount();
39
        $this->validateDescription();
40
        $this->validateSender();
41
        $this->validateRecipient();
42
        $this->validateInitializationType();
43
    }
44
45
    /**
46
     * This validates the amount
47
     *
48
     * @return void
49
     * @throws InvalidArgumentException
50
     */
51
    private function validateAmount()
52
    {
53
        $amount = $this->pixCashoutManual->amount;
54
        if (empty($amount) || !is_string($amount) || !is_numeric($amount) || $amount <= 0) {
55
            throw new \InvalidArgumentException('amount should be a numeric string and greater than zero');
56
        }
57
    }
58
59
    /**
60
     * This validates a description
61
     *
62
     * @return void
63
     * @throws InvalidArgumentException
64
     */
65
    private function validateDescription()
66
    {
67
        $description = $this->pixCashoutManual->description;
68
        if (empty($description) || !is_string($description)) {
69
            throw new \InvalidArgumentException('cashout description should be a string');
70
        }
71
    }
72
73
    /**
74
     * This validates a sender bank account
75
     *
76
     * @return void
77
     * @throws InvalidArgumentException
78
     */
79
    private function validateSender()
80
    {
81
        if (!$this->pixCashoutManual->sender instanceof BankAccount) {
82
            throw new \InvalidArgumentException('sender should be a BankAccount');
83
        }
84
85
        $this->pixCashoutManual
86
            ->sender
87
            ->validate();
88
    }
89
90
    /**
91
     * This validates a recipient bank account
92
     *
93
     * @return void
94
     * @throws InvalidArgumentException
95
     */
96
    private function validateRecipient()
97
    {
98
        if (!$this->pixCashoutManual->recipient instanceof BankAccount) {
99
            throw new \InvalidArgumentException('recipient should be a BankAccount');
100
        }
101
102
        $this->pixCashoutManual
103
            ->recipient
104
            ->validate();
105
    }
106
107
    /**
108
     * This validates a initialization type
109
     *
110
     * @return void
111
     * @throws InvalidArgumentException
112
     */
113 View Code Duplication
    private function validateInitializationType()
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...
114
    {
115
        $initializationType = $this->pixCashoutManual->initializationType;
116
        if (empty($initializationType) || !is_string($initializationType)) {
117
            throw new \InvalidArgumentException('initialization type should be a string');
118
        }
119
120
        if ($this->pixCashoutManual->initializationType != 'Manual') {
121
            throw new \InvalidArgumentException('this initialization type is not valid');
122
        }
123
    }
124
}
125