@@ 10-65 (lines=56) @@ | ||
7 | use NilPortugues\MessageBus\CommandBus\Contracts\CommandBusMiddleware as CommandBusMiddlewareInterface; |
|
8 | use Psr\Log\LoggerInterface; |
|
9 | ||
10 | class LoggerCommandBusMiddleware implements CommandBusMiddlewareInterface |
|
11 | { |
|
12 | /** @var LoggerInterface */ |
|
13 | protected $logger; |
|
14 | ||
15 | /** |
|
16 | * CachingCommandBus constructor. |
|
17 | * |
|
18 | * @param LoggerInterface $logger |
|
19 | */ |
|
20 | public function __construct(LoggerInterface $logger) |
|
21 | { |
|
22 | $this->logger = $logger; |
|
23 | } |
|
24 | ||
25 | /** |
|
26 | * @param Command $command |
|
27 | * @param callable|null $next |
|
28 | */ |
|
29 | public function __invoke(Command $command, callable $next = null) |
|
30 | { |
|
31 | try { |
|
32 | if ($next) { |
|
33 | $this->preCommandLog($command); |
|
34 | $next($command); |
|
35 | $this->postCommandLog($command); |
|
36 | } |
|
37 | } catch (Exception $e) { |
|
38 | $this->logException($e); |
|
39 | } |
|
40 | } |
|
41 | ||
42 | /** |
|
43 | * @param Command $command |
|
44 | */ |
|
45 | protected function preCommandLog(Command $command) |
|
46 | { |
|
47 | $this->logger->info(sprintf('Starting %s handling.', get_class($command))); |
|
48 | } |
|
49 | ||
50 | /** |
|
51 | * @param Command $command |
|
52 | */ |
|
53 | protected function postCommandLog(Command $command) |
|
54 | { |
|
55 | $this->logger->info(sprintf('%s was handled successfully.', get_class($command))); |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * @param Exception $e |
|
60 | */ |
|
61 | protected function logException(Exception $e) |
|
62 | { |
|
63 | $this->logger->alert(sprintf('[%s:%s] %s', $e->getFile(), $e->getLine(), $e->getMessage())); |
|
64 | } |
|
65 | } |
|
66 |
@@ 13-68 (lines=56) @@ | ||
10 | /** |
|
11 | * Class LoggerEventBusMiddleware. |
|
12 | */ |
|
13 | class LoggerEventBusMiddleware implements EventBusMiddlewareInterface |
|
14 | { |
|
15 | /** @var LoggerInterface */ |
|
16 | protected $logger; |
|
17 | ||
18 | /** |
|
19 | * CachingEventBus constructor. |
|
20 | * |
|
21 | * @param LoggerInterface $logger |
|
22 | */ |
|
23 | public function __construct(LoggerInterface $logger) |
|
24 | { |
|
25 | $this->logger = $logger; |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param Event $event |
|
30 | * @param callable|null $next |
|
31 | */ |
|
32 | public function __invoke(Event $event, callable $next = null) |
|
33 | { |
|
34 | try { |
|
35 | if ($next) { |
|
36 | $this->preEventLog($event); |
|
37 | $next($event); |
|
38 | $this->postEventLog($event); |
|
39 | } |
|
40 | } catch (Exception $e) { |
|
41 | $this->logException($e); |
|
42 | } |
|
43 | } |
|
44 | ||
45 | /** |
|
46 | * @param Event $event |
|
47 | */ |
|
48 | protected function preEventLog(Event $event) |
|
49 | { |
|
50 | $this->logger->info(sprintf('Starting %s handling.', get_class($event))); |
|
51 | } |
|
52 | ||
53 | /** |
|
54 | * @param Event $event |
|
55 | */ |
|
56 | protected function postEventLog(Event $event) |
|
57 | { |
|
58 | $this->logger->info(sprintf('%s was handled successfully.', get_class($event))); |
|
59 | } |
|
60 | ||
61 | /** |
|
62 | * @param Exception $e |
|
63 | */ |
|
64 | protected function logException(Exception $e) |
|
65 | { |
|
66 | $this->logger->alert(sprintf('[%s:%s] %s', $e->getFile(), $e->getLine(), $e->getMessage())); |
|
67 | } |
|
68 | } |
|
69 |