These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Drupal\mongodb_watchdog\Controller; |
||
| 4 | |||
| 5 | use Drupal\Core\Config\ImmutableConfig; |
||
| 6 | use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
||
| 7 | use Drupal\Core\Template\Attribute; |
||
| 8 | use Drupal\mongodb_watchdog\EventController; |
||
| 9 | use Drupal\mongodb_watchdog\EventTemplate; |
||
| 10 | use Drupal\mongodb_watchdog\Logger; |
||
| 11 | use Psr\Log\LoggerInterface; |
||
| 12 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
| 13 | use Symfony\Component\HttpFoundation\Request; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class DetailController implements the controller for the event detail page. |
||
| 17 | */ |
||
| 18 | class DetailController extends ControllerBase { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The mongodb.watchdog_event_controller service. |
||
| 22 | * |
||
| 23 | * @var \Drupal\mongodb_watchdog\EventController |
||
| 24 | */ |
||
| 25 | protected $eventController; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Controller constructor. |
||
| 29 | * |
||
| 30 | * @param \Psr\Log\LoggerInterface $logger |
||
| 31 | * The logger service, to log intervening events. |
||
| 32 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 33 | * The MongoDB logger, to load stored events. |
||
| 34 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 35 | * The module configuration. |
||
| 36 | * @param \Drupal\mongodb_watchdog\EventController $event_controller |
||
| 37 | * The event controller service. |
||
| 38 | */ |
||
| 39 | public function __construct( |
||
| 40 | LoggerInterface $logger, |
||
| 41 | Logger $watchdog, |
||
| 42 | ImmutableConfig $config, |
||
| 43 | EventController $event_controller) { |
||
| 44 | parent::__construct($logger, $watchdog, $config); |
||
| 45 | |||
| 46 | $this->eventController = $event_controller; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Controller. |
||
| 51 | * |
||
| 52 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 53 | * The current request. |
||
| 54 | * @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
||
| 55 | * The event template. |
||
| 56 | * |
||
| 57 | * @return array A render array. |
||
|
0 ignored issues
–
show
|
|||
| 58 | * A render array. |
||
| 59 | */ |
||
| 60 | public function build(Request $request, EventTemplate $event_template) { |
||
| 61 | $page = $this->setupPager($request, $event_template); |
||
| 62 | $template_rows = $this->buildHeader($event_template); |
||
| 63 | $event_rows = $this->buildRows($event_template, $page); |
||
| 64 | |||
| 65 | $base = [ |
||
| 66 | '#attributes' => new Attribute(['class' => 'mongodb_watchdog-detail']), |
||
| 67 | '#type' => 'table', |
||
| 68 | ]; |
||
| 69 | |||
| 70 | $event_header = [ |
||
| 71 | t('Date'), |
||
| 72 | t('User'), |
||
| 73 | t('Message'), |
||
| 74 | t('Location'), |
||
| 75 | t('Referrer'), |
||
| 76 | t('Hostname'), |
||
| 77 | t('Operations'), |
||
| 78 | ]; |
||
| 79 | |||
| 80 | $ret = [ |
||
| 81 | 'template' => $base + [ |
||
| 82 | '#caption' => t('Event template'), |
||
| 83 | '#rows' => $template_rows, |
||
| 84 | ], |
||
| 85 | 'events' => $base + [ |
||
| 86 | '#caption' => t('Event occurrences'), |
||
| 87 | '#header' => $event_header, |
||
| 88 | '#rows' => $event_rows, |
||
| 89 | ], |
||
| 90 | 'pager' => [ |
||
| 91 | '#type' => 'pager', |
||
| 92 | ], |
||
| 93 | '#attached' => [ |
||
| 94 | 'library' => ['mongodb_watchdog/styling'], |
||
| 95 | ], |
||
| 96 | ]; |
||
| 97 | |||
| 98 | return $ret; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Build the heading rows on the event occurrences page. |
||
| 103 | * |
||
| 104 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 105 | * The event template. |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | * A table render array. |
||
| 109 | */ |
||
| 110 | protected function buildHeader(EventTemplate $template) { |
||
| 111 | $rows = []; |
||
| 112 | foreach (EventTemplate::keys() as $key => $info) { |
||
| 113 | $value = $template->{$key}; |
||
| 114 | $row = [ |
||
| 115 | [ |
||
| 116 | 'header' => TRUE, |
||
| 117 | 'data' => $info['label'], |
||
| 118 | ], |
||
| 119 | isset($info['display_callback']) ? $info['display_callback']($value) : $value, |
||
| 120 | ]; |
||
| 121 | $rows[] = $row; |
||
| 122 | } |
||
| 123 | return $rows; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Build the occurrence rows on the event occurrences page. |
||
| 128 | * |
||
| 129 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 130 | * The event template. |
||
| 131 | * @param int $page |
||
| 132 | * The page number, starting at 0. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | * A table render array. |
||
| 136 | */ |
||
| 137 | protected function buildRows(EventTemplate $template, $page) { |
||
| 138 | $rows = []; |
||
| 139 | $skip = $page * $this->itemsPerPage; |
||
| 140 | $limit = $this->itemsPerPage; |
||
| 141 | $events = $this->eventController->find($template, $skip, $limit); |
||
| 142 | |||
| 143 | /** @var \Drupal\mongodb_watchdog\Event $event */ |
||
| 144 | foreach ($events as $event) { |
||
| 145 | $rows[] = $this->eventController->asTableRow($template, $event); |
||
| 146 | } |
||
| 147 | return $rows; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Title callback for mongodb_watchdog.detail. |
||
| 152 | * |
||
| 153 | * @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
||
| 154 | * The event template for which the title is built. |
||
| 155 | * |
||
| 156 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup |
||
| 157 | * The page title. |
||
| 158 | */ |
||
| 159 | public function buildTitle(EventTemplate $event_template) { |
||
| 160 | return t('MongoDB events: "@template"', ['@template' => $event_template->message]); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | View Code Duplication | public static function create(ContainerInterface $container) { |
|
| 167 | /** @var \Psr\Log\LoggerInterface $logger */ |
||
| 168 | $logger = $container->get('logger.channel.mongodb_watchdog'); |
||
| 169 | |||
| 170 | /** @var \Drupal\mongodb_watchdog\Logger $watchdog */ |
||
| 171 | $watchdog = $container->get('mongodb.logger'); |
||
| 172 | |||
| 173 | /** @var \Drupal\Core\Config\ImmutableConfig $config */ |
||
| 174 | $config = $container->get('config.factory')->get('mongodb_watchdog.settings'); |
||
| 175 | |||
| 176 | /** @var \Drupal\mongodb_watchdog\EventController $eventController */ |
||
| 177 | $eventController = $container->get('mongodb.watchdog_event_controller'); |
||
| 178 | |||
| 179 | return new static($logger, $watchdog, $config, $eventController); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set up the pager. |
||
| 184 | * |
||
| 185 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 186 | * The current request. |
||
| 187 | * |
||
| 188 | * @return int |
||
| 189 | * The number of the page to display, starting at 0. |
||
| 190 | */ |
||
| 191 | View Code Duplication | public function setupPager(Request $request, EventTemplate $template) { |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 192 | $count = $this->watchdog->eventCount($template); |
||
| 193 | $height = $this->itemsPerPage; |
||
| 194 | pager_default_initialize($count, $height); |
||
| 195 | |||
| 196 | $page = intval($request->query->get('page')); |
||
| 197 | if ($page < 0) { |
||
| 198 | $page = 0; |
||
| 199 | } |
||
| 200 | else { |
||
| 201 | $page_max = intval(min(ceil($count / $height), PHP_INT_MAX) - 1); |
||
| 202 | if ($page > $page_max) { |
||
| 203 | $page = $page_max; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | return $page; |
||
| 208 | } |
||
| 209 | |||
| 210 | } |
||
| 211 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.