Completed
Push — master ( 1a9396...a94258 )
by Till
03:52
created

LoggerMiddleware::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 9.0856
cc 3
eloc 14
nc 3
nop 2
1
<?php
2
3
namespace TillKruss\LaravelTactician\Middleware;
4
5
use Exception;
6
use Throwable;
7
use League\Tactician\Middleware;
8
use Illuminate\Contracts\Logging\Log;
9
use Illuminate\Foundation\Application;
10
11
class LoggerMiddleware implements Middleware
12
{
13
    protected $app;
14
    protected $logger;
15
16
    public function __construct(Log $logger, Application $app) {
17
        $this->app = $app;
18
        $this->logger = $logger;
19
    }
20
21
    public function execute($command, callable $next)
22
    {
23
        $levels = $this->app->config->get('tactician.log.levels');
24
        $formatter = $this->app->make($this->app->config->get('tactician.log.formatter'));
25
26
        $this->log($levels['received'], $formatter->commandReceived($command));
27
28
        try {
29
            $returnValue = $next($command);
30
        } catch (Exception $exception) {
31
            $this->log($levels['failed'], $formatter->commandFailed($command, $exception));
32
33
            throw $exception;
34
        } catch (Throwable $exception) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
35
            $this->log($levels['failed'], $formatter->commandFailed($command, $exception));
36
37
            throw $exception;
38
        }
39
40
        $this->log($levels['handled'], $formatter->commandHandled($command));
41
42
        return $returnValue;
43
    }
44
45
    protected function log($logLevel, $message)
46
    {
47
        if (! is_null($message)) {
48
            $this->logger->log($logLevel, $message);
49
        }
50
    }
51
}
52