Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
90 / 90
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
VisitorsOnlineBlock
100.00% covered (success)
100.00%
90 / 90
100.00% covered (success)
100.00%
7 / 7
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 defaultConfiguration
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 blockForm
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
1
 blockSubmit
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 build
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
7
 getCacheMaxAge
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\visitors\Plugin\Block;
6
7use Drupal\Core\Block\BlockBase;
8use Drupal\Core\Form\FormStateInterface;
9use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10use Drupal\visitors\VisitorsOnlineInterface;
11use Symfony\Component\DependencyInjection\ContainerInterface;
12
13/**
14 * Provides a 'Visitors Online' block.
15 *
16 * @Block(
17 *   id = "visitors_online",
18 *   admin_label = @Translation("Visitors Online")
19 * )
20 */
21final class VisitorsOnlineBlock extends BlockBase implements ContainerFactoryPluginInterface {
22
23  /**
24   * Five minutes in seconds.
25   */
26  const MINUTE_5 = 300;
27
28  /**
29   * The visitors online service.
30   *
31   * @var \Drupal\visitors\VisitorsOnlineInterface
32   */
33  protected $online;
34
35  /**
36   * Constructs a StatisticsPopularBlock object.
37   *
38   * @param array $configuration
39   *   A configuration array containing information about the plugin instance.
40   * @param string $plugin_id
41   *   The plugin_id for the plugin instance.
42   * @param mixed $plugin_definition
43   *   The plugin implementation definition.
44   * @param \Drupal\visitors\VisitorsOnlineInterface $visitors_online
45   *   The visitors online service.
46   */
47  public function __construct(array $configuration, $plugin_id, $plugin_definition, VisitorsOnlineInterface $visitors_online) {
48    parent::__construct($configuration, $plugin_id, $plugin_definition);
49
50    $this->online = $visitors_online;
51  }
52
53  /**
54   * {@inheritdoc}
55   */
56  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57    return new self(
58      $configuration,
59      $plugin_id,
60      $plugin_definition,
61      $container->get('visitors.online'),
62    );
63  }
64
65  /**
66   * {@inheritdoc}
67   */
68  public function defaultConfiguration() {
69    return [
70      'now_30_minute' => TRUE,
71      'now_24_hour' => FALSE,
72      'yesterday_30_minute' => FALSE,
73      'yesterday_24_hour' => FALSE,
74      'last_week_30_minute' => FALSE,
75      'last_week_24_hour' => FALSE,
76    ];
77  }
78
79  /**
80   * {@inheritdoc}
81   */
82  public function blockForm($form, FormStateInterface $form_state) {
83
84    $form['now_30_minute'] = [
85      '#type' => 'checkbox',
86      '#title' => $this->t('Active visitors (last 30 min)'),
87      '#description' => $this->t('Show the number of visitors in the last 30 minutes.'),
88      '#default_value' => $this->configuration['now_30_minute'],
89    ];
90    $form['now_24_hour'] = [
91      '#type' => 'checkbox',
92      '#title' => $this->t('Visitors in the past 24 hours'),
93      '#description' => $this->t('Show the number of visitors in the past 24 hours.'),
94      '#default_value' => $this->configuration['now_24_hour'],
95    ];
96    $form['yesterday_30_minute'] = [
97      '#type' => 'checkbox',
98      '#title' => $this->t('Visitors yesterday (same 30 min)'),
99      '#description' => $this->t('Show the number of visitors in the same 30-minute period yesterday.'),
100      '#default_value' => $this->configuration['yesterday_30_minute'],
101    ];
102    $form['yesterday_24_hour'] = [
103      '#type' => 'checkbox',
104      '#title' => $this->t('Visitors yesterday (total)'),
105      '#description' => $this->t('Show the total number of visitors yesterday.'),
106      '#default_value' => $this->configuration['yesterday_24_hour'],
107    ];
108    $form['last_week_30_minute'] = [
109      '#type' => 'checkbox',
110      '#title' => $this->t('Visitors last week (same 30 min)'),
111      '#description' => $this->t('Show the number of visitors in the same 30-minute period last week.'),
112      '#default_value' => $this->configuration['last_week_30_minute'],
113    ];
114    $form['last_week_24_hour'] = [
115      '#type' => 'checkbox',
116      '#title' => $this->t('Visitors last week (same day)'),
117      '#description' => $this->t('Show the total number of visitors on the same day last week.'),
118      '#default_value' => $this->configuration['last_week_24_hour'],
119    ];
120
121    return $form;
122  }
123
124  /**
125   * {@inheritdoc}
126   */
127  public function blockSubmit($form, FormStateInterface $form_state) {
128    $this->configuration['now_30_minute'] = $form_state->getValue('now_30_minute');
129    $this->configuration['now_24_hour'] = $form_state->getValue('now_24_hour');
130    $this->configuration['yesterday_30_minute'] = $form_state->getValue('yesterday_30_minute');
131    $this->configuration['yesterday_24_hour'] = $form_state->getValue('yesterday_24_hour');
132    $this->configuration['last_week_30_minute'] = $form_state->getValue('last_week_30_minute');
133    $this->configuration['last_week_24_hour'] = $form_state->getValue('last_week_24_hour');
134  }
135
136  /**
137   * {@inheritdoc}
138   */
139  public function build() {
140
141    $items = [];
142    if ($this->configuration['now_30_minute']) {
143      $items[] = $this->t('Last 30 min: @count', [
144        '@count' => $this->online->getLast30Minutes(),
145      ]);
146    }
147    if ($this->configuration['now_24_hour']) {
148      $items[] = $this->t('Last 24 hours: @count', [
149        '@count' => $this->online->getLast24Hours(),
150      ]);
151    }
152    if ($this->configuration['yesterday_30_minute']) {
153      $items[] = $this->t('Yesterday (same 30 min): @count', [
154        '@count' => $this->online->getYesterday30Minutes(),
155      ]);
156    }
157    if ($this->configuration['yesterday_24_hour']) {
158      $items[] = $this->t('Yesterday (total): @count', [
159        '@count' => $this->online->getYesterday24Hours(),
160      ]);
161    }
162    if ($this->configuration['last_week_30_minute']) {
163      $items[] = $this->t('Last week (same 30 min): @count', [
164        '@count' => $this->online->getLastWeek30Minutes(),
165      ]);
166    }
167    if ($this->configuration['last_week_24_hour']) {
168      $items[] = $this->t('Last week (same day): @count', [
169        '@count' => $this->online->getLastWeek24Hours(),
170      ]);
171    }
172
173    $content = [
174      '#theme' => 'item_list',
175      '#items' => $items,
176    ];
177
178    return $content;
179  }
180
181  /**
182   * {@inheritdoc}
183   */
184  public function getCacheMaxAge() {
185    return self::MINUTE_5;
186  }
187
188}