| 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 Psr\Log\LogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Add support for writing a message to the log whenever a command is received, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * handled or failed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class LoggerMiddleware implements Middleware | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var LoggerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var Formatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $commandReceivedLogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $commandHandledLogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $commandFailedLogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param Formatter $formatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param LoggerInterface $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $commandReceivedLogLevel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $commandHandledLogLevel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $commandFailedLogLevel | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         Formatter $formatter, | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         LoggerInterface $logger, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $commandReceivedLogLevel = LogLevel::DEBUG, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $commandHandledLogLevel = LogLevel::DEBUG, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $commandFailedLogLevel = LogLevel::ERROR | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->formatter = $formatter; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $this->logger = $logger; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->commandReceivedLogLevel = $commandReceivedLogLevel; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->commandHandledLogLevel = $commandHandledLogLevel; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->commandFailedLogLevel = $commandFailedLogLevel; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function execute($command, callable $next) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $commandContext = $this->formatter->commandContext($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->log( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->commandReceivedLogLevel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $this->formatter->commandReceived($command), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $commandContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $returnValue = $next($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $this->log( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 $this->commandFailedLogLevel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $this->formatter->commandFailed($command), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $this->formatter->failureContext($commandContext, $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->log( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $this->commandHandledLogLevel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $this->formatter->commandHandled($command), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $commandContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return $returnValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Write a message to the log or skip over it if the message is null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param string $logLevel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param string|null $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param array $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     protected function log($logLevel, $message, array $context = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         if ($message === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->logger->log($logLevel, $message, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  |