Smile::process()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Bytesfield\SimpleKyc\Pipes;
4
5
use Closure;
6
use Noodlehaus\Config;
7
use Bytesfield\SimpleKyc\HttpProcessor;
8
use Bytesfield\SimpleKyc\Classes\IdFilter;
9
10
class Smile
11
{
12
    public function __construct()
13
    {
14
        $config = new Config(__DIR__ . '/../config');
15
16
        $this->handler = $config->get('smile.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('smile.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('smile.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
        $this->partnerId = $config->get('smile.partner_id');
0 ignored issues
show
Bug Best Practice introduced by
The property partnerId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
    }
21
22
    /**
23
     * Process http calls
24
     * 
25
     * @param string $method The call method get|post|put|delete|patch
26
     * @param string $url The url to call
27
     * @param array payload The payload data to send with the call
0 ignored issues
show
Bug introduced by
The type Bytesfield\SimpleKyc\Pipes\payload 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...
28
     * 
29
     * @return \Bytesfield\SimpleKyc\HttpProcessor
30
     */
31
    private function process($method, $url, $payload): HttpProcessor
32
    {
33
        //HttpProcessor class to handle http request
34
        $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

34
        $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

34
        $processor = new HttpProcessor($this->baseUrl, $this->apiKey, /** @scrutinizer ignore-type */ $this->handler);
Loading history...
35
36
        return $processor->process($method, $url, $payload);
37
    }
38
39
    /**
40
     * Handles the ID request
41
     * 
42
     * @param \Bytesfield\SimpleKyc\Classes\IdFilter
43
     * @param Closure $next
44
     * 
45
     * @return array
46
     */
47
    public function handle(IdFilter $IdFilter, Closure $next)
48
    {
49
        if (!$IdFilter->isSuccessful()) {
50
51
            $secKey = $this->generateKey($this->partnerId, $this->apiKey);
0 ignored issues
show
Bug introduced by
It seems like $this->partnerId can also be of type null; however, parameter $partnerId of Bytesfield\SimpleKyc\Pipes\Smile::generateKey() 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
            $secKey = $this->generateKey(/** @scrutinizer ignore-type */ $this->partnerId, $this->apiKey);
Loading history...
Bug introduced by
It seems like $this->apiKey can also be of type null; however, parameter $apiKey of Bytesfield\SimpleKyc\Pipes\Smile::generateKey() 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
            $secKey = $this->generateKey($this->partnerId, /** @scrutinizer ignore-type */ $this->apiKey);
Loading history...
52
            $jobId = md5(time() . rand());
53
54
            $body = [
55
                'partner_id' => $this->partnerId,
56
                'sec_key' => $secKey[0],
57
                'timestamp' => $secKey[1],
58
                'country' => strtoupper($IdFilter->getCountry()),
0 ignored issues
show
Bug introduced by
It seems like $IdFilter->getCountry() can also be of type null; however, parameter $string of strtoupper() 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

58
                'country' => strtoupper(/** @scrutinizer ignore-type */ $IdFilter->getCountry()),
Loading history...
59
                'id_type' => $IdFilter->getIdType(),
60
                'id_number' => $IdFilter->getIdNumber(),
61
                'first_name' => $IdFilter->getFirstName(),
62
                'last_name' => $IdFilter->getLastName(),
63
                'dob' => $IdFilter->getDOB(),
64
                'phone_number' => $IdFilter->getPhoneNumber(),
65
                'company' => $IdFilter->getCompany(),
66
                'partner_params' => [
67
                    'job_type' => 5,
68
                    'job_id' => $jobId,
69
                    'user_id' => $IdFilter->getUserId()
70
                ]
71
            ];
72
73
            try {
74
                $response = $this->process('POST', '/v1/id_verification', array_filter($body));
75
76
                $result = $response->getResponse();
77
78
                if ($result['IsFinalResult']) {
79
                    $IdFilter->confirmSuccess();
80
                    $IdFilter->setHandler($this->handler);
81
82
                    $IdFilter->setData([
83
                        'handler' => $IdFilter->getHandler(),
84
                        'country' => $IdFilter->getCountry(),
85
                        'message' => $IdFilter->getIDType() . ' Verified' . ' Successfully',
86
                        'data' => $result
87
                    ]);
88
89
                    return $IdFilter->getData();
90
                }
91
            } catch (\Exception $e) {
92
                $IdFilter->setError(['error' => $e->getMessage()]);
93
94
                return $next($IdFilter);
95
            }
96
        }
97
        return $next($IdFilter);
98
    }
99
100
    /**
101
     * Generate Secret Key
102
     *
103
     * @param string $partnerId
104
     * @param string $apiKey
105
     * @return array
106
     */
107
    private function generateKey(string $partnerId, string $apiKey): array
108
    {
109
        $timestamp = time();
110
        $plaintext = intval($partnerId) . ":" . $timestamp;
111
        $hashSignature = hash('sha256', $plaintext);
112
113
        openssl_public_encrypt($hashSignature, $secKey, base64_decode($apiKey), OPENSSL_PKCS1_PADDING);
114
115
        $secKey = base64_encode($secKey);
116
        $secKey = $secKey . "|" . $hashSignature;
117
118
        return array($secKey, $timestamp);
119
    }
120
}
121