These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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; |
||
| 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 |
||
| 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
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 | } |
||
| 138 |