Completed
Push — 8.x-2.x ( c6d484...0f1fa4 )
by Frédéric G.
02:32
created

Event::bsonUnserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Drupal\mongodb_watchdog;
4
5
use MongoDB\BSON\Unserializable;
6
7
/**
8
 * Class Event.
9
 *
10
 * @package Drupal\mongodb_watchdog
11
 */
12
class Event 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...
13
14
  // @codingStandardsIgnoreStart
15
  /**
16
   * The string representation of a MongoId.
17
   *
18
   * @var int
19
   */
20
  public $_id;
21
  // @codingStandardsIgnoreEnd
22
23
  /**
24
   * User id.
25
   *
26
   * @var int
27
   */
28
  public $uid;
29
30
  /**
31
   * Event type, often a module name.
32
   *
33
   * @var string
34
   */
35
  public $type;
36
37
  /**
38
   * Event template.
39
   *
40
   * @var string
41
   */
42
  public $message;
43
44
  /**
45
   * The template parameters.
46
   *
47
   * @var array
48
   */
49
  public $variables;
50
51
  /**
52
   * A RFC5424 severity level.
53
   *
54
   * @var int
55
   */
56
  public $severity;
57
58
  /**
59
   * A link provided by the event emitter. Optional.
60
   *
61
   * @var string
62
   */
63
  public $link;
64
65
  /**
66
   * The absolute URL for the path on which the event was logged.
67
   *
68
   * @var string
69
   */
70
  public $location;
71
72
  /**
73
   * A HTTP referrer for the path on which the event was logged. Optional.
74
   *
75
   * @var string
76
   */
77
  public $referrer;
78
79
  /**
80
   * The server host.
81
   *
82
   * @var string
83
   */
84
  public $hostname;
85
86
  /**
87
   * The timestamp at which the event was logged.
88
   *
89
   * @var int
90
   */
91
  public $timestamp;
92
93
  /**
94
   * Constructor.
95
   *
96
   * @param array $event
97
   *   The event in array form.
98
   */
99
  public function __construct(array $event) {
100
    $keys = [
101
      '_id',
102
      'hostname',
103
      'link',
104
      'location',
105
      'message',
106
      'referrer',
107
      'severity',
108
      'timestamp',
109
      'type',
110
      'uid',
111
      'variables',
112
    ];
113
    foreach ($keys as $key) {
114
      if (isset($event[$key])) {
115
        $this->$key = $event[$key];
116
      }
117
    }
118
  }
119
120
  /**
121
   * Load a MongoDB watchdog event.
122
   *
123
   * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
introduced by
Doc comment for parameter $id does not match actual variable name $template_id
Loading history...
124
   *   The string representation of a MongoId.
125
   *
126
   * @return \Drupal\mongodb_watchdog\Event|bool
127
   *   FALSE if the event cannot be loaded.
128
   */
129
  public function find($template_id) {
130
    $criteria = ['_id' => new ObjectID($id)];
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
introduced by
Variable $id is undefined.
Loading history...
131
    $options = [
132
      'typeMap' => [
133
        'array' => 'array',
134
        'document' => 'array',
135
        'root' => 'Drupal\mongodb_watchdog\Event',
136
      ],
137
    ];
138
139
    $result = $this->templatesCollection->findOne($criteria, $options);
140
    var_dump($result);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($result); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
141
    return $result;
142
  }
143
144
  function bsonUnserialize(array $data) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
introduced by
Missing function doc comment
Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for bsonUnserialize.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
145
    $event = new static($data);
146
    return $event;
147
  }
148
}
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...
149