Passed
Pull Request — 8.x-2.x (#71)
by Frédéric G.
05:37
created

Event::bsonUnserialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 4
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 MongoDB\BSON\Unserializable;
9
10
/**
11
 * Class Event is a value object for a logged event.
12
 *
13
 * @package Drupal\mongodb_watchdog
14
 *
15
 * Since this is essentially a value object, naming is constrained by the
16
 * property names in MongoDB, so ignore variable naming rules for fields.
17
 *
18
 * @SuppressWarnings(PHPMD.CamelCasePropertyName)
19
 * @SuppressWarnings(Variable)
20
 */
21
class Event implements Unserializable {
22
  const KEYS = [
23
    '_id',
24
    'hostname',
25
    'link',
26
    'location',
27
    'message',
28
    'referrer',
29
    'severity',
30
    'timestamp',
31
    'type',
32
    'uid',
33
    'variables',
34
    'requestTracking_id',
35
    'requestTracking_sequence',
36
  ];
37
38
  // @codingStandardsIgnoreStart
39
  /**
40
   * The string representation of a MongoId.
41
   *
42
   * @var int
43
   */
44
  public $_id;
45
  // @codingStandardsIgnoreEnd
46
47
  /**
48
   * User id. Use uid() instead for type safety.
49
   *
50
   * @var int
51
   */
52
  public $uid;
53
54
  /**
55
   * Event type, often a module name.
56
   *
57
   * @var string
58
   */
59
  public $type;
60
61
  /**
62
   * Event template.
63
   *
64
   * @var string
65
   */
66
  public $message;
67
68
  // @codingStandardsIgnoreStart
69
  /**
70
   * The identifier of the request during which this event occurred.
71
   *
72
   * Coding standards are suspended for requestTracking_id which is required by
73
   * the MongoDB property.
74
   *
75
   * @var string
76
   */
77
  public $requestTracking_id;
78
  // @codingStandardsIgnoreEnd
79
80
  // @codingStandardsIgnoreStart
81
  /**
82
   * The sequence number of the event during the request when it happened.
83
   *
84
   * Coding standards are suspended for requestTracking_sequence which is
85
   * required by the MongoDB property.
86
   *
87
   * @var int
88
   */
89
  public $requestTracking_sequence = 0;
90
  // @codingStandardsIgnoreEnd
91
92
  /**
93
   * The template parameters.
94
   *
95
   * @var array
96
   */
97
  public $variables = [];
98
99
  /**
100
   * A RFC5424 severity level.
101
   *
102
   * @var int
103
   */
104
  public $severity = RfcLogLevel::DEBUG;
105
106
  /**
107
   * A link provided by the event emitter. Optional.
108
   *
109
   * @var string
110
   */
111
  public $link;
112
113
  /**
114
   * The absolute URL for the path on which event was logged. Use location().
115
   *
116
   * @var string
117
   */
118
  public $location;
119
120
  /**
121
   * A HTTP referrer for the path on which the event was logged. Optional.
122
   *
123
   * @var string
124
   */
125
  public $referrer;
126
127
  /**
128
   * The server host.
129
   *
130
   * @var string
131
   */
132
  public $hostname;
133
134
  /**
135
   * The timestamp at which the event was logged.
136
   *
137
   * @var int
138
   */
139
  public $timestamp = 0;
140
141
  /**
142
   * Constructor.
143
   *
144
   * @param array $event
145
   *   The event in array form.
146
   */
147
  public function __construct(array $event) {
148
    $this->bsonUnserialize($event);
149
  }
150
151
  /**
152
   * {@inheritdoc}
153
   */
154
  public function bsonUnserialize(array $data): void {
155
    foreach (static::KEYS as $key) {
156
      if (isset($data[$key])) {
157
        $this->{$key} = $data[$key];
158
      }
159
    }
160
  }
161
162
  /**
163
   * Type-safe getter for location.
164
   *
165
   * @return string
166
   *   The location property, always as a string, never NULL.
167
   */
168
  public function location(): string {
169
    return (string) $this->location ?? '';
170
  }
171
172
  /**
173
   * Type-safe getter for uid.
174
   *
175
   * @return int
176
   *   The uid property, always as an int, never NULL.
177
   */
178
  public function uid(): int {
179
    return (int) $this->uid;
180
  }
181
182
}
183