EventTemplate::bsonUnserialize()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 7
c 1
b 0
f 1
nc 6
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb_watchdog;
6
7
use Drupal\Core\Logger\RfcLogLevel;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Logger\RfcLogLevel 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\StringTranslation\StringTranslationTrait;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\StringTransl...\StringTranslationTrait 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\StringTranslation\TranslatableMarkup;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\StringTranslation\TranslatableMarkup 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 MongoDB\BSON\Unserializable;
11
12
/**
13
 * Class EventTemplate models an event template.
14
 *
15
 * Templates are the invariant part of events.
16
 *
17
 * Since this is essentially a value object, naming is constrained by the
18
 * property names in MongoDB, so ignore variable naming rules for fields.
19
 *
20
 * @SuppressWarnings(PHPMD.CamelCasePropertyName)
21
 */
22
class EventTemplate implements Unserializable {
23
  use StringTranslationTrait;
24
25
  // @codingStandardsIgnoreStart
26
  /**
27
   * The event identifier.
28
   *
29
   * Coding standards are suspended to avoid a warning on _id, which is not
30
   * standards-compliant, but required by MongoDB.
31
   *
32
   * @var string
33
   */
34
  public $_id;
35
  // @codingStandardsIgnoreEnd
36
37
  /**
38
   * Latest event insertion for the template.
39
   *
40
   * @var int
41
   */
42
  public $changed;
43
44
  /**
45
   * Event count for the template.
46
   *
47
   * @var int
48
   */
49
  public $count;
50
51
  /**
52
   * The message "type": a Drupal logger "channel".
53
   *
54
   * @var string
55
   */
56
  public $type;
57
58
  /**
59
   * The event template message with placeholders, not substituted.
60
   *
61
   * @var string
62
   */
63
  public $message;
64
65
  /**
66
   * The RFC 5424 severity level.
67
   *
68
   * @var int
69
   */
70
  public $severity;
71
72
  /**
73
   * EventTemplate constructor.
74
   *
75
   * @param array<mixed,mixed> $data
76
   *   The raw properties.
77
   */
78
  public function __construct(array $data) {
79
    $this->bsonUnserialize($data);
80
  }
81
82
  /**
83
   * List the template keys and their behaviours.
84
   *
85
   * @return array<string, array<string, mixed>>
86
   *   A properties by key array.
87
   */
88
  public static function keys(): array {
89
    $ret = [
90
      '_id' => [
91
        'label' => t('ID'),
0 ignored issues
show
Bug introduced by
The function t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        'label' => /** @scrutinizer ignore-call */ t('ID'),
Loading history...
92
      ],
93
      'changed' => [
94
        'label' => t('Changed'),
95
        'creation_callback' => 'intval',
96
      ],
97
      'count' => [
98
        'label' => t('Count'),
99
        'creation_callback' => 'intval',
100
      ],
101
      'type' => [
102
        'label' => t('Type'),
103
      ],
104
      'message' => [
105
        'label' => t('Message'),
106
      ],
107
      'severity' => [
108
        'label' => t('Severity'),
109
        'creation_callback' => 'intval',
110
        'display_callback' => function ($level) {
111
          return RfcLogLevel::getLevels()[$level];
112
        },
113
      ],
114
    ];
115
116
    return $ret;
117
  }
118
119
  /**
120
   * {@inheritdoc}
121
   *
122
   * @param array<mixed,mixed> $data
123
   *   The raw data.
124
   */
125
  public function bsonUnserialize(array $data): void {
126
    foreach (static::keys() as $key => $info) {
127
      $datum = $data[$key] ?? NULL;
128
      $this->{$key} = isset($info['creation_callback'])
129
        ? $info['creation_callback']($datum)
130
        : $datum;
131
    }
132
    if (!is_string($this->message)) {
0 ignored issues
show
introduced by
The condition is_string($this->message) is always true.
Loading history...
133
      $this->message = print_r($this->message, TRUE);
134
    }
135
  }
136
137
  /**
138
   * Returns the message with its variables substituted into it.
139
   *
140
   * This code passes a variable to $this->t() because the "variable" ultimately
141
   * comes from a module code, not from user input. This assumes modules
142
   * correctly pass only template messages to PSR-3 methods, instead of already
143
   * assembled messages.
144
   *
145
   * XXX babysit broken modules using messages containing user input.
146
   *
147
   * This code disables coding standards because the module relies on translated
148
   * event templates, which are known to be variable but - assuming no coding
149
   * errors - will always match a constant event template string found in code.
150
   *
151
   * @param array<string,string|\Stringable> $variables
152
   *   The event variables.
153
   *
154
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
155
   *   The template message with its variables substituted.
156
   */
157
  public function asString(array $variables): TranslatableMarkup {
158
    // @codingStandardsIgnoreStart
159
    return $this->t($this->message, $variables);
160
    // @codingStandardsIgnoreEnd
161
  }
162
163
}
164