Passed
Pull Request — 8.x-2.x (#71)
by Frédéric G.
05:37
created

ControllerBase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 154
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getTop() 0 3 1
A setupPager() 0 8 1
A buildEmpty() 0 8 1
A buildDefaults() 0 12 1
A getPage() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb_watchdog\Controller;
6
7
use Drupal\Core\Config\ImmutableConfig;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Config\ImmutableConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\Core\Controller\ControllerBase as CoreControllerBase;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Controller\ControllerBase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Drupal\Core\Pager\PagerManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Pager\PagerManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Drupal\Core\StringTranslation\TranslatableMarkup;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\StringTranslation\TranslatableMarkup was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Drupal\mongodb_watchdog\Logger;
12
use Psr\Log\LoggerAwareTrait;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerAwareTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * Base controller class for paged reports.
18
 */
19
abstract class ControllerBase extends CoreControllerBase {
20
21
  use LoggerAwareTrait;
22
23
  /**
24
   * The items_per_page configuration value.
25
   *
26
   * @var int
27
   */
28
  protected $itemsPerPage;
29
30
  /**
31
   * The pager.manager service.
32
   *
33
   * @var \Drupal\Core\Pager\PagerManagerInterface
34
   */
35
  protected $pagerManager;
36
37
  /**
38
   * The MongoDB logger, to load events.
39
   *
40
   * @var \Drupal\mongodb_watchdog\Logger
41
   */
42
  protected $watchdog;
43
44
  /**
45
   * ControllerBase constructor.
46
   *
47
   * @param \Psr\Log\LoggerInterface $logger
48
   *   The logger.channel.mongodb_watchdog service.
49
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
50
   *   The mongodb.logger service, to load stored events.
51
   * @param \Drupal\Core\Pager\PagerManagerInterface $pagerManager
52
   *   The core pager.manager service.
53
   * @param \Drupal\Core\Config\ImmutableConfig $config
54
   *   The mongodb_watchdog configuration.
55
   */
56
  public function __construct(
57
    LoggerInterface $logger,
58
    Logger $watchdog,
59
    PagerManagerInterface $pagerManager,
60
    ImmutableConfig $config) {
61
    $this->setLogger($logger);
62
63
    $this->itemsPerPage = $config->get('items_per_page');
64
    $this->pagerManager = $pagerManager;
65
    $this->watchdog = $watchdog;
66
  }
67
68
  /**
69
   * The default build() implementation.
70
   *
71
   * Cannot be a build() method because each controller build() has a
72
   * different signature.
73
   *
74
   * @param array $main
75
   *   A render array for the main table.
76
   * @param array $top
77
   *   A render array for the top element present on some controllers results.
78
   *
79
   * @return array
80
   *   A render array for the whole controller.
81
   */
82
  protected function buildDefaults(array $main, array $top): array {
83
    $ret = ['top' => $top];
84
85
    $ret += [
86
      'main' => $main,
87
      'pager' => ['#type' => 'pager'],
88
      '#attached' => [
89
        'library' => ['mongodb_watchdog/styling'],
90
      ],
91
    ];
92
93
    return $ret;
94
  }
95
96
  /**
97
   * Build markup for a message about the lack of results.
98
   *
99
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $markup
100
   *   The message proper.
101
   *
102
   * @return array
103
   *   A render array for a message.
104
   */
105
  protected function buildEmpty(TranslatableMarkup $markup): array {
106
    $ret = [
107
      '#markup' => $markup,
108
      '#prefix' => '<div class="mongodb-watchdog__message">',
109
      '#suffix' => '</div>',
110
    ];
111
112
    return $ret;
113
  }
114
115
  /**
116
   * Return the top element: empty by default.
117
   *
118
   * @return array
119
   *   A render array for the top filter form.
120
   */
121
  protected function getTop(): array {
122
    $top = [];
123
    return $top;
124
  }
125
126
  /**
127
   * Set up the pager.
128
   *
129
   * @param \Symfony\Component\HttpFoundation\Request $request
130
   *   The current request.
131
   * @param int $count
132
   *   The total number of possible rows.
133
   *
134
   * @return int
135
   *   The number of the page to display, starting at 0.
136
   */
137
  public function setupPager(Request $request, int $count): int {
138
    $height = $this->itemsPerPage;
139
    $this->pagerManager->createPager($count, $height);
140
141
    $requestedPage = intval($request->query->get('page', 0));
142
    $page = $this->getPage($count, $requestedPage, $height);
143
144
    return $page;
145
  }
146
147
  /**
148
   * Return a reliable page number based on available data.
149
   *
150
   * @param int $count
151
   *   The number of events templates in the collection.
152
   * @param int $requestedPage
153
   *   The page number requested by the user, starting at 0.
154
   * @param int $height
155
   *   The pager height.
156
   *
157
   * @return int
158
   *   The actual index of the page to display.
159
   */
160
  public static function getPage(int $count, int $requestedPage, int $height): int {
161
    if ($requestedPage <= 0) {
162
      return 0;
163
    }
164
165
    // There is always at least one page, even with $count === 0.
166
    $pageCount = max(1, intval(ceil($count / $height)));
167
    if ($requestedPage < $pageCount) {
168
      return $requestedPage;
169
    }
170
171
    $page = $pageCount - 1;
172
    return $page;
173
  }
174
175
}
176