Helper::generateExpired()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wavecell;
4
5
use Wavecell\Config;
6
7
/**
8
 * Wavecell Helper.
9
 *
10
 * @author     Pribumi Technology
11
 * @license    MIT
12
 * @copyright  (c) 2019, Pribumi Technology
13
 */
14
class Helper
15
{
16
    /**
17
     * Generate a more truly "random" alpha-numeric string.
18
     *
19
     * @param int $length how long char will generate
20
     * @return string
21
     * @throws \Exception;
22
     */
23
    public static function random($length = 16)
24
    {
25
        $string = '';
26
27
        while (($len = strlen($string)) < $length) {
28
            $size = $length - $len;
29
30
            $bytes = random_bytes($size);
31
32
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
33
        }
34
35
        return $string;
36
    }
37
38
    /**
39
     * Generate ISO8601 Time.
40
     *
41
     * @param int $minutes Add Minutes from now
42
     * @return string
43
     */
44
    public static function generateExpired($minutes = 60)
45
    {
46
        $date = \Carbon\Carbon::now(Config::$timeZone);
47
        date_default_timezone_set(Config::$timeZone);
48
        $date->addMinutes($minutes);
49
        $fmt = $date->format('Y-m-d\TH:i:s.u\Z');
50
        return $fmt;
51
    }
52
}
53