Completed
Push — 8.x-2.x ( 29896f...edd842 )
by Frédéric G.
01:56
created

ControllerBase::getPage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 3
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\mongodb_watchdog\Controller;
4
5
use Drupal\Core\Config\ImmutableConfig;
6
use Drupal\Core\Controller\ControllerBase as CoreControllerBase;
7
use Drupal\Core\StringTranslation\TranslatableMarkup;
8
use Drupal\mongodb_watchdog\Logger;
9
use Psr\Log\LoggerAwareTrait;
10
use Psr\Log\LoggerInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * Base controller class for paged reports.
15
 */
16
abstract class ControllerBase extends CoreControllerBase {
17
18
  use LoggerAwareTrait;
19
20
  /**
21
   * The items_per_page configuration value.
22
   *
23
   * @var int
24
   */
25
  protected $itemsPerPage;
26
27
  /**
28
   * The MongoDB logger, to load events.
29
   *
30
   * @var \Drupal\mongodb_watchdog\Logger
31
   */
32
  protected $watchdog;
33
34
  /**
35
   * ControllerBase constructor.
36
   *
37
   * @param \Drupal\Core\Config\ImmutableConfig $config
38
   *   The module configuration.
39
   */
40
  public function __construct(LoggerInterface $logger, Logger $watchdog, ImmutableConfig $config) {
41
    $this->setLogger($logger);
42
43
    $this->itemsPerPage = $config->get('items_per_page');
44
    $this->watchdog = $watchdog;
45
  }
46
47
  /**
48
   * The default build() implementation.
49
   *
50
   * Cannot be a build() method because each controller build() has a
51
   * different signature.
52
   *
53
   * @param array $main
54
   *   A render array for the main table.
55
   * @param array|null $top
56
   *   A render array for the top element present on some controllers results.
57
   *
58
   * @return string[string|array]
0 ignored issues
show
Documentation introduced by
The doc-type string[string|array] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
59
   *   A render array for the whole controller.
60
   */
61
  protected function buildDefaults(array $main, array $top = NULL) {
62
    $ret = empty($top) ? [] : ['top' => $top];
63
64
    $ret += [
65
      'main' => $main,
66
      'pager' => ['#type' => 'pager'],
67
      '#attached' => [
68
        'library' => ['mongodb_watchdog/styling'],
69
      ],
70
    ];
71
72
    return $ret;
73
  }
74
75
  /**
76
   * Build markup for a message about the lack of results.
77
   *
78
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $markup
79
   *   The message proper.
80
   *
81
   * @return string[string|\Drupal\Core\StringTranslation\TranslatableMarkup]
0 ignored issues
show
Documentation introduced by
The doc-type string[string|\Drupal\Co...ion\TranslatableMarkup] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
82
   *   A render array for a message.
83
   */
84
  protected function buildEmpty(TranslatableMarkup $markup) {
85
    $ret = [
86
      '#markup' => $markup,
87
      '#prefix' => '<div class="mongodb-watchdog__message">',
88
      '#suffix' => '</div>',
89
    ];
90
91
    return $ret;
92
  }
93
94
  /**
95
   * Return the top element: empty by default.
96
   *
97
   * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
98
   *   A render array for the top filter form.
99
   */
100
  protected function getTop() {
101
    $top = NULL;
102
    return $top;
103
  }
104
105
  /**
106
   * Set up the pager.
107
   *
108
   * @param \Symfony\Component\HttpFoundation\Request $request
109
   *   The current request.
110
   * @param int $count
111
   *   The total number of possible rows.
112
   *
113
   * @return int
114
   *   The number of the page to display, starting at 0.
115
   */
116
  public function setupPager(Request $request, $count) {
117
    $height = $this->itemsPerPage;
118
    pager_default_initialize($count, $height);
119
120
    $requestedPage = intval($request->query->get('page', 0));
121
    $page = $this->getPage($count, $requestedPage, $height);
122
123
    return $page;
124
  }
125
126
  /**
127
   * Return a reliable page number based on available data.
128
   *
129
   * @param int $count
130
   *   The number of events templates in the collection.
131
   * @param int $requestedPage
132
   *   The page number requested by the user, starting at 0.
133
   * @param int $height
134
   *   The pager height.
135
   *
136
   * @return int
137
   *   The actual index of the page to display.
138
   */
139
  public static function getPage(int $count, int $requestedPage, int $height): int {
0 ignored issues
show
introduced by
Unknown type hint "int" found for $count
Loading history...
introduced by
Unknown type hint "int" found for $requestedPage
Loading history...
introduced by
Unknown type hint "int" found for $height
Loading history...
introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
140
    if ($requestedPage <= 0) {
141
      return 0;
142
    }
143
144
    // There is always at least one page, even with $count === 0.
145
    $pageCount = max(1, intval(ceil($count / $height)));
146
    if ($requestedPage < $pageCount) {
147
      return $requestedPage;
148
    }
149
150
    $page = $pageCount - 1;
151
    return $page;
152
  }
153
154
}
155