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

Emitter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 26.32 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A emit() 11 11 1
A messageLoggerContext() 9 9 1
A log() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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