Completed
Push — master ( 6c8478...564414 )
by Luis Andrés
01:00
created

EcuadorIdentification::validatePublicCompanyRuc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
c 0
b 0
f 0
rs 9.9
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Luilliarcec\LaravelEcuadorIdentification\Support;
4
5
use Luilliarcec\LaravelEcuadorIdentification\Exceptions\EcuadorIdentificationException;
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 $error
32
     */
33
    protected function setError(string $error): void
34
    {
35
        $this->error = $error;
36
    }
37
38
    /**
39
     * Get Error
40
     *
41
     * @return string
42
     */
43
    public function getError(): string
44
    {
45
        return $this->error;
46
    }
47
48
    /**
49
     * Validates the Ecuadorian Identification Card
50
     *
51
     * @param string $number Number of Identification Card
52
     * @return string|null
53
     */
54 View Code Duplication
    public function validatePersonalIdentification($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
        $identification = new PersonalIdentification();
57
58
        try {
59
            return $identification->validate($number);
60
        } catch (EcuadorIdentificationException $e) {
61
            $this->setError($e->getMessage());
62
            return null;
63
        }
64
    }
65
66
    /**
67
     * Validates the Ecuadorian RUC of Natural Person
68
     *
69
     * @param string $number Number of RUC Natural Person
70
     * @return string|null
71
     */
72 View Code Duplication
    public function validateNaturalPersonRuc($number)
73
    {
74
        $identification = new NaturalRuc();
75
76
        try {
77
            return $identification->validate($number);
78
        } catch (EcuadorIdentificationException $e) {
79
            $this->setError($e->getMessage());
80
            return null;
81
        }
82
    }
83
84
    /**
85
     * Validates the Ecuadorian RUC of Private Companies
86
     *
87
     * @param string $number Number of RUC Private Companies
88
     * @return string|null
89
     */
90 View Code Duplication
    public function validatePrivateCompanyRuc($number)
91
    {
92
        $identification = new PrivateRuc();
93
94
        try {
95
            return $identification->validate($number);
96
        } catch (EcuadorIdentificationException $e) {
97
            $this->setError($e->getMessage());
98
            return null;
99
        }
100
    }
101
102
    /**
103
     * Validates the Ecuadorian RUC of Public Companies
104
     *
105
     * @param string $number Number of RUC Public Companies
106
     * @return string|null
107
     */
108 View Code Duplication
    public function validatePublicCompanyRuc($number)
109
    {
110
        $identification = new PublicRuc();
111
112
        try {
113
            return $identification->validate($number);
114
        } catch (EcuadorIdentificationException $e) {
115
            $this->setError($e->getMessage());
116
            return null;
117
        }
118
    }
119
120
    /**
121
     * Validates the Ecuadorian Final Consumer
122
     *
123
     * @param $number
124
     * @return string|null
125
     */
126 View Code Duplication
    public function validateFinalConsumer($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...
127
    {
128
        $identification = new FinalCustomer();
129
130
        try {
131
            return $identification->validate($number);
132
        } catch (EcuadorIdentificationException $e) {
133
            $this->setError($e->getMessage());
134
            return null;
135
        }
136
    }
137
138
    /**
139
     * Validates the Ecuadorian Ruc's
140
     *
141
     * @param $number
142
     * @return string|null
143
     */
144
    public function validateRuc($number)
145
    {
146
        if (($result = $this->validatePrivateCompanyRuc($number)) !== null) {
147
            return $result;
148
        }
149
150
        if (($result = $this->validatePublicCompanyRuc($number)) !== null) {
151
            return $result;
152
        }
153
154
        return $this->validateNaturalPersonRuc($number);
155
    }
156
157
    /**
158
     * Validate that the number belongs to natural persons.
159
     *
160
     * @param $number
161
     * @return string|null
162
     */
163
    public function validateIsNaturalPersons($number)
164
    {
165
        return $this->validatePersonalIdentification($number) !== null ?
166
            $this->validatePersonalIdentification($number) : $this->validateNaturalPersonRuc($number);
167
    }
168
169
    /**
170
     * Validate that the number belongs to juridical persons.
171
     *
172
     * @param $number
173
     * @return string|null
174
     */
175
    public function validateIsJuridicalPersons($number)
176
    {
177
        return $this->validatePrivateCompanyRuc($number) !== null ?
178
            $this->validatePrivateCompanyRuc($number) : $this->validatePublicCompanyRuc($number);
179
    }
180
181
    /**
182
     * Validate the number with all types of documents.
183
     *
184
     * @param $number
185
     * @return string|null
186
     */
187
    public function validateAllIdentificatons($number)
188
    {
189
        if (($result = $this->validateFinalConsumer($number)) !== null) {
190
            return $result;
191
        }
192
193
        if (($result = $this->validatePersonalIdentification($number)) !== null) {
194
            return $result;
195
        }
196
197
        if (($result = $this->validateNaturalPersonRuc($number)) !== null) {
198
            return $result;
199
        }
200
201
        if (($result = $this->validatePrivateCompanyRuc($number)) !== null) {
202
            return $result;
203
        }
204
205
        return $this->validatePublicCompanyRuc($number);
206
    }
207
}
208