Completed
Push — 8.x-2.x ( 74e505...ec080d )
by Frédéric G.
03:07
created

DetailController::buildTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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,string|array>
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
      ? $this->buildEmpty(t('No occurrence of this event found in logger.'))
65
      : $this->buildMainTable($rows, $eventTemplate);
66
67
    $ret = $this->buildDefaults($main, $top);
68
    return $ret;
69
  }
70
71
  /**
72
   * Build the main table.
73
   *
74
   * @param \Drupal\mongodb_watchdog\Event[] $events
75
   *   The event data.
76
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
77
   *   The template for which to built the detail lines.
78
   *
79
   * @return array<string,string|array>
80
   *   A render array for the main table.
81
   */
82
  protected function buildMainTable(array $events, EventTemplate $eventTemplate) {
83
    $ret = [
84
      '#attributes' => new Attribute(['class' => 'mongodb_watchdog__detail']),
85
      '#caption' => t('Event occurrences'),
86
      '#header' => $this->buildMainTableHeader(),
87
      '#rows' => $this->buildMainTableRows($events, $eventTemplate),
88
      '#type' => 'table',
89
    ];
90
91
    return $ret;
92
  }
93
94
  /**
95
   * Build the main table header.
96
   *
97
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
98
   *   A table header array.
99
   */
100
  protected function buildMainTableHeader() {
101
    $header = [
102
      t('Date'),
103
      t('User'),
104
      t('Message'),
105
      t('Location'),
106
      t('Referrer'),
107
      t('Hostname'),
108
      t('Operations'),
109
    ];
110
111
    return $header;
112
  }
113
114
  /**
115
   * Build the main table rows.
116
   *
117
   * @param \Drupal\mongodb_watchdog\Event[] $events
118
   *   The event row data.
119
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
120
   *   The template for these events.
121
   *
122
   * @return array<string,array|string>
123
   *   A render array for a table.
124
   */
125
  protected function buildMainTableRows(array $events, EventTemplate $eventTemplate) {
126
    $rows = [];
127
128
    foreach ($events as $event) {
129
      // TODO bring this back from "model": it is a display method.
130
      $rows[] = $this->eventController->asTableRow($eventTemplate, $event);
131
    }
132
133
    return $rows;
134
  }
135
136
  /**
137
   * Title callback for mongodb_watchdog.detail.
138
   *
139
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
140
   *   The event template for which the title is built.
141
   *
142
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
143
   *   The page title.
144
   */
145
  public function buildTitle(EventTemplate $eventTemplate) {
146
    return t('MongoDB events: "@template"', ['@template' => $eventTemplate->message]);
147
  }
148
149
  /**
150
   * {@inheritdoc}
151
   */
152 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...
153
    /** @var \Psr\Log\LoggerInterface $logger */
154
    $logger = $container->get('logger.channel.mongodb_watchdog');
155
156
    /** @var \Drupal\mongodb_watchdog\Logger $watchdog */
157
    $watchdog = $container->get('mongodb.logger');
158
159
    /** @var \Drupal\Core\Config\ImmutableConfig $config */
160
    $config = $container->get('config.factory')->get('mongodb_watchdog.settings');
161
162
    /** @var \Drupal\mongodb_watchdog\EventController $eventController */
163
    $eventController = $container->get('mongodb.watchdog_event_controller');
164
165
    return new static($logger, $watchdog, $config, $eventController);
166
  }
167
168
  /**
169
   * Obtain the data from the logger.
170
   *
171
   * @param \Symfony\Component\HttpFoundation\Request $request
172
   *   The current request. Needed for paging.
173
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
174
   *   The template for which to build the detail page.
175
   *
176
   * @return \Drupal\mongodb_watchdog\Event[]
177
   *   The data array.
178
   */
179 View Code Duplication
  protected function getRowData(Request $request, EventTemplate $eventTemplate) {
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...
180
    $count = $this->watchdog->eventCount($eventTemplate);
181
    $page = $this->setupPager($request, $count);
182
    $skip = $page * $this->itemsPerPage;
183
    $limit = $this->itemsPerPage;
184
185
    $rows = $this->eventController
186
      ->find($eventTemplate, $skip, $limit)
187
      ->toArray();
188
189
    return $rows;
190
  }
191
192
  /**
193
   * Build the heading rows on the per-template event occurrences page.
194
   *
195
   * @param \Drupal\mongodb_watchdog\EventTemplate|null $eventTemplate
196
   *   The template for which to provide details. Not actually expected to be
197
   *   NULL, but this is needed to remain compatible with parent class.
198
   *
199
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
200
   *   A render array for a table.
201
   */
202
  protected function getTop(EventTemplate $eventTemplate = NULL) {
203
    $rows = [];
204
    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...
205
      $value = $eventTemplate->{$key};
206
      $row = [];
207
      $row[] = [
208
        'header' => TRUE,
209
        'data' => $info['label'],
210
      ];
211
      $row[] = isset($info['display_callback']) ? $info['display_callback']($value) : $value;
212
      $rows[] = $row;
213
    }
214
215
    $ret = [
216
      '#caption' => t('Event template'),
217
      '#rows' => $rows,
218
      '#type' => 'table',
219
    ];
220
221
    return $ret;
222
  }
223
224
}
225