Completed
Push — 2761013-watchdog_pager ( 2d12f8 )
by Frédéric G.
02:46
created

DetailController::detailFooter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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