Completed
Push — 8.x-2.x ( f7089a...f2468c )
by Frédéric G.
03:14
created

src/Controller/DetailController.php (2 issues)

Upgrade to new PHP Analysis Engine

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 implements ContainerInjectionInterface {
19
20
  /**
21
   * The mongodb.watchdog_event_controller service.
22
   *
23
   * @var \Drupal\mongodb_watchdog\EventController
24
   */
25
  protected $eventController;
26
27
  /**
28
   * The items_per_page configuration value.
29
   *
30
   * @var int
31
   */
32
  protected $itemsPerPage;
33
34
  /**
35
   * The logger.mongodb_watchdog logger channel, to log events.
36
   *
37
   * @var \Psr\Log\LoggerInterface
38
   */
39
  protected $logger;
40
41
  /**
42
   * The MongoDB logger service, to load events.
43
   *
44
   * @var \Drupal\mongodb_watchdog\Logger
45
   */
46
  protected $watchdog;
47
48
  /**
49
   * Constructor.
50
   *
51
   * @param \Psr\Log\LoggerInterface $logger
52
   *   The logger service, to log intervening events.
53
   * @param \Drupal\mongodb_watchdog\EventController $event_controller
54
   *   The event controller service.
55
   * @param \Drupal\Core\Config\ImmutableConfig $config
56
   *   The module configuration.
57
   */
58
  public function __construct(Logger $watchdog, LoggerInterface $logger, EventController $event_controller, ImmutableConfig $config) {
59
    $this->config = $config;
60
    $this->eventController = $event_controller;
61
    $this->logger = $logger;
62
    $this->watchdog = $watchdog;
63
64
    $this->itemsPerPage = $config->get('items_per_page');
65
  }
66
67
  /**
68
   * {@inheritdoc}
69
   */
70 View Code Duplication
  public static function create(ContainerInterface $container) {
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...
71
    /** @var \Psr\Log\LoggerInterface $logger */
72
    $logger = $container->get('logger.channel.mongodb_watchdog');
73
74
    /** @var \Drupal\mongodb_watchdog\EventController $eventController */
75
    $eventController = $container->get('mongodb.watchdog_event_controller');
76
77
    /** @var \Drupal\mongodb_watchdog\Logger $watchdog */
78
    $watchdog = $container->get('mongodb.logger');
79
80
    /** @var array $config */
81
    $config = $container->get('config.factory')->get('mongodb_watchdog.settings');
82
83
    return new static($watchdog, $logger, $eventController, $config);
84
  }
85
86
  /**
87
   * Controller for mongodb_watchdog.detail.
88
   *
89
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
90
   *   The event template.
91
   *
92
   * @return array
93
   *   A render array.
94
   */
95
  public function detail(EventTemplate $event_template, Request $request) {
96
    $page = $this->setupPager($event_template, $request);
97
    $template_rows = $this->detailHeader($event_template);
98
    $event_rows = $this->detailRows($event_template, $page);
99
100
    $base = [
101
      '#attributes' => new Attribute(['class' => 'mongodb_watchdog-detail']),
102
      '#type' => 'table',
103
    ];
104
105
    $event_header = [
106
      t('Date'),
107
      t('User'),
108
      t('Message'),
109
      t('Location'),
110
      t('Referrer'),
111
      t('Hostname'),
112
      t('Operations'),
113
    ];
114
115
    $ret = [
116
      "#attached" => [
117
        'library' => 'mongodb_watchdog/styling',
118
      ],
119
      'template' => $base + [
120
        '#caption' => t('Event template'),
121
        '#rows' => $template_rows,
122
      ],
123
      'events' => $base + [
124
        '#caption' => t('Event occurrences'),
125
        '#header' => $event_header,
126
        '#rows' => $event_rows,
127
      ],
128
      'pager' => [
129
        '#type' => 'pager',
130
      ],
131
    ];
132
133
    return $ret;
134
  }
135
136
  /**
137
   * Build the heading rows on the event occurrences page.
138
   *
139
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
140
   *   The event template.
141
   *
142
   * @return array
143
   *   A table render array.
144
   */
145
  protected function detailHeader(EventTemplate $template) {
146
    $rows = [];
147
    foreach (EventTemplate::keys() as $key => $info) {
148
      $value = $template->{$key};
149
      $row = [
150
        [
151
          'header' => TRUE,
152
          'data' => $info['label'],
153
        ],
154
        isset($info['display_callback']) ? $info['display_callback']($value) : $value,
155
      ];
156
      $rows[] = $row;
157
    }
158
    return $rows;
159
  }
160
161
  /**
162
   * Build the occurrence rows on the event occurrences page.
163
   *
164
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
165
   *   The event template.
166
   * @param int $page
167
   *   The page number, starting at 0.
168
   *
169
   * @return array
170
   *   A table render array.
171
   */
172
  protected function detailRows(EventTemplate $template, $page) {
173
    $rows = [];
174
    $skip = $page * $this->itemsPerPage;
175
    $limit = $this->itemsPerPage;
176
    $events = $this->eventController->find($template, $skip, $limit);
177
178
    /** @var \Drupal\mongodb_watchdog\Event $event */
179
    foreach ($events as $event) {
180
      $rows[] = $this->eventController->asTableRow($template, $event);
181
    }
182
    return $rows;
183
  }
184
185
  /**
186
   * Title callback for mongodb_watchdog.detail.
187
   *
188
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
189
   *   The event template for which the title is built.
190
   *
191
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
192
   *   The page title.
193
   */
194
  public function detailTitle(EventTemplate $event_template) {
195
    return t('MongoDB events: "@template"', ['@template' => $event_template->message]);
196
  }
197
198
  /**
199
   * Set up the events pager.
200
   *
201
   * @param \Symfony\Component\HttpFoundation\Request $request
202
   *   The current request.
203
   *
204
   * @return int
205
   *   The number of the page to display, starting at 0.
206
   */
207 View Code Duplication
  public function setupPager(EventTemplate $template, Request $request) {
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...
208
    $count = $this->watchdog->eventCount($template);
209
    $height = $this->itemsPerPage;
210
    pager_default_initialize($count, $height);
211
212
    $page = intval($request->query->get('page'));
213
    if ($page < 0) {
214
      $page = 0;
215
    }
216
    else {
217
      $page_max = intval(min(ceil($count / $height), PHP_INT_MAX) - 1);
218
      if ($page > $page_max) {
219
        $page = $page_max;
220
      }
221
    }
222
223
    return $page;
224
  }
225
226
}
227