1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WeDevBr\Bankly\Validators\Card; |
4
|
|
|
|
5
|
|
|
use WeDevBr\Bankly\Types\Card\Address; |
6
|
|
|
use WeDevBr\Bankly\Types\Card\Duplicate; |
7
|
|
|
use WeDevBr\Bankly\Validators\CpfCnpjValidator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* DuplicateCardValidator class |
11
|
|
|
* |
12
|
|
|
* PHP version 7.3|7.4|8.0 |
13
|
|
|
* |
14
|
|
|
* @author WeDev Brasil Team <[email protected]> |
15
|
|
|
* @author Yan de Paula <[email protected]> |
16
|
|
|
* @copyright 2021 We Dev Tecnologia Ltda |
17
|
|
|
* @link https://github.com/wedevBr/bankly-laravel/ |
18
|
|
|
*/ |
19
|
|
|
class DuplicateCardValidator |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Duplicate |
23
|
|
|
*/ |
24
|
|
|
private $duplicateCard; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $status = [ |
30
|
|
|
'LostMyCard', |
31
|
|
|
'CardWasStolen', |
32
|
|
|
'CardWasDamaged', |
33
|
|
|
'CardNotDelivered', |
34
|
|
|
'UnrecognizedOnlinePurchase' |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* DuplicateCardValidator constructor. |
39
|
|
|
* |
40
|
|
|
* @param Duplicate $duplicateCard |
41
|
|
|
*/ |
42
|
|
|
public function __construct(Duplicate $duplicateCard) |
43
|
|
|
{ |
44
|
|
|
$this->duplicateCard = $duplicateCard; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* This validate the duplicate card data |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function validate(): void |
53
|
|
|
{ |
54
|
|
|
$this->validateStatus(); |
55
|
|
|
$this->validateDocumentNumber(); |
56
|
|
|
$this->validatePassword(); |
57
|
|
|
$this->validateAddress(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* This validate duplicate card status |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
* @throws \InvalidArgumentException |
65
|
|
|
*/ |
66
|
|
|
public function validateStatus() |
67
|
|
|
{ |
68
|
|
|
$status = $this->duplicateCard->status; |
69
|
|
|
if (!in_array($status, $this->status)) { |
70
|
|
|
$message = 'invalid status, needs to be one of these'; |
71
|
|
|
$message .= ' LostMyCard, CardWasStolen, CardWasDamaged, CardNotDelivered, UnrecognizedOnlinePurchase'; |
72
|
|
|
throw new \InvalidArgumentException($message); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* This validate a duplicate card document |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
* @throws \InvalidArgumentException |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
private function validateDocumentNumber() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$documentNumber = $this->duplicateCard->documentNumber; |
85
|
|
|
if (empty($documentNumber) || !is_string($documentNumber) || !is_numeric($documentNumber)) { |
86
|
|
|
throw new \InvalidArgumentException('document number should be a numeric string'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$documentValidator = new CpfCnpjValidator($documentNumber); |
90
|
|
|
$documentValidator->validate(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* This validate a duplicate card password |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
* @throws \InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
private function validatePassword() |
100
|
|
|
{ |
101
|
|
|
$password = $this->duplicateCard->password; |
102
|
|
|
if (empty($password) || !is_string($password) || !is_numeric($password) || strlen($password) != 4) { |
103
|
|
|
throw new \InvalidArgumentException('password should be a numeric string'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* This validate a duplicate card address |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
private function validateAddress() |
113
|
|
|
{ |
114
|
|
|
if ($this->duplicateCard->address instanceof Address) { |
115
|
|
|
$address = $this->duplicateCard->address; |
116
|
|
|
$address->validate(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.