Passed
Pull Request — 8.x-2.x (#69)
by Frédéric G.
06:32
created

MongodbQueue::doCreateItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Drupal\mongodb\Queue;
5
6
use Drupal\Core\Queue\QueueInterface;
7
use Drupal\mongodb\MongoDb;
8
use MongoDB\Database;
9
10
/**
11
 * Mongodb queue implementation.
12
 *
13
 * @ingroup queue
14
 */
15
class MongodbQueue implements QueueInterface {
16
17
  /**
18
   * The queue storage.
19
   *
20
   * @var \MongoDB\Database
21
   */
22
  protected $database;
23
24
  /**
25
   * The collection name for the queue.
26
   *
27
   * @var \MongoDB\Collection[] $collection
28
   */
29
  protected $collection;
30
31
  /**
32
   * Constructs a \Drupal\mongodb\Queue\MongodbQueue object.
33
   *
34
   * @param string $name
35
   *   The name of the queue.
36
   * @param array $settings
37
   *   Array of Mongodb-related settings for this queue.
38
   * @param \MongoDB\Database $database
39
   *   The database object.
40
   */
41
  public function __construct($name, $settings, Database $database) {
42
    $this->name = $name;
43
    $this->database = $database;
44
    $this->collection = $this->database
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->database->selectCollection($name) of type object<MongoDB\Collection> is incompatible with the declared type array<integer,object<MongoDB\Collection>> of property $collection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
      ->selectCollection($name);
46
  }
47
48
  /**
49
   * {@inheritdoc}
50
   */
51
  public function createItem($data) {
52
    try {
53
      $id = $this->doCreateItem($data);
54
    }
55
    catch (\Exception $e) {
56
        throw $e;
57
    }
58
59
    return $id;
60
  }
61
62
  /**
63
   * Adds a queue item and store it directly to the queue.
64
   *
65
   * @param $data
66
   *   Arbitrary data to be associated with the new task in the queue.
67
   *
68
   * @return
69
   *   A unique ID if the item was successfully created and was (best effort)
70
   *   added to the queue, otherwise FALSE. We don't guarantee the item was
71
   *   committed to disk etc, but as far as we know, the item is now in the
72
   *   queue.
73
   */
74
  protected function doCreateItem($data) {
75
76
    $item = [
77
      'name' => $this->name,
78
      'data' => $data,
79
      'expire' => 0,
80
      'created' => time(),
81
    ];
82
83
    $result = $this->collection->insertOne($item);
0 ignored issues
show
Bug introduced by
The method insertOne cannot be called on $this->collection (of type array<integer,object<MongoDB\Collection>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
84
85
    return $result->getInsertedId();
86
  }
87
88
  /**
89
   * {@inheritdoc}
90
   */
91
  public function numberOfItems() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
92
93
    try {
94
      return (int) MongoDb::countCollection($this->collection);
0 ignored issues
show
Documentation introduced by
$this->collection is of type array<integer,object<MongoDB\Collection>>, but the function expects a object<MongoDB\Collection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
    }
96
    catch (\Exception $e) {
97
      throw $e;
98
      // If there is no collection there cannot be any items.
99
      return 0;
0 ignored issues
show
Unused Code introduced by
return 0; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
100
    }
101
  }
102
103
  /**
104
   * {@inheritdoc}
105
   */
106
  public function claimItem($lease_time = 30) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
107
    $newobj = [
108
      'expire' => time() + $lease_time,
109
    ];
110
    return $this->collection->findOneAndUpdate(
0 ignored issues
show
Bug introduced by
The method findOneAndUpdate cannot be called on $this->collection (of type array<integer,object<MongoDB\Collection>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
111
      [],
112
      [ '$set' => $newobj ],
113
      [ 'sort' => ['created' => 1],]
114
    );
115
116
117
  }
118
119
  /**
120
   * {@inheritdoc}
121
   */
122
  public function releaseItem($item) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
123
    return $this->collection
0 ignored issues
show
Bug introduced by
The method updateOne cannot be called on $this->collection (of type array<integer,object<MongoDB\Collection>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
124
      ->updateOne(
125
        ['_id' => $item->_id],
126
        ['$set' =>
127
          [
128
            'expire' => 0
129
          ],
130
        ]
131
      );
132
  }
133
134
  /**
135
   * {@inheritdoc}
136
   */
137
  public function deleteItem($item) {
138
    try {
139
      $this->collection
0 ignored issues
show
Bug introduced by
The method deleteOne cannot be called on $this->collection (of type array<integer,object<MongoDB\Collection>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
140
        ->deleteOne(
141
          [
142
            '_id' => $item->_id
143
          ]
144
        );
145
    }
146
    catch (\Exception $e) {
147
      throw $e;
148
    }
149
  }
150
151
  /**
152
   * {@inheritdoc}
153
   */
154
  public function createQueue() {
155
    // Create the index.
156
    $this->collection->createIndex([
0 ignored issues
show
Bug introduced by
The method createIndex cannot be called on $this->collection (of type array<integer,object<MongoDB\Collection>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
157
      'expire' => 1,
158
      'created' => 1
159
    ]);
160
  }
161
162
  /**
163
   * {@inheritdoc}
164
   */
165
  public function deleteQueue() {
166
    try {
167
      $this->collection->drop();
0 ignored issues
show
Bug introduced by
The method drop cannot be called on $this->collection (of type array<integer,object<MongoDB\Collection>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
168
    }
169
    catch (\Exception $e) {
170
      throw $e;
171
    }
172
  }
173
174
}
175