vendor/m6web/amqp-bundle/src/AmqpBundle/Amqp/DataCollector.php line 41

Open in your IDE?
  1. <?php
  2. namespace M6Web\Bundle\AmqpBundle\Amqp;
  3. use Symfony\Component\HttpKernel\DataCollector\DataCollector as SymfonyDataCollector;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. /**
  7.  * Handle datacollector for amqp.
  8.  */
  9. class DataCollector extends SymfonyDataCollector
  10. {
  11.     /**
  12.      * @param string $name
  13.      *
  14.      * Construct the data collector
  15.      */
  16.     public function __construct(string $name)
  17.     {
  18.         $this->data['name'] = $name;
  19.         $this->reset();
  20.     }
  21.     /**
  22.      * Collect the data.
  23.      *
  24.      * @param Request    $request   The request object
  25.      * @param Response   $response  The response object
  26.      * @param \Exception $exception An exception
  27.      */
  28.     public function collect(Request $requestResponse $response, \Exception $exception null)
  29.     {
  30.     }
  31.     /**
  32.      * Listen for command event.
  33.      *
  34.      * @param object $event The event object
  35.      */
  36.     public function onCommand($event)
  37.     {
  38.         $this->data['commands'][] = array(
  39.             'command' => $event->getCommand(),
  40.             'arguments' => $event->getArguments(),
  41.             'executiontime' => $event->getExecutionTime(),
  42.         );
  43.     }
  44.     /**
  45.      * Return command list and number of times they were called.
  46.      *
  47.      * @return array The command list and number of times called
  48.      */
  49.     public function getCommands(): array
  50.     {
  51.         return $this->data['commands'];
  52.     }
  53.     /**
  54.      * Return the name of the collector.
  55.      *
  56.      * @return string data collector name
  57.      */
  58.     public function getName(): string
  59.     {
  60.         return $this->data['name'];
  61.     }
  62.     /**
  63.      * Return total command execution time.
  64.      *
  65.      * @return float
  66.      */
  67.     public function getTotalExecutionTime(): float
  68.     {
  69.         return (float) array_sum(array_column($this->data['commands'], 'executiontime'));
  70.     }
  71.     /**
  72.      * Get average execution time.
  73.      *
  74.      * @return float
  75.      */
  76.     public function getAvgExecutionTime(): float
  77.     {
  78.         return $this->getTotalExecutionTime() ? ($this->getTotalExecutionTime() / \count($this->data['commands'])) : (float) 0;
  79.     }
  80.     /**
  81.      * {@inheritdoc}
  82.      */
  83.     public function reset()
  84.     {
  85.         // Reset the current data, while keeping the 'name' intact.
  86.         $this->data = [
  87.             'name' => $this->data['name'],
  88.             'commands' => [],
  89.         ];
  90.     }
  91. }