UidProcessor::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Logger
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\Logger\Processor;
16
17
use Phossa2\Logger\Entry\LogEntryInterface;
18
19
/**
20
 * UidProcessor
21
 *
22
 * Inject an unique id for this session. Should be the first processor ?
23
 *
24
 * @package Phossa2\Logger
25
 * @author  Hong Zhang <[email protected]>
26
 * @see     ProcessorAbstract
27
 * @version 2.0.0
28
 * @since   2.0.0 added
29
 */
30
class UidProcessor extends ProcessorAbstract
31
{
32
    /**
33
     * @var    string
34
     * @access protected
35
     */
36
    protected $uid;
37
38
    /**
39
     * Using external uid or create one
40
     *
41
     * @param  string $uid external one
42
     * @param  int $length uid length
43
     * @access public
44
     */
45
    public function __construct(/*# string */ $uid = '', /*# int */ $length = 8)
46
    {
47
        if (empty($uid)) {
48
            $uid = substr(hash('md5', uniqid('', true)), 0, $length);
49
        }
50
        $this->uid = $uid;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function __invoke(LogEntryInterface $logEntry)
57
    {
58
        $context = $logEntry->getContext();
59
        $context['uid'] = $this->uid;
60
        $logEntry->setContext($context);
61
    }
62
}
63