Completed
Push — watchdog-config ( 593fab )
by Frédéric G.
8s
created

DetailController::detail()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 28
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 42
rs 8.8571
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 Psr\Log\LoggerInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * Class DetailController implements the controller for the event detail page.
15
 */
16
class DetailController implements ContainerInjectionInterface {
17
18
  /**
19
   * The mongodb.watchdog_event_controller service.
20
   *
21
   * @var \Drupal\mongodb_watchdog\EventController
22
   */
23
  protected $eventController;
24
25
  /**
26
   * Constructor.
27
   *
28
   * @param \Psr\Log\LoggerInterface $logger
29
   *   The logger service, to log intervening events.
30
   * @param \Drupal\mongodb_watchdog\EventController $event_controller
31
   *   The event controller service.
32
   * @param \Drupal\Core\Config\ImmutableConfig $config
33
   *   The module configuration.
34
   */
35
  public function __construct(LoggerInterface $logger, EventController $event_controller, ImmutableConfig $config) {
36
    $this->config = $config;
37
    $this->eventController = $event_controller;
38
    $this->logger = $logger;
39
  }
40
41
  /**
42
   * {@inheritdoc}
43
   */
44
  public static function create(ContainerInterface $container) {
45
    /** @var \Psr\Log\LoggerInterface $logger */
46
    $logger = $container->get('logger.channel.mongodb_watchdog');
47
48
    /** @var \Drupal\mongodb_watchdog\EventController $eventController */
49
    $eventController = $container->get('mongodb.watchdog_event_controller');
1 ignored issue
show
introduced by
Variable "eventController" is camel caps format. do not use mixed case (camelCase), use lower case and _
Loading history...
50
51
    /** @var array $config */
52
    $config = $container->get('config.factory')->get('mongodb_watchdog.settings');
53
54
    return new static($logger, $eventController, $config);
1 ignored issue
show
introduced by
Variable "eventController" is camel caps format. do not use mixed case (camelCase), use lower case and _
Loading history...
55
  }
56
57
  /**
58
   * Controller for mongodb_watchdog.detail.
59
   *
60
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
61
   *   The event template.
62
   *
63
   * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
64
   *   A render array.
65
   */
66
  public function detail(EventTemplate $event_template) {
67
    $template_rows = $this->detailHeader($event_template);
68
    $event_rows = $this->detailRows($event_template);
69
70
    $base = [
71
      '#attributes' => new Attribute(['class' => 'mongodb_watchdog-detail']),
72
      '#type' => 'table',
73
    ];
74
75
    $event_header = [
76
      t('Date'),
77
      t('User'),
78
      t('Location'),
79
      t('Referrer'),
80
      t('Hostname'),
81
      t('Message'),
82
      t('Operations'),
83
    ];
84
85
    $ret = [
86
      "#attached" => [
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal #attached does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
87
        'library' => 'mongodb_watchdog/styling',
88
      ],
89
      'template' => $base + [
90
        '#caption' => t('Event template'),
91
        '#rows' => $template_rows,
92
      ],
93
      'events' => $base + [
94
        '#caption' => t('Event occurrences'),
95
        '#header' => $event_header,
96
        '#rows' => $event_rows,
97
      ],
98
    ];
99
100
    if ($footer = $this->detailFooter($event_template, $event_rows)) {
101
      $ret['extra'] = [
102
        '#markup' => $footer,
103
      ];
104
    }
105
106
    return $ret;
107
  }
108
109
  /**
110
   * Report on the extra event occurrences.
111
   *
112
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
113
   *   The event template.
114
   * @param array $event_rows
115
   *   The event rows array.
116
   *
117
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup|null
118
   *   A message about the remaining events not displayed on the page, or NULL
119
   *   if there are no additional messages.
120
   *
121
   * @XXX Maybe replace by a pager later ?
122
   */
123
  protected function detailFooter(EventTemplate $event_template, array $event_rows) {
124
    $display_count = count($event_rows);
125
    $ret = NULL;
126
    if ($display_count >= $this->config->get('items_per_page')) {
127
      $count = $this->eventController->count($event_template);
128
      if ($count > $display_count) {
129
        $ret = t('There are also @count more older occurrences of this event', [
130
          '@count' => $count - $display_count,
131
        ]);
132
      }
133
    }
134
    return $ret;
135
  }
136
137
  /**
138
   * Build the heading rows on the event occurrences page.
139
   *
140
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
141
   *   The event template.
142
   *
143
   * @return array
144
   *   A table render array.
145
   */
146
  protected function detailHeader(EventTemplate $template) {
147
    $rows = [];
148
    foreach (EventTemplate::keys() as $key => $info) {
149
      $value = $template->{$key};
150
      $rows[] = [
151
        [
152
          'header' => TRUE,
153
          'data' => $info['label'],
154
        ],
155
        isset($info['display_callback']) ? $info['display_callback']($value) : $value,
156
      ];
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
   *
167
   * @return array
168
   *   A table render array.
169
   */
170
  protected function detailRows(EventTemplate $template) {
171
    $rows = [];
172
173
    $events = $this->eventController->find($template, NULL, $this->config->get('items_per_page'));
174
    /** @var \Drupal\mongodb_watchdog\Event $event */
175
    foreach ($events as $event) {
176
      $rows[] = $this->eventController->asTableRow($template, $event);
177
    }
178
    return $rows;
179
  }
180
181
  /**
182
   * Title callback for mongodb_watchdog.detail.
183
   *
184
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
185
   *   The event template for which the title is built.
186
   *
187
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
188
   *   The page title.
189
   */
190
  public function detailTitle(EventTemplate $event_template) {
191
    return t('MongoDB events: "@template"', ['@template' => $event_template->message]);
192
  }
193
194
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
195