Generator::generateTransactionNumber()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 10
loc 10
ccs 0
cts 7
cp 0
crap 6
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Samerior\MobileMoney\Mpesa\Repositories;
4
5
use Samerior\MobileMoney\Mpesa\Exceptions\MpesaException;
6
7
/**
8
 * Class Generator
9
 * @package Samerior\MobileMoney\Repositories
10
 */
11
class Generator
12
{
13
    /**
14
     * Generate a random transaction number
15
     *
16
     * @return string
17
     */
18 View Code Duplication
    public static function generateTransactionNumber(): string
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...
19
    {
20
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
21
        $charactersLength = \strlen($characters);
22
        $randomString = '';
23
        for ($i = 0; $i < 15; $i++) {
24
            $randomString .= $characters[\random_int(0, $charactersLength - 1)];
25
        }
26
        return $randomString;
27
    }
28
29
    /**
30
     * @param string $initiatorPass
31
     * @return string
32
     * @throws MpesaException
33
     */
34
    public static function computeSecurityCredential($initiatorPass): string
35
    {
36
        if (\config('samerior.mpesa.sandbox')) {
37
            $pubKeyFile = __DIR__ . '/../Support/sandbox.cer';
38
        } else {
39
            $pubKeyFile = __DIR__ . '/../Support/production.cer';
40
        }
41
        if (\is_file($pubKeyFile)) {
42
            $pubKey = file_get_contents($pubKeyFile);
43
        } else {
44
            throw new MpesaException('Please provide a valid public key file');
45
        }
46
        openssl_public_encrypt($initiatorPass, $encrypted, $pubKey, OPENSSL_PKCS1_PADDING);
47
        return base64_encode($encrypted);
48
    }
49
}
50