ConfigForm::buildForm()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 5
eloc 25
c 2
b 0
f 2
nc 5
nop 2
dl 0
loc 36
rs 9.2088
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb_watchdog\Form;
6
7
use Drupal\Core\Config\ConfigFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Config\ConfigFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\Core\Form\ConfigFormBase;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Form\ConfigFormBase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Drupal\Core\Form\FormStateInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Form\FormStateInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Drupal\mongodb_watchdog\Logger;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * Class ConfigForm provides configuration for the MongoDB watchdog module.
15
 */
16
class ConfigForm extends ConfigFormBase {
17
18
  /**
19
   * Typed schema for the configuration: a plugin definition array.
20
   *
21
   * @var array<string,mixed>
22
   */
23
  protected $typed;
24
25
  /**
26
   * ConfigForm constructor.
27
   *
28
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
29
   *   The core config.factory service.
30
   * @param array<string,mixed> $typed
31
   *   The type config for the module: a plugin definition array.
32
   */
33
  public function __construct(ConfigFactoryInterface $configFactory, array $typed) {
34
    parent::__construct($configFactory);
35
    $this->typed = $typed;
36
  }
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public static function create(ContainerInterface $container): self {
42
    return new static(
43
      $container->get('config.factory'),
44
      $container
45
        ->get('config.typed')
46
        ->getDefinition('mongodb_watchdog.settings')
47
    );
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   *
53
   * @param array<string,mixed> $form
54
   *   The existing form array.
55
   * @param \Drupal\Core\Form\FormStateInterface $formState
56
   *   The form state.
57
   *
58
   * @return array<string,mixed>
59
   *   The extended form.
60
   */
61
  public function buildForm(array $form, FormStateInterface $formState): array {
62
    $config = $this->config(Logger::CONFIG_NAME);
63
    foreach ($config->getRawData() as $key => $default) {
64
      if (mb_substr($key, 0, 1) === '_') {
65
        continue;
66
      }
67
      $schema = $this->typed['mapping'][$key];
68
      [$title, $description] = explode(': ', $schema['label']);
69
      $form[$key] = [
70
        '#default_value' => $default,
71
        '#description' => $description,
72
        '#title' => $title,
73
      ];
74
75
      switch ($schema['type']) {
76
        case 'integer':
77
          $form[$key] += [
78
            '#max' => $schema['max'] ?? PHP_INT_MAX,
79
            '#min' => $schema['min'] ?? 0,
80
            '#type' => 'number',
81
          ];
82
          break;
83
84
        case 'boolean':
85
          $form[$key] += [
86
            '#type' => 'checkbox',
87
          ];
88
          break;
89
90
        default:
91
          break;
92
      }
93
    }
94
95
    $parentedForm = parent::buildForm($form, $formState);
96
    return $parentedForm;
97
  }
98
99
  /**
100
   * {@inheritdoc}
101
   *
102
   * @param array<string,mixed> $form
103
   *   The submitted form array.
104
   * @param \Drupal\Core\Form\FormStateInterface $formState
105
   *   The form state.
106
   */
107
  public function submitForm(array &$form, FormStateInterface $formState): void {
108
    $config = $this->config(Logger::CONFIG_NAME);
109
    foreach (array_keys($config->getRawData()) as $key) {
110
      $config->set($key, intval($formState->getValue($key)));
111
    }
112
    $config->save();
113
    $this->messenger()->addMessage($this->t('The configuration options have been saved.'));
114
  }
115
116
  /**
117
   * Gets the configuration names that will be editable.
118
   *
119
   * @return string[]
120
   *   An array of configuration object names that are editable if called in
121
   *   conjunction with the trait's config() method.
122
   */
123
  protected function getEditableConfigNames(): array {
124
    return ['mongodb_watchdog.settings'];
125
  }
126
127
  /**
128
   * Returns a unique string identifying the form.
129
   *
130
   * @return string
131
   *   The unique string identifying the form.
132
   */
133
  public function getFormId(): string {
134
    return 'mongodb_watchdog_config';
135
  }
136
137
}
138