| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Abacaphiliac\Doctrine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\DBAL\Logging\SQLLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Psr\Log\LogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use function array_merge; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function call_user_func; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function is_callable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use function microtime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function uniqid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | final class PsrSqlLoggerConfigurableLogLevels implements SQLLogger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var LoggerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var float */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $start; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var callable */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $startQueryCallable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $queryId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $defaultLogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var LogLevelConfiguration|null */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $logLevelConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         LoggerInterface $logger, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         LogLevelConfiguration $logLevelMapping, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         string $defaultLogLevel = LogLevel::INFO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->logger                = $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->logLevelConfiguration = $logLevelMapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->defaultLogLevel       = $defaultLogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->startQueryCallable    = $this->getStartQueryCallable($defaultLogLevel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private function getStartQueryCallable(string $level): callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $callable = $this->getLoggerCallable($level); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if (!is_callable($callable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             throw new InvalidArgumentException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 '%s::%s" is not callable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 LoggerInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $this->defaultLogLevel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return $callable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     private function getLoggerCallable(string $level) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return [$this->logger, $level]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     public function startQuery($sql, array $params = null, array $types = null) : void | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $this->queryId = uniqid('', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $this->start = microtime(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         call_user_func($this->startQueryCallable, 'Query started', array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             $this->getStartQueryContext($sql, $params, $types), | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 'query_id' => $this->queryId, | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     protected function getStartQueryContext($sql, array $params = null, array $types = null) : array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'sql' => $sql, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'types' => $types, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function stopQuery() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $stop = microtime(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $durationInSeconds = $stop - $this->start; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         call_user_func($this->getStopQueryCallable($durationInSeconds), 'Query finished', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'query_id' => $this->queryId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'start' => $this->start, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'stop' => $stop, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'duration_s' => $durationInSeconds, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     private function getStopQueryCallable(float $durationInSeconds): callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return $this->getLoggerCallable($this->getApplicableLogLevel($durationInSeconds) ?? $this->defaultLogLevel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     private function getApplicableLogLevel(float $durationInSeconds): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         return $this->logLevelConfiguration instanceof LogLevelConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ? $this->logLevelConfiguration->getApplicableLogLevel($durationInSeconds) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             : $this->defaultLogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.