Completed
Pull Request — 8.x-2.x (#24)
by Frédéric G.
03:04
created

DetailController::build()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 29
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 41
rs 8.8571
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
 * Class DetailController implements 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 $event_controller
36
   *   The event controller service.
37
   */
38
  public function __construct(
39
    LoggerInterface $logger,
40
    Logger $watchdog,
41
    ImmutableConfig $config,
42
    EventController $event_controller) {
43
    parent::__construct($logger, $watchdog, $config);
44
45
    $this->eventController = $event_controller;
46
  }
47
48
  /**
49
   * Controller.
50
   *
51
   * @param \Symfony\Component\HttpFoundation\Request $request
52
   *   The current request.
53
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
54
   *   The event template.
55
   *
56
   * @return array<string,array>
57
   *   A render array.
58
   */
59
  public function build(Request $request, EventTemplate $event_template) {
60
    $count = $this->watchdog->eventCount($event_template);
61
    $page = $this->setupPager($request, $count);
62
    $template_rows = $this->buildHeader($event_template);
63
    $event_rows = $this->buildRows($event_template, $page);
64
65
    $base = [
66
      '#attributes' => new Attribute(['class' => 'mongodb_watchdog-detail']),
67
      '#type' => 'table',
68
    ];
69
70
    $event_header = [
71
      t('Date'),
72
      t('User'),
73
      t('Message'),
74
      t('Location'),
75
      t('Referrer'),
76
      t('Hostname'),
77
      t('Operations'),
78
    ];
79
80
    $ret = [
81
      'template' => $base + [
82
        '#caption' => t('Event template'),
83
        '#rows' => $template_rows,
84
      ],
85
      'events' => $base + [
86
        '#caption' => t('Event occurrences'),
87
        '#header' => $event_header,
88
        '#rows' => $event_rows,
89
      ],
90
      'pager' => [
91
        '#type' => 'pager',
92
      ],
93
      '#attached' => [
94
        'library' => ['mongodb_watchdog/styling'],
95
      ],
96
    ];
97
98
    return $ret;
99
  }
100
101
  /**
102
   * Build the heading rows on the event occurrences page.
103
   *
104
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
105
   *   The event template.
106
   *
107
   * @return array
108
   *   A table render array.
109
   */
110
  protected function buildHeader(EventTemplate $template) {
111
    $rows = [];
112
    foreach (EventTemplate::keys() as $key => $info) {
113
      $value = $template->{$key};
114
      $row = [
115
        [
116
          'header' => TRUE,
117
          'data' => $info['label'],
118
        ],
119
        isset($info['display_callback']) ? $info['display_callback']($value) : $value,
120
      ];
121
      $rows[] = $row;
122
    }
123
    return $rows;
124
  }
125
126
  /**
127
   * Build the occurrence rows on the event occurrences page.
128
   *
129
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
130
   *   The event template.
131
   * @param int $page
132
   *   The page number, starting at 0.
133
   *
134
   * @return array
135
   *   A table render array.
136
   */
137
  protected function buildRows(EventTemplate $template, $page) {
138
    $rows = [];
139
    $skip = $page * $this->itemsPerPage;
140
    $limit = $this->itemsPerPage;
141
    $events = $this->eventController->find($template, $skip, $limit);
142
143
    /** @var \Drupal\mongodb_watchdog\Event $event */
144
    foreach ($events as $event) {
145
      $rows[] = $this->eventController->asTableRow($template, $event);
146
    }
147
    return $rows;
148
  }
149
150
  /**
151
   * Title callback for mongodb_watchdog.detail.
152
   *
153
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
154
   *   The event template for which the title is built.
155
   *
156
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
157
   *   The page title.
158
   */
159
  public function buildTitle(EventTemplate $event_template) {
160
    return t('MongoDB events: "@template"', ['@template' => $event_template->message]);
161
  }
162
163
  /**
164
   * {@inheritdoc}
165
   */
166 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...
167
    /** @var \Psr\Log\LoggerInterface $logger */
168
    $logger = $container->get('logger.channel.mongodb_watchdog');
169
170
    /** @var \Drupal\mongodb_watchdog\Logger $watchdog */
171
    $watchdog = $container->get('mongodb.logger');
172
173
    /** @var \Drupal\Core\Config\ImmutableConfig $config */
174
    $config = $container->get('config.factory')->get('mongodb_watchdog.settings');
175
176
    /** @var \Drupal\mongodb_watchdog\EventController $eventController */
177
    $eventController = $container->get('mongodb.watchdog_event_controller');
178
179
    return new static($logger, $watchdog, $config, $eventController);
180
  }
181
182
}
183