Completed
Push — master ( 0dcd87...976654 )
by Luis Andrés
01:08
created

validatePersonalIdentification()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 3
nop 1
1
<?php
2
3
namespace Luilliarcec\LaravelEcuadorIdentification\Validations;
4
5
use Exception;
6
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\FinalCustomer;
7
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\NaturalRuc;
8
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\PersonalIdentification;
9
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\PrivateRuc;
10
use Luilliarcec\LaravelEcuadorIdentification\Support\Identifications\PublicRuc;
11
12
/**
13
 * Class to validate Ecuadorian identity card, Natural ruc, Private ruc and Public ruc
14
 *
15
 * @link https://www.sri.gob.ec/web/guest/RUC#%C2%BFc%C3%B3mo-se
16
 * @link http://www.sri.gob.ec/DocumentosAlfrescoPortlet/descargar/1ee224e6-b84b-4a8f-8127-59f8cd99ae58/LOGARITMO_VALIDA_RUC.docx
17
 * @package Luilliarcec\LaravelEcuadorIdentification\Support
18
 */
19
class EcuadorIdentification
20
{
21
    /**
22
     * Error encapsulator variable
23
     *
24
     * @var string
25
     */
26
    private $error;
27
28
    /**
29
     * Set Error
30
     *
31
     * @param string|null $error
32
     */
33
    protected function setError($error): void
34
    {
35
        $this->error = $error;
36
    }
37
38
    /**
39
     * Get Error
40
     *
41
     * @return string|null
42
     */
43
    public function getError()
44
    {
45
        return $this->error;
46
    }
47
48
    /**
49
     * Validates the Ecuadorian Final Consumer
50
     *
51
     * @param string $identification_number Final Consumer Identification
52
     * @return string|null
53
     */
54 View Code Duplication
    public function validateFinalConsumer(string $identification_number)
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...
55
    {
56
        $this->setError(null);
57
58
        try {
59
            $identification = new FinalCustomer();
60
            return $identification->validate($identification_number);
61
        } catch (Exception $e) {
62
            $this->setError($e->getMessage());
63
            return null;
64
        }
65
    }
66
67
    /**
68
     * Validates the Ecuadorian Identification Card
69
     *
70
     * @param string $identification_number Number of Identification Card
71
     * @return string|null
72
     */
73 View Code Duplication
    public function validatePersonalIdentification(string $identification_number)
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...
74
    {
75
        $this->setError(null);
76
77
        try {
78
            $identification = new PersonalIdentification();
79
            return $identification->validate($identification_number);
80
        } catch (Exception $e) {
81
            $this->setError($e->getMessage());
82
            return null;
83
        }
84
    }
85
86
    /**
87
     * Validates the Ecuadorian RUC of Natural Person
88
     *
89
     * @param string $identification_number Number of RUC Natural Person
90
     * @return string|null
91
     */
92 View Code Duplication
    public function validateNaturalRuc(string $identification_number)
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...
93
    {
94
        $this->setError(null);
95
96
        try {
97
            $identification = new NaturalRuc();
98
            return $identification->validate($identification_number);
99
        } catch (Exception $e) {
100
            $this->setError($e->getMessage());
101
            return null;
102
        }
103
    }
104
105
    /**
106
     * Validates the Ecuadorian RUC of Public Companies
107
     *
108
     * @param string $identification_number Number of RUC Public Companies
109
     * @return string|null
110
     */
111 View Code Duplication
    public function validatePublicRuc(string $identification_number)
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...
112
    {
113
        $this->setError(null);
114
115
        try {
116
            $identification = new PublicRuc();
117
            return $identification->validate($identification_number);
118
        } catch (Exception $e) {
119
            $this->setError($e->getMessage());
120
            return null;
121
        }
122
    }
123
124
    /**
125
     * Validates the Ecuadorian RUC of Private Companies
126
     *
127
     * @param string $identification_number Number of RUC Private Companies
128
     * @return string|null
129
     */
130 View Code Duplication
    public function validatePrivateRuc(string $identification_number)
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...
131
    {
132
        $this->setError(null);
133
134
        try {
135
            $identification = new PrivateRuc();
136
            return $identification->validate($identification_number);
137
        } catch (Exception $e) {
138
            $this->setError($e->getMessage());
139
            return null;
140
        }
141
    }
142
143
    /**
144
     * Validates the Ecuadorian Ruc's
145
     *
146
     * @param string $identification_number Number of RUC
147
     * @return string|null
148
     */
149
    public function validateRuc(string $identification_number)
150
    {
151
        if (($result = $this->validatePrivateRuc($identification_number)) !== null) {
152
            return $result;
153
        }
154
155
        if (($result = $this->validatePublicRuc($identification_number)) !== null) {
156
            return $result;
157
        }
158
159
        return $this->validateNaturalRuc($identification_number);
160
    }
161
162
    /**
163
     * Validate that the number belongs to natural persons.
164
     *
165
     * @param string $identification_number Number of identification
166
     * @return string|null
167
     */
168
    public function validateIsNaturalPersons(string $identification_number)
169
    {
170
        return $this->validatePersonalIdentification($identification_number) ?: $this->validateNaturalRuc($identification_number);
171
    }
172
173
    /**
174
     * Validate that the number belongs to juridical persons.
175
     *
176
     * @param string $identification_number Number of identification
177
     * @return string|null
178
     */
179
    public function validateIsJuridicalPersons(string $identification_number)
180
    {
181
        return $this->validatePrivateRuc($identification_number) ?: $this->validatePublicRuc($identification_number);
182
    }
183
184
    /**
185
     * Validate the number with all types of documents.
186
     *
187
     * @param string $identification_number Number of identification
188
     * @return string|null
189
     */
190
    public function validateAllTypeIdentification(string $identification_number)
191
    {
192
        if (($result = $this->validateFinalConsumer($identification_number)) !== null) {
193
            return $result;
194
        }
195
196
        if (($result = $this->validateRuc($identification_number)) !== null) {
197
            return $result;
198
        }
199
200
        return $this->validatePersonalIdentification($identification_number);
201
    }
202
}
203