GeneratorAwareTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setGenerator() 0 10 2
A getGenerator() 0 4 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