DetailController::buildMainTableRows()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb_watchdog\Controller;
6
7
use Drupal\Component\Render\MarkupInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Component\Render\MarkupInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\Core\Config\ImmutableConfig;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Config\ImmutableConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Drupal\Core\Pager\PagerManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Pager\PagerManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Drupal\Core\Template\Attribute;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Template\Attribute was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Drupal\mongodb_watchdog\EventController;
12
use Drupal\mongodb_watchdog\EventTemplate;
13
use Drupal\mongodb_watchdog\Logger;
14
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\DependencyInjection\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
/**
19
 * The controller for the event detail page.
20
 */
21
class DetailController extends ControllerBase {
22
23
  /**
24
   * The mongodb.watchdog_event_controller service.
25
   *
26
   * @var \Drupal\mongodb_watchdog\EventController
27
   */
28
  protected $eventController;
29
30
  /**
31
   * Controller constructor.
32
   *
33
   * @param \Psr\Log\LoggerInterface $logger
34
   *   The logger service, to log intervening events.
35
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
36
   *   The MongoDB logger, to load stored events.
37
   * @param \Drupal\Core\Config\ImmutableConfig $config
38
   *   The module configuration.
39
   * @param \Drupal\mongodb_watchdog\EventController $eventController
40
   *   The event controller service.
41
   * @param \Drupal\Core\Pager\PagerManagerInterface $pagerManager
42
   *   The core pager.manager service.
43
   */
44
  public function __construct(
45
    LoggerInterface $logger,
46
    Logger $watchdog,
47
    ImmutableConfig $config,
48
    EventController $eventController,
49
    PagerManagerInterface $pagerManager) {
50
    parent::__construct($logger, $watchdog, $pagerManager, $config);
51
52
    $this->eventController = $eventController;
53
  }
54
55
  /**
56
   * Controller.
57
   *
58
   * @param \Symfony\Component\HttpFoundation\Request $request
59
   *   The current request.
60
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
61
   *   The event template.
62
   *
63
   * @return array<string,mixed>
64
   *   A render array.
65
   */
66
  public function build(Request $request, EventTemplate $eventTemplate): array {
67
    $top = $this->getTop($eventTemplate);
68
69
    $rows = $this->getRowData($request, $eventTemplate);
70
    $main = empty($rows)
71
      ? $this->buildEmpty($this->t('No occurrence of this event found in logger.'))
72
      : $this->buildMainTable($rows, $eventTemplate);
73
74
    $ret = $this->buildDefaults($main, $top);
75
    return $ret;
76
  }
77
78
  /**
79
   * Build the main table.
80
   *
81
   * @param \Drupal\mongodb_watchdog\Event[] $events
82
   *   The event data.
83
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
84
   *   The template for which to built the detail lines.
85
   *
86
   * @return array<string,mixed>
87
   *   A render array for the main table.
88
   */
89
  protected function buildMainTable(array $events, EventTemplate $eventTemplate): array {
90
    $ret = [
91
      '#attributes' => new Attribute(['class' => 'mongodb_watchdog__detail']),
92
      '#caption' => $this->t('Event occurrences'),
93
      '#header' => $this->buildMainTableHeader(),
94
      '#rows' => $this->buildMainTableRows($events, $eventTemplate),
95
      '#type' => 'table',
96
    ];
97
98
    return $ret;
99
  }
100
101
  /**
102
   * Build the main table header.
103
   *
104
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
105
   *   A table header array.
106
   */
107
  protected function buildMainTableHeader(): array {
108
    $header = [
109
      $this->t('Date'),
110
      $this->t('User'),
111
      $this->t('Message'),
112
      $this->t('Location'),
113
      $this->t('Referrer'),
114
      $this->t('Hostname'),
115
      $this->t('Operations'),
116
    ];
117
118
    return $header;
119
  }
120
121
  /**
122
   * Build the main table rows.
123
   *
124
   * @param \Drupal\mongodb_watchdog\Event[] $events
125
   *   The event row data.
126
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
127
   *   The template for these events.
128
   *
129
   * @return array<int,\Drupal\Core\Link[]|\Drupal\Core\StringTranslation\TranslatableMarkup[]|null[]|string[]>
130
   *   A render array for a table.
131
   *
132
   * @throws \Drupal\Core\Entity\EntityMalformedException
133
   */
134
  protected function buildMainTableRows(array $events, EventTemplate $eventTemplate): array {
135
    $rows = [];
136
137
    foreach ($events as $event) {
138
      // @todo bring this back from "model": it is a display method.
139
      $rows[] = $this->eventController->asTableRow($eventTemplate, $event);
140
    }
141
142
    return $rows;
143
  }
144
145
  /**
146
   * Title callback for mongodb_watchdog.detail.
147
   *
148
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
149
   *   The event template for which the title is built.
150
   *
151
   * @return \Drupal\Component\Render\MarkupInterface
152
   *   The page title.
153
   */
154
  public function buildTitle(EventTemplate $eventTemplate): MarkupInterface {
155
    return $this->t('MongoDB events: "@template"', ['@template' => $eventTemplate->message]);
156
  }
157
158
  /**
159
   * {@inheritdoc}
160
   */
161
  public static function create(ContainerInterface $container): self {
162
    /** @var \Psr\Log\LoggerInterface $logger */
163
    $logger = $container->get('logger.channel.mongodb_watchdog');
164
165
    /** @var \Drupal\mongodb_watchdog\Logger $watchdog */
166
    $watchdog = $container->get(Logger::SERVICE_LOGGER);
167
168
    /** @var \Drupal\Core\Config\ImmutableConfig $config */
169
    $config = $container->get('config.factory')->get('mongodb_watchdog.settings');
170
171
    /** @var \Drupal\mongodb_watchdog\EventController $eventController */
172
    $eventController = $container->get('mongodb.watchdog_event_controller');
173
174
    /** @var \Drupal\Core\Pager\PagerManagerInterface $pagerManager */
175
    $pagerManager = $container->get('pager.manager');
176
177
    return new static($logger, $watchdog, $config, $eventController, $pagerManager);
178
  }
179
180
  /**
181
   * Obtain the data from the logger.
182
   *
183
   * @param \Symfony\Component\HttpFoundation\Request $request
184
   *   The current request. Needed for paging.
185
   * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate
186
   *   The template for which to build the detail page.
187
   *
188
   * @return \Drupal\mongodb_watchdog\Event[]
189
   *   The data array.
190
   */
191
  protected function getRowData(Request $request, EventTemplate $eventTemplate): array {
192
    $count = $this->watchdog->eventCount($eventTemplate);
193
    $page = $this->setupPager($request, $count);
194
    $skip = $page * $this->itemsPerPage;
195
    $limit = $this->itemsPerPage;
196
197
    $rows = $this->eventController
198
      ->find($eventTemplate, $skip, $limit)
199
      ->toArray();
200
201
    return $rows;
202
  }
203
204
  /**
205
   * Build the heading rows on the per-template event occurrences page.
206
   *
207
   * @param \Drupal\mongodb_watchdog\EventTemplate|null $eventTemplate
208
   *   The template for which to provide details. Not actually expected to be
209
   *   NULL, but this is needed to remain compatible with parent class.
210
   *
211
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
212
   *   A render array for a table.
213
   */
214
  protected function getTop(EventTemplate $eventTemplate = NULL): array {
215
    $rows = [];
216
    foreach ($eventTemplate->keys() as $key => $info) {
0 ignored issues
show
Bug introduced by
The method keys() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

216
    foreach ($eventTemplate->/** @scrutinizer ignore-call */ keys() as $key => $info) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
217
      $value = $eventTemplate->{$key};
218
      $row = [];
219
      $row[] = [
220
        'header' => TRUE,
221
        'data' => $info['label'],
222
      ];
223
      $row[] = isset($info['display_callback']) ? $info['display_callback']($value) : $value;
224
      $rows[] = $row;
225
    }
226
227
    $ret = [
228
      '#caption' => $this->t('Event template'),
229
      '#rows' => $rows,
230
      '#type' => 'table',
231
    ];
232
233
    return $ret;
234
  }
235
236
}
237