Appruve::handle()   A
last analyzed

Complexity

Conditions 3
Paths 7

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 49
rs 9.376
cc 3
nc 7
nop 2
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 Appruve
11
{
12
    public function __construct()
13
    {
14
        $config = new Config(__DIR__ . '/../config');
15
16
        $this->handler = $config->get('appruve.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('appruve.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('appruve.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
     * 
41
     * @param \Bytesfield\SimpleKyc\Classes\IdFilter
42
     * @param Closure $next
43
     * 
44
     * @return mixed
45
     */
46
    public function handle(IdFilter $IdFilter, Closure $next)
47
    {
48
        if (!$IdFilter->isSuccessful()) {
49
50
            $idType =  strtoupper($IdFilter->getIDType());
51
            $country = strtolower($IdFilter->getCountry());
0 ignored issues
show
Bug introduced by
It seems like $IdFilter->getCountry() can also be of type null; however, parameter $string of strtolower() 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

51
            $country = strtolower(/** @scrutinizer ignore-type */ $IdFilter->getCountry());
Loading history...
52
            $type = $this->getType($idType);
53
            $url = '/v1/verifications/' . $country . '/' . $type;
54
55
            $body = [
56
                'id' => $IdFilter->getIdNumber(),
57
                'first_name' => $IdFilter->getFirstName(),
58
                'last_name' => $IdFilter->getLastName(),
59
                'middle_name' => $IdFilter->getMiddleName(),
60
                'date_of_birth' => $IdFilter->getDOB(),
61
                'phone_number' => $IdFilter->getPhoneNumber(),
62
                'expiry_date' => $IdFilter->getExpiry(),
63
                'gender' => $IdFilter->getGender(),
64
                'address' => $IdFilter->getAddress(),
65
                'pin' => $IdFilter->getPin(),
66
                'tin' => $IdFilter->getTin(),
67
                'full_name' => $IdFilter->getFullName(),
68
                'company_name' => $IdFilter->getCompany(),
69
                'registration_number' => $IdFilter->getRegistrationNumber()
70
            ];
71
72
            try {
73
                $response = $this->process('POST', $url, array_filter($body))->getResponse();
74
75
                $IdFilter->confirmSuccess();
76
77
                $IdFilter->setHandler($this->handler);
78
79
                $IdFilter->setData([
80
                    'handler' => $IdFilter->getHandler(),
81
                    'country' => $IdFilter->getCountry(),
82
                    'message' => $IdFilter->getIDType() . ' Verified' . ' Successfully',
83
                    'data' => $response
84
                ]);
85
86
                return $IdFilter->getData();
87
            } catch (\Exception $e) {
88
89
                $IdFilter->setError(['error' => $e->getMessage()]);
90
91
                return $next($IdFilter);
92
            }
93
        }
94
        return $next($IdFilter);
95
    }
96
97
    /**
98
     * Transform the ID
99
     *
100
     * @param string $type
101
     * 
102
     * @return string
103
     */
104
    private function getType(string $type): string
105
    {
106
        if ($type === IdFilter::IDVALUES['TYPE_NATIONAL_ID']  || $type === IdFilter::IDVALUES['TYPE_NIN']) {
107
            return 'national_id';
108
        }
109
        if ($type === IdFilter::IDVALUES['TYPE_DRIVERS_LICENSE']) {
110
            return 'driver_license';
111
        }
112
        if ($type === IdFilter::IDVALUES['TYPE_VOTER_CARD']) {
113
            return 'voter';
114
        }
115
        if ($type === IdFilter::IDVALUES['TYPE_BVN']) {
116
            return 'bvn';
117
        }
118
        if ($type === IdFilter::IDVALUES['TELCO_SUBSCRIBER']) {
119
            return 'telco_subscriber';
120
        }
121
        return strtolower($type);
122
    }
123
}
124