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

ControllerBase::buildDefaults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 13
rs 9.4285
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 array<string,string|array>
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 array<string,string|\Drupal\Core\StringTranslation\TranslatableMarkup>
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
    $page = intval($request->query->get('page'));
121
    if ($page < 0) {
122
      $page = 0;
123
    }
124
    else {
125
      $page_max = intval(min(ceil($count / $height), PHP_INT_MAX) - 1);
126
      if ($page > $page_max) {
127
        $page = $page_max;
128
      }
129
    }
130
131
    return $page;
132
  }
133
134
}
135