Completed
Push — master ( fa732c...3d1320 )
by Ross
9s
created

LoggerMiddleware::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
namespace League\Tactician\Logger;
3
4
use League\Tactician\Logger\Formatter\Formatter;
5
use League\Tactician\Middleware;
6
use Psr\Log\LoggerInterface;
7
use Exception;
8
9
/**
10
 * Add support for writing a message to the log whenever a command is received,
11
 * handled or failed.
12
 */
13
class LoggerMiddleware implements Middleware
14
{
15
    /**
16
     * @var LoggerInterface
17
     */
18
    private $logger;
19
20
    /**
21
     * @var Formatter
22
     */
23
    private $formatter;
24
25
    /**
26
     * @param Formatter $formatter
27
     * @param LoggerInterface $logger
28
     */
29
    public function __construct(Formatter $formatter, LoggerInterface $logger)
30
    {
31
        $this->formatter = $formatter;
32
        $this->logger = $logger;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function execute($command, callable $next)
39
    {
40
        $this->formatter->logCommandReceived($this->logger, $command);
41
42
        try {
43
            $returnValue = $next($command);
44
        } catch (Exception $e) {
45
            $this->formatter->logCommandFailed($this->logger, $command, $e);
46
            throw $e;
47
        }
48
49
        $this->formatter->logCommandSucceeded($this->logger, $command, $returnValue);
50
51
        return $returnValue;
52
    }
53
}
54