Completed
Push — 2761013-watchdog_pager ( d635f7...2f2a35 )
by Frédéric G.
03:11
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\Template\Attribute;
8
use Drupal\mongodb_watchdog\EventController;
9
use Drupal\mongodb_watchdog\EventTemplate;
10
use Drupal\mongodb_watchdog\Logger;
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Class DetailController implements the controller for the event detail page.
17
 */
18
class DetailController implements ContainerInjectionInterface {
19
20
  /**
21
   * The mongodb.watchdog_event_controller service.
22
   *
23
   * @var \Drupal\mongodb_watchdog\EventController
24
   */
25
  protected $eventController;
26
27
  /**
28
   * The items_per_page configuration value.
29
   *
30
   * @var int
31
   */
32
  protected $itemsPerPage;
33
34
  /**
35
   * The logger.mongodb_watchdog logger channel, to log events.
36
   *
37
   * @var \Psr\Log\LoggerInterface
38
   */
39
  protected $logger;
40
41
  /**
42
   * The MongoDB logger service, to load events.
43
   *
44
   * @var \Drupal\mongodb_watchdog\Logger
45
   */
46
  protected $watchdog;
47
48
  /**
49
   * Constructor.
50
   *
51
   * @param \Psr\Log\LoggerInterface $logger
52
   *   The logger service, to log intervening events.
53
   * @param \Drupal\mongodb_watchdog\EventController $event_controller
54
   *   The event controller service.
55
   * @param \Drupal\Core\Config\ImmutableConfig $config
56
   *   The module configuration.
57
   */
58
  public function __construct(Logger $watchdog, LoggerInterface $logger, EventController $event_controller, ImmutableConfig $config) {
59
    $this->config = $config;
60
    $this->eventController = $event_controller;
61
    $this->logger = $logger;
62
    $this->watchdog = $watchdog;
63
64
    $this->itemsPerPage = $config->get('items_per_page');
65
  }
66
67
  /**
68
   * {@inheritdoc}
69
   */
70 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...
71
    /** @var \Psr\Log\LoggerInterface $logger */
72
    $logger = $container->get('logger.channel.mongodb_watchdog');
73
74
    /** @var \Drupal\mongodb_watchdog\EventController $eventController */
75
    $eventController = $container->get('mongodb.watchdog_event_controller');
76
77
    /** @var \Drupal\mongodb_watchdog\Logger $watchdog */
78
    $watchdog = $container->get('mongodb.logger');
79
80
    /** @var array $config */
81
    $config = $container->get('config.factory')->get('mongodb_watchdog.settings');
82
83
    return new static($watchdog, $logger, $eventController, $config);
84
  }
85
86
  /**
87
   * Controller for mongodb_watchdog.detail.
88
   *
89
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
90
   *   The event template.
91
   *
92
   * @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...
93
   *   A render array.
94
   */
95
  public function detail(EventTemplate $event_template, Request $request) {
96
    $page = $this->setupPager($event_template, $request);
97
    $template_rows = $this->detailHeader($event_template);
98
    $event_rows = $this->detailRows($event_template, $page);
99
100
    $base = [
101
      '#attributes' => new Attribute(['class' => 'mongodb_watchdog-detail']),
102
      '#type' => 'table',
103
    ];
104
105
    $event_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
    $ret = [
116
      "#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...
117
        'library' => 'mongodb_watchdog/styling',
118
      ],
119
      'template' => $base + [
120
        '#caption' => t('Event template'),
121
        '#rows' => $template_rows,
122
      ],
123
      'events' => $base + [
124
        '#caption' => t('Event occurrences'),
125
        '#header' => $event_header,
126
        '#rows' => $event_rows,
127
      ],
128
      'pager' => [
129
        '#type' => 'pager',
130
      ],
131
    ];
132
133
    return $ret;
134
  }
135
136
  /**
137
   * Build the heading rows on the event occurrences page.
138
   *
139
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
140
   *   The event template.
141
   *
142
   * @return array
143
   *   A table render array.
144
   */
145
  protected function detailHeader(EventTemplate $template) {
146
    $rows = [];
147
    foreach (EventTemplate::keys() as $key => $info) {
148
      $value = $template->{$key};
149
      $row = [
150
        [
151
          'header' => TRUE,
152
          'data' => $info['label'],
153
        ],
154
        isset($info['display_callback']) ? $info['display_callback']($value) : $value,
155
      ];
156
      $rows[] = $row;
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
   * @param int $page
167
   *   The page number, starting at 0.
168
   *
169
   * @return array
170
   *   A table render array.
171
   */
172
  protected function detailRows(EventTemplate $template, $page) {
173
    $rows = [];
174
    $skip = $page * $this->itemsPerPage;
175
    $limit = $this->itemsPerPage;
176
    $events = $this->eventController->find($template, $skip, $limit);
177
178
    /** @var \Drupal\mongodb_watchdog\Event $event */
179
    foreach ($events as $event) {
180
      $rows[] = $this->eventController->asTableRow($template, $event);
181
    }
182
    return $rows;
183
  }
184
185
  /**
186
   * Title callback for mongodb_watchdog.detail.
187
   *
188
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
189
   *   The event template for which the title is built.
190
   *
191
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
192
   *   The page title.
193
   */
194
  public function detailTitle(EventTemplate $event_template) {
195
    return t('MongoDB events: "@template"', ['@template' => $event_template->message]);
196
  }
197
198
  /**
199
   * Set up the events pager.
200
   *
201
   * @param \Symfony\Component\HttpFoundation\Request $request
202
   *   The current request.
203
   *
204
   * @return int
205
   *   The number of the page to display, starting at 0.
206
   */
207 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...
208
    $count = $this->watchdog->eventCount($template);
209
    $height = $this->itemsPerPage;
210
    pager_default_initialize($count, $height);
211
212
    $page = intval($request->query->get('page'));
213
    if ($page < 0) {
214
      $page = 0;
215
    }
216
    else {
217
      $page_max = intval(min(ceil($count / $height), PHP_INT_MAX) - 1);
218
      if ($page > $page_max) {
219
        $page = $page_max;
220
      }
221
    }
222
223
    return $page;
224
  }
225
226
}
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...
227