BankAccountValidator   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 83
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 8 1
A validateDocument() 0 9 3
A validateAccountType() 0 8 3
A validateBranch() 0 7 4
A validateAccount() 0 7 4
A validateName() 0 7 3
1
<?php
2
3
/**
4
 * BankAccountValidator class
5
 *
6
 * PHP version 7.3
7
 *
8
 * @author    WeDev Brasil Team <[email protected]>
9
 * @author    Adeildo Amorim <[email protected]>
10
 * @copyright 2020 We Dev Tecnologia Ltda
11
 * @link      https://github.com/wedevBr/cda-admin-backend-laravel/
12
 */
13
14
namespace WeDevBr\Bankly\Validators;
15
16
use WeDevBr\Bankly\BankAccount;
17
18
class BankAccountValidator
19
{
20
    private $bank_account;
21
22
    /**
23
     * BankAccountValidator constructor.
24
     * @param BankAccount $bankAccount
25
     */
26
    public function __construct(BankAccount $bankAccount)
27
    {
28
        $this->bank_account = $bankAccount;
29
    }
30
31
    /**
32
     * This validate the bank account
33
     */
34
    public function validate(): void
35
    {
36
        $this->validateBranch();
37
        $this->validateAccount();
38
        $this->validateDocument();
39
        $this->validateName();
40
        $this->validateAccountType();
41
    }
42
43
    /**
44
     * This validate a bank branch
45
     */
46
    private function validateBranch()
47
    {
48
        $branch = $this->bank_account->branch;
49
        if (is_null($branch) || !is_string($branch) || !is_numeric($branch)) {
50
            throw new \InvalidArgumentException('branch should be a numeric string');
51
        }
52
    }
53
54
    /**
55
     * This validate a bank account
56
     */
57
    private function validateAccount()
58
    {
59
        $account = $this->bank_account->account;
60
        if (is_null($account) || !is_string($account)  || !is_numeric($account)) {
61
            throw new \InvalidArgumentException('account should be a numeric string');
62
        }
63
    }
64
65
    /**
66
     * This validates a cpf_cnpj
67
     */
68
    private function validateDocument()
69
    {
70
        $document = $this->bank_account->document;
71
        if (is_null($document) || !is_string($document)) {
72
            throw new \InvalidArgumentException('document should be a string');
73
        }
74
        $documentValidator = new CpfCnpjValidator($document);
75
        $documentValidator->validate();
76
    }
77
78
    /**
79
     * This validates a given name
80
     */
81
    private function validateName()
82
    {
83
        $name = $this->bank_account->name;
84
        if (is_null($name) || !is_string($name)) {
85
            throw new \InvalidArgumentException('name should be a string');
86
        }
87
    }
88
89
    /**
90
     * This validates bank account type
91
     */
92
    private function validateAccountType()
93
    {
94
        $allowed = ['CHECKING', 'SAVINGS'];
95
        $accountType = $this->bank_account->accountType;
96
        if (!in_array($accountType, $allowed) || is_null($accountType)) {
97
            throw new \InvalidArgumentException('accountType should be one of them: ' . implode(', ', $allowed));
98
        }
99
    }
100
}
101