Completed
Push — master ( 26a60f...9314aa )
by PROSPER
02:23
created

TransRef::secureCrypt()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 13
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Laravel Paystack package.
5
 *
6
 * (c) Prosper Otemuyiwa <[email protected]>
7
 *
8
 * Source http://stackoverflow.com/a/13733588/179104
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Unicodeveloper\Paystack;
15
16
class TransRef
17
{
18
    /**
19
     * Get the pool to use based on the type of prefix hash
20
     * @param  string $type
21
     * @return string
22
     */
23
    private static function getPool($type = 'alnum')
24
    {
25
        switch ($type) {
26
            case 'alnum':
27
                $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
28
                break;
29
            case 'alpha':
30
                $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
31
                break;
32
            case 'hexdec':
33
                $pool = '0123456789abcdef';
34
                break;
35
            case 'numeric':
36
                $pool = '0123456789';
37
                break;
38
            case 'nozero':
39
                $pool = '123456789';
40
                break;
41
            case 'distinct':
42
                $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
43
                break;
44
            default:
45
                $pool = (string) $type;
46
                break;
47
        }
48
49
        return $pool;
50
    }
51
52
    /**
53
     * Generate a random secure crypt figure
54
     * @param  integer $min
55
     * @param  integer $max
56
     * @return integer
57
     */
58
    private static function secureCrypt($min, $max)
59
    {
60
        $range = $max - $min;
61
62
        if ($range < 0) {
63
            return $min; // not so random...
64
        }
65
66
        $log    = log($range, 2);
67
        $bytes  = (int) ($log / 8) + 1; // length in bytes
68
        $bits   = (int) $log + 1; // length in bits
69
        $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
70
        do {
71
            $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
72
            $rnd = $rnd & $filter; // discard irrelevant bits
73
        } while ($rnd >= $range);
74
75
        return $min + $rnd;
76
    }
77
78
    /**
79
     * Finally, generate a hashed token
80
     * @param  integer $length
81
     * @return string
82
     */
83
    public static function getHashedToken($length = 25)
84
    {
85
        $token = "";
86
        $max   = strlen(static::getPool());
0 ignored issues
show
Bug introduced by
Since getPool() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getPool() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
87
        for ($i = 0; $i < $length; $i++) {
88
            $token .= static::getPool()[static::secureCrypt(0, $max)];
0 ignored issues
show
Bug introduced by
Since getPool() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getPool() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
Bug introduced by
Since secureCrypt() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of secureCrypt() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
89
        }
90
91
        return $token;
92
    }
93
}
94