Completed
Push — master ( 2a953d...5971d3 )
by
unknown
27s queued 10s
created

VirtualCardValidator   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 141
Duplicated Lines 9.93 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 3
dl 14
loc 141
rs 9.84
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 11 1
A validateDocumentNumber() 0 10 4
A validateCardName() 7 7 3
A validateAlias() 7 7 3
A validateBankAgency() 0 7 5
A validateBankAccount() 0 7 4
A validateProgramId() 0 7 5
A validatePassword() 0 7 5
A validateAddress() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace WeDevBr\Bankly\Validators\VirtualCard;
4
5
use WeDevBr\Bankly\Types\VirtualCard\VirtualCard;
6
use WeDevBr\Bankly\Validators\CpfCnpjValidator;
7
8
/**
9
 * VirtualCardValidator 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 VirtualCardValidator
19
{
20
    private $virtualCard;
21
22
    /**
23
     * VirtualCardValidator constructor.
24
     * @param VirtualCard $virtualCard
25
     */
26
    public function __construct(VirtualCard $virtualCard)
27
    {
28
        $this->virtualCard = $virtualCard;
29
    }
30
31
    /**
32
     * This validate the virtual card
33
     */
34
    public function validate(): void
35
    {
36
        $this->validateDocumentNumber();
37
        $this->validateCardName();
38
        $this->validateAlias();
39
        $this->validateBankAgency();
40
        $this->validateBankAccount();
41
        $this->validateProgramId();
42
        $this->validatePassword();
43
        $this->validateAddress();
44
    }
45
46
    /**
47
     * This validate a virtual card document
48
     *
49
     * @return void
50
     * @throws InvalidArgumentException
51
     */
52
    private function validateDocumentNumber()
53
    {
54
        $documentNumber = $this->virtualCard->documentNumber;
55
        if (empty($documentNumber) || !is_string($documentNumber) || !is_numeric($documentNumber)) {
56
            throw new \InvalidArgumentException('document number should be a numeric string');
57
        }
58
59
        $documentValidator = new CpfCnpjValidator($documentNumber);
60
        $documentValidator->validate();
61
    }
62
63
    /**
64
     * This validates a card name
65
     *
66
     * @return void
67
     * @throws InvalidArgumentException
68
     */
69 View Code Duplication
    private function validateCardName()
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...
70
    {
71
        $cardName = $this->virtualCard->cardName;
72
        if (empty($cardName) || !is_string($cardName)) {
73
            throw new \InvalidArgumentException('card name should be a string');
74
        }
75
    }
76
77
    /**
78
     * This validate a virtual card alias
79
     *
80
     * @return void
81
     * @throws InvalidArgumentException
82
     */
83 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...
84
    {
85
        $alias = $this->virtualCard->alias;
86
        if (empty($alias) || !is_string($alias)) {
87
            throw new \InvalidArgumentException('alias should be a string');
88
        }
89
    }
90
91
    /**
92
     * This validate a virtual card bank agency
93
     *
94
     * @return void
95
     * @throws InvalidArgumentException
96
     */
97
    private function validateBankAgency()
98
    {
99
        $bankAgency = $this->virtualCard->bankAgency;
100
        if (empty($bankAgency) || !is_string($bankAgency) || !is_numeric($bankAgency) || strlen($bankAgency) != 4) {
101
            throw new \InvalidArgumentException('bank agency should be a numeric string');
102
        }
103
    }
104
105
    /**
106
     * This validate a virtual card bank account
107
     *
108
     * @return void
109
     * @throws InvalidArgumentException
110
     */
111
    private function validateBankAccount()
112
    {
113
        $bankAccount = $this->virtualCard->bankAccount;
114
        if (empty($bankAccount) || !is_string($bankAccount) || !is_numeric($bankAccount)) {
115
            throw new \InvalidArgumentException('bank account should be a numeric string');
116
        }
117
    }
118
119
    /**
120
     * This validate a virtual card program ID
121
     *
122
     * @return void
123
     * @throws InvalidArgumentException
124
     */
125
    private function validateProgramId()
126
    {
127
        $programId = $this->virtualCard->programId;
128
        if (!empty($programId) && (!is_string($programId) || !is_numeric($programId) || strlen($programId) != 32)) {
129
            throw new \InvalidArgumentException('program ID should be a numeric string');
130
        }
131
    }
132
133
    /**
134
     * This validate a virtual card password
135
     *
136
     * @return void
137
     * @throws InvalidArgumentException
138
     */
139
    private function validatePassword()
140
    {
141
        $password = $this->virtualCard->password;
142
        if (empty($password) || !is_string($password) || !is_numeric($password) || strlen($password) != 4) {
143
            throw new \InvalidArgumentException('password should be a numeric string');
144
        }
145
    }
146
147
    /**
148
     * This validate a virtual card address
149
     *
150
     * @return void
151
     * @throws InvalidArgumentException
152
     */
153
    private function validateAddress()
154
    {
155
        $address = $this->virtualCard->address;
156
        $address->validate();
157
    }
158
}
159