Completed
Push — watchdog-config ( 593fab )
by Frédéric G.
8s
created

EventTemplate::keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 14
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 21
rs 9.3142
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 {
0 ignored issues
show
Coding Style introduced by
The property $_id is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
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
   * The message "type": a Drupal logger "channel".
26
   *
27
   * @var string
28
   */
29
  public $type;
30
31
  /**
32
   * The event template message with placeholders, not substituted.
33
   *
34
   * @var string
35
   */
36
  public $message;
37
38
  /**
39
   * The RFC 5424 severity level.
40
   *
41
   * @var int
42
   */
43
  public $severity;
44
45
  /**
46
   * EventTemplate constructor.
47
   *
48
   * @param array $data
49
   *   The raw properties.
50
   */
51
  public function __construct(array $data) {
52
    $this->bsonUnserialize($data);
53
  }
54
55
  /**
56
   * List the template keys and their behaviours.
57
   *
58
   * @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...
59
   *   A properties by key array.
60
   */
61
  public static function keys() {
62
    $ret = [
63
      '_id' => [
64
        'label' => t('ID'),
65
      ],
66
      'type' => [
67
        'label' => t('Type'),
68
      ],
69
      'message' => [
70
        'label' => t('Message'),
71
      ],
72
      'severity' => [
73
        'label' => t('Severity'),
74
        'creation_callback' => 'intval',
75
        'display_callback' => function ($level) {
76
          return RfcLogLevel::getLevels()[$level];
77
        },
78
      ],
79
    ];
80
    return $ret;
81
  }
82
83
  /**
84
   * {@inheritdoc}
85
   */
86
  public function bsonUnserialize(array $data) {
87
    foreach (static::keys() as $key => $info) {
88
      $this->{$key} = isset($info['creation_callback'])
89
        ? $info['creation_callback']($data[$key])
90
        : $data[$key];
91
    }
92
  }
93
94
  /**
95
   * Returns the message with its variables substituted into it.
96
   *
97
   * This code passes a variable to $this->t() because the "variable" ultimately
98
   * comes from a module code, not from user input. This assumes modules
99
   * correctly pass only template messages to PSR-3 methods, instead of already
100
   * assembled messages.
101
   *
102
   * XXX babysit broken modules using messages containing user input.
103
   *
104
   * @param array $variables
105
   *   The event variables.
106
   *
107
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
108
   *   The template message with its variables substituted.
109
   */
110
  public function asString(array $variables) {
111
    return $this->t($this->message, $variables);
112
  }
113
114
}
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...
115