SessionUtils   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 31
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A generateRandomString() 0 13 3
1
<?php
2
3
/**
4
 * \AppserverIo\Psr\Servlet\SessionUtils
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io-psr/servlet
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Psr\Servlet;
22
23
/**
24
 * A servlet session implementation.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2015 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io-psr/servlet
30
 * @link      http://www.appserver.io
31
 */
32
class SessionUtils
33
{
34
35
    /**
36
     * Private constructor to disable direct instantiation.
37
     */
38
    private function __construct()
39
    {
40
    }
41
42
    /**
43
     * Creates a random string with the passed length.
44
     *
45
     * @param integer $length The byte length to generate
46
     *
47
     * @return string The hex encoded random bytes
48
     */
49
    public static function generateRandomString($length = 32)
50
    {
51
52
        // generate random bytes using openssl
53
        $randomBytes = openssl_random_pseudo_bytes($length, $cryptoSafe);
54
55
        // make sure the generation was a) successful and b) cryptographically safe
56
        if (false === $randomBytes || false === $cryptoSafe) {
57
            throw new \RuntimeException('Unable to generate cryptographically safe pseudo random bytes.');
58
        }
59
60
        return bin2hex($randomBytes);
61
    }
62
}
63