Completed
Push — master ( 02f3c5...149937 )
by Tomas
06:44
created

Emitter::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 3
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 DispatcherInterface
0 ignored issues
show
Bug introduced by
There is one abstract method registerHandler in this class; you could implement it, or declare this class as abstract.
Loading history...
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
        $this->startTime = new DateTime();
0 ignored issues
show
Bug introduced by
The property startTime does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 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...
42
    {
43
        $this->driver->send($message);
44
45
        $this->log(
46
            LogLevel::INFO,
47
            "Dispatcher send message #{$message->getId()} to driver " . get_class($this->driver),
48
            $this->messageLoggerContext($message)
0 ignored issues
show
Bug introduced by
The method messageLoggerContext() does not seem to exist on object<Tomaj\Hermes\Emitter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        );
50
        return $this;
51
    }
52
53
    /**
54
     * Interal log method wrapper
55
     *
56
     * @param string $level
57
     * @param string $message
58
     * @param array $context
59
     *
60
     * @return void
61
     */
62
    private function log($level, $message, array $context = array())
63
    {
64
        if ($this->logger) {
65
            $this->logger->log($level, $message, $context);
66
        }
67
    }
68
}
69