Completed
Push — 15-request_grouping ( 2ffca1...83903a )
by Frédéric G.
02:42
created

RequestController::buildTrackRequest()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 20
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 32
rs 8.8571
1
<?php
2
3
namespace Drupal\mongodb_watchdog\Controller;
4
5
use Drupal\Component\Utility\Unicode;
6
use Drupal\Core\Datetime\DateFormatterInterface;
7
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8
use Drupal\Core\Logger\RfcLogLevel;
9
use Drupal\mongodb_watchdog\Logger;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Implements the controller for the request events page.
14
 */
15
class RequestController implements ContainerInjectionInterface {
16
17
  /**
18
   * The core date.formatter service.
19
   *
20
   * @var \Drupal\Core\Datetime\DateFormatterInterface
21
   */
22
  protected $dateFormatter;
23
24
  /**
25
   * The length of the absolute path to the site root, in runes.
26
   *
27
   * @var int
28
   */
29
  protected $rootLength;
30
31
  /**
32
   * The mongodb_watchdog logger, to access events.
33
   *
34
   * @var \Drupal\mongodb_watchdog\Logger
35
   */
36
  protected $watchdog;
37
38
  /**
39
   * RequestController constructor.
40
   *
41
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
42
   *   The MongoDB logger service, to load event data.
43
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
44
   *   The core date.formatter service.
45
   */
46
  public function __construct(Logger $watchdog, DateFormatterInterface $date_formatter) {
47
    $this->dateFormatter = $date_formatter;
48
    $this->watchdog = $watchdog;
49
50
    // Add terminal "/".
51
    $this->rootLength = Unicode::strlen(DRUPAL_ROOT) + 1;
52
  }
53
54
  /**
55
   * Build the top part of the page, about the request.
56
   *
57
   * @param string $unique_id
58
   *   The unique request id.
59
   * @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events
60
   *   A fully loaded array of events and their templates.
61
   *
62
   * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,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...
63
   *   A render array for a table.
64
   */
65
  public function buildTrackRequest($unique_id, array $events) {
66
    if ($events) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $events of type Drupal\mongodb_watchdog\...godb_watchdog\Event[][] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
67
      /** @var \Drupal\mongodb_watchdog\Event $first */
68
      $first = $events[0][1];
69
      $location = $first->location;
70
      $timestamp = $this->dateFormatter->format($first->timestamp, 'long');
71
    }
72
    else {
73
      $location = $timestamp = t('No information');
74
    }
75
76
    $rows = [
77
      [t('Request ID'), $unique_id],
78
      [t('Location'), $location],
79
      [t('Date/time'), $timestamp],
80
    ];
81
82
    foreach ($rows as &$row) {
83
      $row[0] = [
84
        'data' => $row[0],
85
        'header' => TRUE,
86
      ];
87
    }
88
89
    $ret = [
90
      'request' => [
91
        '#type' => 'table',
92
        '#rows' => $rows,
93
      ],
94
    ];
95
    return $ret;
96
  }
97
98
  /**
99
   * Build the bottom part of the page, about the events during the request.
100
   *
101
   * @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events
102
   *   A fully loaded array of events and their templates.
103
   *
104
   * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,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...
105
   *   A render array for a table.
106
   */
107
  public function buildTrackRows(array $events) {
108
    $header = [
109
      t('Sequence'),
110
      t('Type'),
111
      t('Severity'),
112
      t('Event'),
113
      t('File'),
114
      t('Line'),
115
    ];
116
    $rows = [];
117
    $levels = RfcLogLevel::getLevels();
118
119
    /**
120
     * @var \Drupal\mongodb_watchdog\EventTemplate $template
121
     * @var \Drupal\mongodb_watchdog\Event $event
122
     */
123
    foreach ($events as list($template, $event)) {
0 ignored issues
show
introduced by
Variable $template is undefined.
Loading history...
introduced by
Variable $event is undefined.
Loading history...
124
      $row = [
125
        ['data' => $event->requestTracking_sequence],
0 ignored issues
show
introduced by
Variable $event is undefined.
Loading history...
126
        $template->type,
0 ignored issues
show
introduced by
Variable $template is undefined.
Loading history...
127
        $levels[$template->severity],
0 ignored issues
show
introduced by
Variable $template is undefined.
Loading history...
128
        ['data' => $template->asString($event->variables)],
0 ignored issues
show
introduced by
Variable $template is undefined.
Loading history...
introduced by
Variable $event is undefined.
Loading history...
129
        $this->simplifyPath($event->variables['%file']),
0 ignored issues
show
introduced by
Variable $event is undefined.
Loading history...
130
        $event->variables['%line'],
0 ignored issues
show
introduced by
Variable $event is undefined.
Loading history...
131
      ];
132
      $rows[] = $row;
133
    }
134
135
    $ret = [
136
      'events' => [
137
        '#type' => 'table',
138
        '#rows' => $rows,
139
        '#header' => $header,
140
      ],
141
    ];
142
    return $ret;
143
  }
144
145
  /**
146
   * {@inheritdoc}
147
   */
148
  public static function create(ContainerInterface $container) {
149
    /** @var \Drupal\mongodb_watchdog\Logger $watchdog */
150
    $watchdog = $container->get('mongodb.logger');
151
152
    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
153
    $date_formatter = $container->get('date.formatter');
154
155
    return new static($watchdog, $date_formatter);
156
  }
157
158
  /**
159
   * Convert an absolute path to a relative one if below the site root.
160
   *
161
   * @param string $path
162
   *   An absolute path on the filesystem.
163
   *
164
   * @return string
165
   *   A relative path if possible, otherwise the input path.
166
   */
167
  public function simplifyPath($path) {
168
    $ret = (Unicode::strpos($path, DRUPAL_ROOT) === 0)
169
      ? Unicode::substr($path, $this->rootLength)
170
      : $path;
171
    return $ret;
172
  }
173
174
  /**
175
   * Controller.
176
   *
177
   * @param string $unique_id
178
   *   The unique request id from mod_unique_id.
179
   *
180
   * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,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...
181
   *   A render array.
182
   */
183
  public function track($unique_id) {
184
    $events = $this->watchdog->requestEvents($unique_id);
185
    $ret = $this->buildTrackRequest($unique_id, $events)
186
      + $this->buildTrackRows($events);
0 ignored issues
show
introduced by
Expected 1 space before "+"; newline found
Loading history...
187
    return $ret;
188
  }
189
190
}
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...
191