Completed
Push — master ( fb2f46...3cbccd )
by Tomas
04:01
created

Emitter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Tomaj\Hermes;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\LogLevel;
7
use Tomaj\Hermes\Driver\DriverInterface;
8
9
class Emitter implements EmitterInterface
10
{
11
    /**
12
     * Dispatcher driver
13
     *
14
     * @var DriverInterface
15
     */
16
    private $driver;
17
18
    /**
19
     * Logger
20
     *
21
     * @var LoggerInterface
22
     */
23
    private $logger;
24
25
    /**
26
     * Create new Dispatcher
27
     *
28
     * @param DriverInterface $driver
29
     * @param LoggerInterface $logger
30
     */
31
    public function __construct(DriverInterface $driver, LoggerInterface $logger = null)
32
    {
33
        $this->driver = $driver;
34
        $this->logger = $logger;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 View Code Duplication
    public function emit(MessageInterface $message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $this->driver->send($message);
43
44
        $this->log(
45
            LogLevel::INFO,
46
            "Dispatcher send message #{$message->getId()} to driver " . get_class($this->driver),
47
            $this->messageLoggerContext($message)
48
        );
49
        return $this;
50
    }
51
52
    /**
53
     * Serialize message to logger context
54
     *
55
     * @param MessageInterface $message
56
     *
57
     * @return array
58
     */
59 View Code Duplication
    private function messageLoggerContext(MessageInterface $message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        return [
62
            'id' => $message->getId(),
63
            'created' => $message->getCreated(),
64
            'type' => $message->getType(),
65
            'payload' => $message->getPayload(),
66
        ];
67
    }
68
69
    /**
70
     * Interal log method wrapper
71
     *
72
     * @param string $level
73
     * @param string $message
74
     * @param array $context
75
     *
76
     * @return void
77
     */
78
    private function log($level, $message, array $context = array())
79
    {
80
        if ($this->logger) {
81
            $this->logger->log($level, $message, $context);
82
        }
83
    }
84
}
85