Completed
Pull Request — 8.x-2.x (#24)
by Frédéric G.
02:40
created

DetailController::buildMainTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace Drupal\mongodb_watchdog\Controller;
4
5
use Drupal\Core\Config\ImmutableConfig;
6
use Drupal\Core\Template\Attribute;
7
use Drupal\mongodb_watchdog\EventController;
8
use Drupal\mongodb_watchdog\EventTemplate;
9
use Drupal\mongodb_watchdog\Logger;
10
use Psr\Log\LoggerInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * The controller for the event detail page.
16
 */
17
class DetailController extends ControllerBase {
18
19
  /**
20
   * The mongodb.watchdog_event_controller service.
21
   *
22
   * @var \Drupal\mongodb_watchdog\EventController
23
   */
24
  protected $eventController;
25
26
  /**
27
   * Controller constructor.
28
   *
29
   * @param \Psr\Log\LoggerInterface $logger
30
   *   The logger service, to log intervening events.
31
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
32
   *   The MongoDB logger, to load stored events.
33
   * @param \Drupal\Core\Config\ImmutableConfig $config
34
   *   The module configuration.
35
   * @param \Drupal\mongodb_watchdog\EventController $eventController
36
   *   The event controller service.
37
   */
38
  public function __construct(
39
    LoggerInterface $logger,
40
    Logger $watchdog,
41
    ImmutableConfig $config,
42
    EventController $eventController) {
43
    parent::__construct($logger, $watchdog, $config);
44
45
    $this->eventController = $eventController;
46
  }
47
48
  /**
49
   * Controller.
50
   *
51
   * @param \Symfony\Component\HttpFoundation\Request $request
52
   *   The current request.
53
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
54
   *   The event template.
55
   *
56
   * @return array<string,array>
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string|array>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
57
   *   A render array.
58
   */
59
  public function build(Request $request, EventTemplate $eventTemplate) {
60
    $top = $this->getTop($eventTemplate);
61
62
    $rows = $this->getRowData($request, $eventTemplate);
63
    $main = empty($rows)
64
      ? [
65
        '#markup' => t('No occurrence of this event found in logger.'),
66
        '#prefix' => '<div class="mongodb_watchdog__message">',
67
        '#suffix' => '</div>',
68
      ]
69
      : $this->buildMainTable($rows, $eventTemplate);
70
71
    $ret = $this->buildDefaults($main, $top);
72
    return $ret;
73
  }
74
75
  /**
76
   * Build the main table.
77
   *
78
   * @param \Drupal\mongodb_watchdog\Event[] $events
79
   *   The event data.
80
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
81
   *   The template for which to built the detail lines.
82
   *
83
   * @return array<string,string|array>
84
   *   A render array for the main table.
85
   */
86
  protected function buildMainTable(array $events, EventTemplate $eventTemplate) {
87
    $ret = [
88
      '#attributes' => new Attribute(['class' => 'mongodb_watchdog__detail']),
89
      '#caption' => t('Event occurrences'),
90
      '#header' => $this->buildMainTableHeader(),
91
      '#rows' => $this->buildMainTableRows($events, $eventTemplate),
92
      '#type' => 'table',
93
    ];
94
95
    return $ret;
96
  }
97
98
  /**
99
   * Build the main table header.
100
   *
101
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
102
   *   A table header array.
103
   */
104
  protected function buildMainTableHeader() {
105
    $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
    return $header;
116
  }
117
118
  /**
119
   * Build the main table rows.
120
   *
121
   * @param \Drupal\mongodb_watchdog\Event[] $events
122
   *   The event row data.
123
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
124
   *   The template for these events.
125
   *
126
   * @return array<string,array|string>
127
   *   A render array for a table.
128
   */
129
  protected function buildMainTableRows(array $events, EventTemplate $eventTemplate) {
130
    $rows = [];
131
132
    foreach ($events as $event) {
133
      // TODO bring this back from "model": it is a display method.
134
      $rows[] = $this->eventController->asTableRow($eventTemplate, $event);
135
    }
136
137
    return $rows;
138
  }
139
140
  /**
141
   * Title callback for mongodb_watchdog.detail.
142
   *
143
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
144
   *   The event template for which the title is built.
145
   *
146
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
147
   *   The page title.
148
   */
149
  public function buildTitle(EventTemplate $eventTemplate) {
150
    return t('MongoDB events: "@template"', ['@template' => $eventTemplate->message]);
151
  }
152
153
  /**
154
   * {@inheritdoc}
155
   */
156 View Code Duplication
  public static function create(ContainerInterface $container) {
0 ignored issues
show
Duplication introduced by
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...
157
    /** @var \Psr\Log\LoggerInterface $logger */
158
    $logger = $container->get('logger.channel.mongodb_watchdog');
159
160
    /** @var \Drupal\mongodb_watchdog\Logger $watchdog */
161
    $watchdog = $container->get('mongodb.logger');
162
163
    /** @var \Drupal\Core\Config\ImmutableConfig $config */
164
    $config = $container->get('config.factory')->get('mongodb_watchdog.settings');
165
166
    /** @var \Drupal\mongodb_watchdog\EventController $eventController */
167
    $eventController = $container->get('mongodb.watchdog_event_controller');
168
169
    return new static($logger, $watchdog, $config, $eventController);
170
  }
171
172
  /**
173
   * Obtain the data from the logger.
174
   *
175
   * @param \Symfony\Component\HttpFoundation\Request $request
176
   *   The current request. Needed for paging.
177
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
178
   *   The template for which to build the detail page.
179
   *
180
   * @return \Drupal\mongodb_watchdog\Event[]
181
   *   The data array.
182
   */
183
  protected function getRowData(Request $request, EventTemplate $eventTemplate) {
184
    $count = $this->watchdog->eventCount($eventTemplate);
185
    $page = $this->setupPager($request, $count);
186
    $skip = $page * $this->itemsPerPage;
187
    $limit = $this->itemsPerPage;
188
189
    $rows = $this->eventController
190
      ->find($eventTemplate, $skip, $limit)
191
      ->toArray();
192
193
    return $rows;
194
  }
195
196
  /**
197
   * Build the heading rows on the event occurrences page.
198
   *
199
   * @param \Drupal\mongodb_watchdog\EventTemplate|null $eventTemplate
200
   *   The template for which to provide details. Not actually expected to be
201
   *   NULL, but this is needed to remain compatible with parent class.
202
   *
203
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
204
   *   A render array for a table.
205
   */
206
  protected function getTop(EventTemplate $eventTemplate = NULL) {
207
    $rows = [];
208
    foreach ($eventTemplate->keys() as $key => $info) {
0 ignored issues
show
Bug introduced by
It seems like $eventTemplate is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
209
      $value = $eventTemplate->{$key};
210
      $row = [];
211
      $row[] = [
212
        'header' => TRUE,
213
        'data' => $info['label'],
214
      ];
215
      $row[] = isset($info['display_callback']) ? $info['display_callback']($value) : $value;
216
      $rows[] = $row;
217
    }
218
219
    $ret = [
220
      '#caption' => t('Event template'),
221
      '#rows' => $rows,
222
      '#type' => 'table',
223
    ];
224
225
    return $ret;
226
  }
227
228
}
229