GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#388)
by Ben
04:21
created

Driver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 2.16 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 77.38%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 3
loc 139
ccs 65
cts 84
cp 0.7738
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A listQueues() 0 4 1
A createQueue() 0 6 1
A countMessages() 0 7 1
A pushMessage() 0 11 1
A popMessage() 3 23 4
A acknowledgeMessage() 0 7 1
A peekQueue() 0 18 1
A removeQueue() 0 5 1
A info() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bernard\Driver\MongoDB;
4
5
use MongoCollection;
6
use MongoDate;
7
use MongoId;
8
9
/**
10
 * Driver supporting MongoDB.
11
 */
12
final class Driver implements \Bernard\Driver
13
{
14
    private $messages;
15
    private $queues;
16
17
    /**
18
     * @param MongoCollection $queues   Collection where queues will be stored
19
     * @param MongoCollection $messages Collection where messages will be stored
20
     */
21 10
    public function __construct(MongoCollection $queues, MongoCollection $messages)
22
    {
23 10
        $this->queues = $queues;
24 10
        $this->messages = $messages;
25 10
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function listQueues()
31
    {
32 1
        return $this->queues->distinct('_id');
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function createQueue($queueName)
39
    {
40 1
        $data = ['_id' => (string) $queueName];
41
42 1
        $this->queues->update($data, $data, ['upsert' => true]);
43 1
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function countMessages($queueName)
49
    {
50 1
        return $this->messages->count([
51 1
            'queue' => (string) $queueName,
52 1
            'visible' => true,
53 1
        ]);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function pushMessage($queueName, $message)
60
    {
61
        $data = [
62 1
            'queue' => (string) $queueName,
63 1
            'message' => (string) $message,
64 1
            'sentAt' => new MongoDate(),
65 1
            'visible' => true,
66 1
        ];
67
68 1
        $this->messages->insert($data);
69 1
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 2
    public function popMessage($queueName, $duration = 5)
75
    {
76 2
        $startAt = microtime(true);
77
78 2
        while (true) {
79 2
            $result = $this->messages->findAndModify(
80 2
                ['queue' => (string) $queueName, 'visible' => true],
81 2
                ['$set' => ['visible' => false]],
82 2
                ['message' => 1],
83 2
                ['sort' => ['sentAt' => 1]]
84 2
            );
85
86 2
            if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
87 1
                return [(string) $result['message'], (string) $result['_id']];
88
            }
89
90 1
            usleep(10000);
91
92 1 View Code Duplication
            if ((microtime(true) - $startAt) >= $duration) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93 1
                return array(null, null);
94
            }
95 1
        }
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 1
    public function acknowledgeMessage($queueName, $receipt)
102
    {
103 1
        $this->messages->remove([
104 1
            '_id' => new MongoId((string) $receipt),
105 1
            'queue' => (string) $queueName,
106 1
        ]);
107 1
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 1
    public function peekQueue($queueName, $index = 0, $limit = 20)
113
    {
114 1
        $query = ['queue' => (string) $queueName, 'visible' => true];
115 1
        $fields = ['_id' => 0, 'message' => 1];
116
117 1
        $cursor = $this->messages
118 1
            ->find($query, $fields)
119 1
            ->sort(['sentAt' => 1])
120 1
            ->limit($limit)
121 1
            ->skip($index)
122 1
        ;
123
124 1
        $mapper = function ($result) {
125 1
            return (string) $result['message'];
126 1
        };
127
128 1
        return array_map($mapper, iterator_to_array($cursor, false));
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 1
    public function removeQueue($queueName)
135
    {
136 1
        $this->queues->remove(['_id' => $queueName]);
137 1
        $this->messages->remove(['queue' => (string) $queueName]);
138 1
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 1
    public function info()
144
    {
145
        return [
146 1
            'messages' => (string) $this->messages,
147 1
            'queues' => (string) $this->queues,
148 1
        ];
149
    }
150
}
151