Passed
Push — 8.x-2.x ( 9af07d...3011f1 )
by Frédéric G.
05:35
created

ConfigForm::buildForm()   A

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.
20
   *
21
   * @var array
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 $typed
31
   *   The type config for the module.
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
  public function buildForm(array $form, FormStateInterface $formState): array {
54
    $config = $this->config(Logger::CONFIG_NAME);
55
    foreach ($config->getRawData() as $key => $default) {
56
      if (mb_substr($key, 0, 1) === '_') {
57
        continue;
58
      }
59
      $schema = $this->typed['mapping'][$key];
60
      list($title, $description) = explode(': ', $schema['label']);
61
      $form[$key] = [
62
        '#default_value' => $default,
63
        '#description' => $description,
64
        '#title' => $title,
65
      ];
66
67
      switch ($schema['type']) {
68
        case 'integer':
69
          $form[$key] += [
70
            '#max' => $schema['max'] ?? PHP_INT_MAX,
71
            '#min' => $schema['min'] ?? 0,
72
            '#type' => 'number',
73
          ];
74
          break;
75
76
        case 'boolean':
77
          $form[$key] += [
78
            '#type' => 'checkbox',
79
          ];
80
          break;
81
82
        default:
83
          break;
84
      }
85
    }
86
87
    $parentedForm = parent::buildForm($form, $formState);
88
    return $parentedForm;
89
  }
90
91
  /**
92
   * {@inheritdoc}
93
   */
94
  public function submitForm(array &$form, FormStateInterface $formState): void {
95
    $config = $this->config(Logger::CONFIG_NAME);
96
    foreach (array_keys($config->getRawData()) as $key) {
97
      $config->set($key, intval($formState->getValue($key)));
98
    }
99
    $config->save();
100
    $this->messenger()->addMessage($this->t('The configuration options have been saved.'));
101
  }
102
103
  /**
104
   * Gets the configuration names that will be editable.
105
   *
106
   * @return string[]
107
   *   An array of configuration object names that are editable if called in
108
   *   conjunction with the trait's config() method.
109
   */
110
  protected function getEditableConfigNames(): array {
111
    return ['mongodb_watchdog.settings'];
112
  }
113
114
  /**
115
   * Returns a unique string identifying the form.
116
   *
117
   * @return string
118
   *   The unique string identifying the form.
119
   */
120
  public function getFormId(): string {
121
    return 'mongodb_watchdog_config';
122
  }
123
124
}
125