Generator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 25.64 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 10
loc 39
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateTransactionNumber() 10 10 2
A computeSecurityCredential() 0 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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