Completed
Push — master ( 165f6c...dd3dbb )
by Franck
05:41
created

AbstractTracker   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 75
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generateUuid() 0 6 2
A setEmitter() 0 5 1
A getEmitter() 0 4 1
A write() 0 9 3
A getUuid() 0 4 1
1
<?php
2
3
namespace Apix\Log;
4
5
use Apix\Log\Emitter\EmitterInterface as LogEmitter;
6
use Ramsey\Uuid\Uuid;
7
8
/**
9
 * Abstract tracking logger for Apix Log.
10
 */
11
abstract class AbstractTracker extends Logger\AbstractLogger implements Logger\LoggerInterface
12
{
13
14
    /**
15
     * Holds a LogEmitter instance.
16
     *
17
     * @var LogEmitter
18
     */
19
    protected $emitter;
20
21
    /**
22
     * Holds the current UUID (currently only v4).
23
     *
24
     * @var string
25
     */
26
    protected $uuid;
27
28
    /**
29
     * Returns the log emmiter.
30
     *
31
     * @return LogEmitter
32
     */
33 108
    public function setEmitter(LogEmitter $emitter, LogFormatter $formatter)
34
    {
35 108
        $this->emitter = $emitter;
36 108
        $this->setLogFormatter($formatter);
37 108
    }
38
39
    /**
40
     * Returns the log emmiter.
41
     *
42
     * @return LogEmitter
43
     */
44 108
    public function getEmitter()
45
    {
46 108
        return $this->emitter;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 28
    public function write(LogEntry $log)
53
    {
54
        try {
55 28
            $payload = $this->deferred ? $log->message : $log;
56 28
            return $this->emitter->send((string)$payload);
57 8
        } catch (\Exception $e) {
58 8
            throw new Exception($e->getMessage(), $e->getCode(), $e);
59
        }
60
    }
61
62
    /**
63
     * Generates an UUID (currently only v4).
64
     *
65
     * @https://pecl.php.net/package/uuid
66
     *
67
     * @return string
68
     */
69 8
    public static function generateUuid()
70
    {
71 8
        return function_exists('uuid_create')
72 6
                ? uuid_create(\UUID_TYPE_DEFAULT) // PECL extension is faster.
73 8
                : Uuid::uuid4()->toString();
74
    }
75
76
    /**
77
     * Returns the current UUID.
78
     *
79
     * @return string
80
     */
81 4
    public function getUuid()
82
    {
83 4
        return $this->uuid;
84
    }
85
}
86