Completed
Pull Request — 8.x-2.x (#24)
by Frédéric G.
02:38
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\mongodb_watchdog\Logger;
8
use Psr\Log\LoggerAwareTrait;
9
use Psr\Log\LoggerInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * Base controller class for paged reports.
14
 */
15
abstract class ControllerBase extends CoreControllerBase {
16
17
  use LoggerAwareTrait;
18
19
  /**
20
   * The items_per_page configuration value.
21
   *
22
   * @var int
23
   */
24
  protected $itemsPerPage;
25
26
  /**
27
   * The MongoDB logger, to load events.
28
   *
29
   * @var \Drupal\mongodb_watchdog\Logger
30
   */
31
  protected $watchdog;
32
33
  /**
34
   * ControllerBase constructor.
35
   *
36
   * @param \Drupal\Core\Config\ImmutableConfig $config
37
   *   The module configuration.
38
   */
39
  public function __construct(LoggerInterface $logger, Logger $watchdog, ImmutableConfig $config) {
40
    $this->setLogger($logger);
41
42
    $this->itemsPerPage = $config->get('items_per_page');
43
    $this->watchdog = $watchdog;
44
  }
45
46
  /**
47
   * The default build() implementation.
48
   *
49
   * Cannot be a build() method because each controller build() has a
50
   * different signature.
51
   *
52
   * @param array $main
53
   *   A render array for the main table.
54
   * @param array|null $top
55
   *   A render array for the top element present on some controllers results.
56
   *
57
   * @return array<string,string|array>
58
   *   A render array for the whole controller.
59
   */
60
  protected function buildDefaults(array $main, array $top = NULL) {
61
    $ret = empty($top) ? [] : ['top' => $top];
62
63
    $ret += [
64
      'main' => $main,
65
      'pager' => ['#type' => 'pager'],
66
      '#attached' => [
67
        'library' => ['mongodb_watchdog/styling'],
68
      ],
69
    ];
70
71
    return $ret;
72
  }
73
74
  /**
75
   * Set up the pager.
76
   *
77
   * @param \Symfony\Component\HttpFoundation\Request $request
78
   *   The current request.
79
   * @param int $count
80
   *   The total number of possible rows.
81
   *
82
   * @return int
83
   *   The number of the page to display, starting at 0.
84
   */
85
  public function setupPager(Request $request, $count) {
86
    $height = $this->itemsPerPage;
87
    pager_default_initialize($count, $height);
88
89
    $page = intval($request->query->get('page'));
90
    if ($page < 0) {
91
      $page = 0;
92
    }
93
    else {
94
      $page_max = intval(min(ceil($count / $height), PHP_INT_MAX) - 1);
95
      if ($page > $page_max) {
96
        $page = $page_max;
97
      }
98
    }
99
100
    return $page;
101
  }
102
103
}
104