Credequity::postData()   A
last analyzed

Complexity

Conditions 3
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 25
rs 9.7998
cc 3
nc 8
nop 3
1
<?php
2
3
namespace Bytesfield\SimpleKyc\Pipes;
4
5
use Bytesfield\SimpleKyc\Classes\IdFilter;
6
use Closure;
7
use Noodlehaus\Config;
8
use Bytesfield\SimpleKyc\HttpProcessor;
9
10
class Credequity
11
{
12
    public function __construct()
13
    {
14
        $config = new Config(__DIR__ . '/../config');
15
16
        $this->handler = $config->get('credequity.handler');
0 ignored issues
show
Bug Best Practice introduced by
The property handler does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $this->apiKey = $config->get('credequity.api_key');
0 ignored issues
show
Bug Best Practice introduced by
The property apiKey does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
        $this->baseUrl = $config->get('credequity.api_url');
0 ignored issues
show
Bug Best Practice introduced by
The property baseUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
    }
20
21
    /**
22
     * Process http calls
23
     * 
24
     * @param string $method The call method get|post|put|delete|patch
25
     * @param string $url The url to call
26
     * @param array $payload
27
28
     * @return \Bytesfield\SimpleKyc\HttpProcessor
29
     */
30
    private function process($method, $url, $payload): HttpProcessor
31
    {
32
        //HttpProcessor class to handle http request
33
        $processor = new HttpProcessor($this->baseUrl, $this->apiKey, $this->handler);
0 ignored issues
show
Bug introduced by
It seems like $this->baseUrl can also be of type null; however, parameter $baseUrl of Bytesfield\SimpleKyc\HttpProcessor::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        $processor = new HttpProcessor(/** @scrutinizer ignore-type */ $this->baseUrl, $this->apiKey, $this->handler);
Loading history...
Bug introduced by
It seems like $this->handler can also be of type null; however, parameter $handler of Bytesfield\SimpleKyc\HttpProcessor::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        $processor = new HttpProcessor($this->baseUrl, $this->apiKey, /** @scrutinizer ignore-type */ $this->handler);
Loading history...
34
35
        return $processor->process($method, $url, $payload);
36
    }
37
38
    /**
39
     * Handles the ID request
40
     * @param \Bytesfield\SimpleKyc\Classes\IdFilter
41
     * @param Closure $next
42
     * 
43
     * @return array
44
     */
45
    public function handle(IdFilter $IdFilter, Closure $next)
46
    {
47
48
        if (!$IdFilter->isSuccessful()) {
49
50
            if ($IdFilter->getCountry() == IdFilter::COUNTRIES['NIGERIA']) {
51
52
                if ($IdFilter->isWithImage()) {
53
                    return $this->getWithImage($IdFilter);
54
                }
55
56
                $idNumber =  $IdFilter->getIDNumber();
57
                $firstName =  $IdFilter->getFirstName();
58
                $lastName =  $IdFilter->getLastName();
59
                $phone =  $IdFilter->getPhoneNumber();
60
61
                if ($IdFilter->getIDType() === IdFilter::IDVALUES['TYPE_BVN']) {
62
                    $url = '/CredBvn/api/v1/Bvn/GetCustomerBvn';
63
64
                    $body = [
65
                        'bvn' => $idNumber,
66
                        'PhoneNumber' => $phone
67
                    ];
68
69
                    return $this->postData($IdFilter, $body, $url);
70
                }
71
                if ($IdFilter->getIDType() === IdFilter::IDVALUES['TYPE_NIN']) {
72
                    if (!$IdFilter->isSuccessful()) {
73
                        $url = '/CredNin/api/v1/Identity?phoneNo=' . $phone;
74
                        $body = [];
75
76
                        return $this->postData($IdFilter, $body, $url);
77
                    }
78
79
                    //If request is not successful makes post with IdNumber
80
                    if (!$IdFilter->isSuccessful()) {
81
                        $url = '/CredNin/api/v1/IdentityByNin?nin=' . $idNumber;
82
                        $body = [];
83
84
                        return $this->postData($IdFilter, $body, $url);
85
                    }
86
                }
87
88
                if ($IdFilter->getIDType() === IdFilter::IDVALUES['TYPE_DRIVERS_LICENSE']) {
89
                    $url = '/Verify/api/v1/FrscInfo';
90
91
                    $body = [
92
                        'firstname' => $firstName,
93
                        'lastname' => $lastName,
94
                        'phoneNo' => $phone,
95
                        'frscidentityNo' => $idNumber
96
                    ];
97
98
                    return $this->postData($IdFilter, $body, $url);
99
                }
100
101
                if ($IdFilter->getIDType() === IdFilter::IDVALUES['TYPE_CUSTOMER_PROFILE']) {
102
                    $url = '/CredBvn/api/v1/CustomerProfile';
103
                    //$IdFilter->setCredequityProfile();
104
                    $profile = $IdFilter->getCredequityProfile();
105
106
                    $body = [
107
                        "Nin" => $profile['nin'],
108
                        "FrscNo" => $profile['frscno'],
109
                        "phoneNo" => $phone,
110
                        "Bvn" => $profile['bvn']
111
                    ];
112
113
                    return $this->postData($IdFilter, $body, $url);
114
                }
115
            }
116
        }
117
        return $next($IdFilter);
118
    }
119
120
    /**
121
     * Get ID information via images
122
     *
123
     * @param \Bytesfield\SimpleKyc\Classes\IdFilter
124
125
     * @return array
126
     */
127
    private function getWithImage(IdFilter $IdFilter): array
128
    {
129
        $relative = ($IdFilter->getIDType() === 'NIN') ? 'VerifyNinWithFace' : 'VerifyFrscWithFace';
130
131
        $url = '/CredOcr/api/v1/' . $relative;
132
133
        $body = [
134
            'IdentificationProof' => $IdFilter->getIdentificationProof(),
135
            'FaceProof' => $IdFilter->getFaceProof()
136
        ];
137
138
        return $this->postData($IdFilter, $body, $url);
139
    }
140
141
    /**
142
     * Make API call and get the required data from Credequity
143
     *
144
     * @param \Bytesfield\SimpleKyc\Classes\IdFilter
145
     * @param array $body
146
     * @param string url
0 ignored issues
show
Bug introduced by
The type Bytesfield\SimpleKyc\Pipes\url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
147
     * 
148
     * @return array
149
     */
150
    private function postData(IdFilter $IdFilter, $body, $url)
151
    {
152
        try {
153
            $result =  $this->process('POST', $url, array_filter($body));
154
155
            $response = (object) $result->getResponse();
156
157
            if ($response->message === 'Successful') {
158
159
                $IdFilter->confirmSuccess();
160
161
                $IdFilter->setHandler($this->handler);
162
163
                $IdFilter->setData([
164
                    'handler' => $IdFilter->getHandler(),
165
                    'message' => $IdFilter->getIDType() . ' Verified' . ' Successfully',
166
                    'data' => $response->data
167
                ]);
168
169
                return $IdFilter->getData();
170
            }
171
        } catch (\Exception $e) {
172
            $IdFilter->setError(['error' => $e->getMessage()]);
173
174
            return json_encode(['error' => $IdFilter->getError()]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode(array...$IdFilter->getError())) returns the type string which is incompatible with the documented return type array.
Loading history...
175
        }
176
    }
177
}
178