Completed
Push — 8.x-2.x ( cba9bd...7c76b2 )
by Frédéric G.
05:20 queued 02:45
created

ClearConfirmForm::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Drupal\mongodb_watchdog\Form;
4
5
use Drupal\Core\Form\FormStateInterface;
6
use Drupal\Core\Url;
7
use Drupal\Core\Form\ConfirmFormBase;
8
use Drupal\mongodb_watchdog\Logger;
9
use MongoDB\Database;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Provides a confirmation form before clearing out the logs.
14
 */
15
class ClearConfirmForm extends ConfirmFormBase {
16
17
  /**
18
   * The logger database.
19
   *
20
   * @var \MongoDB\Database
21
   */
22
  protected $database;
23
24
  /**
25
   * The MongoDB watchdog "logger" service.
26
   *
27
   * @var \Drupal\mongodb_watchdog\Logger
28
   */
29
  protected $logger;
30
31
  /**
32
   * ClearConfirmForm constructor.
33
   *
34
   * @param \MongoDB\Database $database
35
   *   The MongoDB logger database.
36
   */
37
  public function __construct(Database $database, Logger $logger) {
38
    $this->database = $database;
39
    $this->logger = $logger;
40
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  public static function create(ContainerInterface $container) {
46
    return new static(
47
      $container->get('mongodb.watchdog_storage'),
48
      $container->get('mongodb.logger')
49
    );
50
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function getFormId() {
56
    return 'mongodb_watchdog_clear_confirm';
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function getQuestion() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
63
    return $this->t('Are you sure you want to delete the recent logs?');
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69
  public function getCancelUrl() {
70
    return new Url('mongodb_watchdog.reports.overview');
71
  }
72
73
  /**
74
   * {@inheritdoc}
75
   */
76
  public function submitForm(array &$form, FormStateInterface $form_state) {
77
    $_SESSION['mongodb_watchdog_overview_filter'] = [];
78
    $this->database->drop();
79
    $this->logger->ensureIndexes();
80
    drupal_set_message($this->t('Database log cleared.'));
81
    $form_state->setRedirectUrl($this->getCancelUrl());
82
  }
83
84
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
85