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

DetailController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 207
Duplicated Lines 8.7 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 18
loc 207
rs 10
wmc 12
lcom 2
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B build() 0 40 1
A buildHeader() 0 15 3
A buildRows() 0 12 2
A buildTitle() 0 3 1
A create() 0 15 1
A setupPager() 18 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 extends ControllerBase 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 logger.mongodb_watchdog logger channel, to log events.
29
   *
30
   * @var \Psr\Log\LoggerInterface
31
   */
32
  protected $logger;
33
34
  /**
35
   * The MongoDB logger, to load events.
36
   *
37
   * @var \Drupal\mongodb_watchdog\Logger
38
   */
39
  protected $watchdog;
40
41
  /**
42
   * Controller constructor.
43
   *
44
   * @param \Psr\Log\LoggerInterface $logger
45
   *   The logger service, to log intervening events.
46
   * @param \Drupal\mongodb_watchdog\EventController $event_controller
47
   *   The event controller service.
48
   * @param \Drupal\Core\Config\ImmutableConfig $config
49
   *   The module configuration.
50
   */
51
  public function __construct(
52
    Logger $watchdog,
53
    LoggerInterface $logger,
54
    EventController $event_controller,
55
    ImmutableConfig $config) {
56
    parent::__construct($config);
57
    $this->config = $config;
58
    $this->eventController = $event_controller;
59
    $this->logger = $logger;
60
    $this->watchdog = $watchdog;
61
  }
62
63
  /**
64
   * Controller.
65
   *
66
   * @param \Symfony\Component\HttpFoundation\Request $request
67
   *   The current request.
68
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
69
   *   The event template.
70
   *
71
   * @return array A render 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...
72
   *   A render array.
73
   */
74
  public function build(Request $request, EventTemplate $event_template) {
75
    $page = $this->setupPager($event_template, $request);
76
    $template_rows = $this->buildHeader($event_template);
77
    $event_rows = $this->buildRows($event_template, $page);
78
79
    $base = [
80
      '#attributes' => new Attribute(['class' => 'mongodb_watchdog-detail']),
81
      '#type' => 'table',
82
    ];
83
84
    $event_header = [
85
      t('Date'),
86
      t('User'),
87
      t('Message'),
88
      t('Location'),
89
      t('Referrer'),
90
      t('Hostname'),
91
      t('Operations'),
92
    ];
93
94
    $ret = [
95
      'template' => $base + [
96
        '#caption' => t('Event template'),
97
        '#rows' => $template_rows,
98
      ],
99
      'events' => $base + [
100
        '#caption' => t('Event occurrences'),
101
        '#header' => $event_header,
102
        '#rows' => $event_rows,
103
      ],
104
      'pager' => [
105
        '#type' => 'pager',
106
      ],
107
      '#attached' => [
108
        'library' => ['mongodb_watchdog/styling'],
109
      ],
110
    ];
111
112
    return $ret;
113
  }
114
115
  /**
116
   * Build the heading rows on the event occurrences page.
117
   *
118
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
119
   *   The event template.
120
   *
121
   * @return array
122
   *   A table render array.
123
   */
124
  protected function buildHeader(EventTemplate $template) {
125
    $rows = [];
126
    foreach (EventTemplate::keys() as $key => $info) {
127
      $value = $template->{$key};
128
      $row = [
129
        [
130
          'header' => TRUE,
131
          'data' => $info['label'],
132
        ],
133
        isset($info['display_callback']) ? $info['display_callback']($value) : $value,
134
      ];
135
      $rows[] = $row;
136
    }
137
    return $rows;
138
  }
139
140
  /**
141
   * Build the occurrence rows on the event occurrences page.
142
   *
143
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
144
   *   The event template.
145
   * @param int $page
146
   *   The page number, starting at 0.
147
   *
148
   * @return array
149
   *   A table render array.
150
   */
151
  protected function buildRows(EventTemplate $template, $page) {
152
    $rows = [];
153
    $skip = $page * $this->itemsPerPage;
154
    $limit = $this->itemsPerPage;
155
    $events = $this->eventController->find($template, $skip, $limit);
156
157
    /** @var \Drupal\mongodb_watchdog\Event $event */
158
    foreach ($events as $event) {
159
      $rows[] = $this->eventController->asTableRow($template, $event);
160
    }
161
    return $rows;
162
  }
163
164
  /**
165
   * Title callback for mongodb_watchdog.detail.
166
   *
167
   * @param \Drupal\mongodb_watchdog\EventTemplate $event_template
168
   *   The event template for which the title is built.
169
   *
170
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
171
   *   The page title.
172
   */
173
  public function buildTitle(EventTemplate $event_template) {
174
    return t('MongoDB events: "@template"', ['@template' => $event_template->message]);
175
  }
176
177
  /**
178
   * {@inheritdoc}
179
   */
180
  public static function create(ContainerInterface $container) {
181
    /** @var \Psr\Log\LoggerInterface $logger */
182
    $logger = $container->get('logger.channel.mongodb_watchdog');
183
184
    /** @var \Drupal\mongodb_watchdog\EventController $eventController */
185
    $eventController = $container->get('mongodb.watchdog_event_controller');
186
187
    /** @var \Drupal\mongodb_watchdog\Logger $watchdog */
188
    $watchdog = $container->get('mongodb.logger');
189
190
    /** @var \Drupal\Core\Config\ImmutableConfig $config */
191
    $config = $container->get('config.factory')->get('mongodb_watchdog.settings');
192
193
    return new static($watchdog, $logger, $eventController, $config);
194
  }
195
196
  /**
197
   * Set up the events pager.
198
   *
199
   * @param \Symfony\Component\HttpFoundation\Request $request
200
   *   The current request.
201
   *
202
   * @return int
203
   *   The number of the page to display, starting at 0.
204
   */
205 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...
206
    $count = $this->watchdog->eventCount($template);
207
    $height = $this->itemsPerPage;
208
    pager_default_initialize($count, $height);
209
210
    $page = intval($request->query->get('page'));
211
    if ($page < 0) {
212
      $page = 0;
213
    }
214
    else {
215
      $page_max = intval(min(ceil($count / $height), PHP_INT_MAX) - 1);
216
      if ($page > $page_max) {
217
        $page = $page_max;
218
      }
219
    }
220
221
    return $page;
222
  }
223
224
}
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...
225