Completed
Push — 8.x-2.x ( 8fc000...ebbf75 )
by Frédéric G.
04:51 queued 01:34
created

EventTemplate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 124
rs 10
wmc 6
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B keys() 0 29 1
A bsonUnserialize() 0 8 3
A asString() 0 3 1
1
<?php
2
3
namespace Drupal\mongodb_watchdog;
4
5
use Drupal\Core\Logger\RfcLogLevel;
6
use Drupal\Core\StringTranslation\StringTranslationTrait;
7
use MongoDB\BSON\Unserializable;
8
9
/**
10
 * Class EventTemplate models an event template.
11
 *
12
 * Templates are the invariant part of events.
13
 */
14
class EventTemplate implements Unserializable {
15
  use StringTranslationTrait;
16
17
  /**
18
   * The event identifier.
19
   *
20
   * @var string
21
   */
22
  public $_id;
0 ignored issues
show
introduced by
Class property $_id should use lowerCamel naming without underscores
Loading history...
23
24
  /**
25
   * Latest event insertion for the template.
26
   *
27
   * @var int
28
   */
29
  public $changed;
30
31
  /**
32
   * Event count for the template.
33
   *
34
   * @var int
35
   */
36
  public $count;
37
38
  /**
39
   * The message "type": a Drupal logger "channel".
40
   *
41
   * @var string
42
   */
43
  public $type;
44
45
  /**
46
   * The event template message with placeholders, not substituted.
47
   *
48
   * @var string
49
   */
50
  public $message;
51
52
  /**
53
   * The RFC 5424 severity level.
54
   *
55
   * @var int
56
   */
57
  public $severity;
58
59
  /**
60
   * EventTemplate constructor.
61
   *
62
   * @param array $data
63
   *   The raw properties.
64
   */
65
  public function __construct(array $data) {
66
    $this->bsonUnserialize($data);
67
  }
68
69
  /**
70
   * List the template keys and their behaviours.
71
   *
72
   * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
73
   *   A properties by key array.
74
   */
75
  public static function keys() {
76
    $ret = [
77
      '_id' => [
78
        'label' => t('ID'),
79
      ],
80
      'changed' => [
81
        'label' => t('Changed'),
82
        'creation_callback' => 'intval',
83
      ],
84
      'count' => [
85
        'label' => t('Count'),
86
        'creation_callback' => 'intval',
87
      ],
88
      'type' => [
89
        'label' => t('Type'),
90
      ],
91
      'message' => [
92
        'label' => t('Message'),
93
      ],
94
      'severity' => [
95
        'label' => t('Severity'),
96
        'creation_callback' => 'intval',
97
        'display_callback' => function ($level) {
98
          return RfcLogLevel::getLevels()[$level];
99
        },
100
      ],
101
    ];
102
    return $ret;
103
  }
104
105
  /**
106
   * {@inheritdoc}
107
   */
108
  public function bsonUnserialize(array $data) {
109
    foreach (static::keys() as $key => $info) {
110
      $datum = $data[$key] ?? NULL;
1 ignored issue
show
introduced by
Expected 1 space after "?"; 0 found
Loading history...
111
      $this->{$key} = isset($info['creation_callback'])
112
        ? $info['creation_callback']($datum)
113
        : $datum;
114
    }
115
  }
116
117
  /**
118
   * Returns the message with its variables substituted into it.
119
   *
120
   * This code passes a variable to $this->t() because the "variable" ultimately
121
   * comes from a module code, not from user input. This assumes modules
122
   * correctly pass only template messages to PSR-3 methods, instead of already
123
   * assembled messages.
124
   *
125
   * XXX babysit broken modules using messages containing user input.
126
   *
127
   * @param array $variables
128
   *   The event variables.
129
   *
130
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
131
   *   The template message with its variables substituted.
132
   */
133
  public function asString(array $variables) {
134
    return $this->t($this->message, $variables);
135
  }
136
137
}
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...
138