GeneratorAwareTrait::setGenerator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Session
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Session\Traits;
16
17
use Phossa2\Session\Interfaces\GeneratorInterface;
18
use Phossa2\Session\Interfaces\GeneratorAwareInterface;
19
20
/**
21
 * GeneratorAwareTrait
22
 *
23
 * @package Phossa2\Session
24
 * @author  Hong Zhang <[email protected]>
25
 * @see     GeneratorAwareInterface
26
 * @version 2.1.0
27
 * @since   2.1.0 added
28
 */
29
trait GeneratorAwareTrait
30
{
31
    /**
32
     * @var    callable
33
     * @access protected
34
     */
35
    protected $generator;
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function setGenerator(GeneratorInterface $generator = null)
41
    {
42
        if (null === $generator) {
43
            $this->generator = function () {
44
                return md5(microtime(true) + rand(1, 10000));
45
            };
46
        } else {
47
            $this->generator = [$generator, 'generate'];
48
        }
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function getGenerator()/*# : callable */
55
    {
56
        return $this->generator;
57
    }
58
}
59