DuplicateCardValidator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 9.9 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 10
loc 101
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 7 1
A validateStatus() 0 9 2
A validateDocumentNumber() 10 10 4
A validatePassword() 0 7 5
A validateAddress() 0 7 2

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\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()
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...
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