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

ControllerBase   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 61
rs 10
wmc 4
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setupPager() 0 17 3
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
   * Set up the pager.
48
   *
49
   * @param \Symfony\Component\HttpFoundation\Request $request
50
   *   The current request.
51
   * @param int $count
52
   *   The total number of possible rows.
53
   *
54
   * @return int
55
   *   The number of the page to display, starting at 0.
56
   */
57
  public function setupPager(Request $request, $count) {
58
    $height = $this->itemsPerPage;
59
    pager_default_initialize($count, $height);
60
61
    $page = intval($request->query->get('page'));
62
    if ($page < 0) {
63
      $page = 0;
64
    }
65
    else {
66
      $page_max = intval(min(ceil($count / $height), PHP_INT_MAX) - 1);
67
      if ($page > $page_max) {
68
        $page = $page_max;
69
      }
70
    }
71
72
    return $page;
73
  }
74
75
}
76