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
Push — master ( ed1d01...0dc6f8 )
by Henrik
02:09
created

DoctrineDriver::createQueue()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 14
nc 2
nop 1
1
<?php
2
3
namespace Bernard\Driver;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Exception\ConstraintViolationException;
7
8
/**
9
 * Driver supporting Doctrine DBAL
10
 *
11
 * @package Bernard
12
 */
13
class DoctrineDriver implements \Bernard\Driver
14
{
15
    protected $connection;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function __construct(Connection $connection)
21
    {
22
        $this->connection = $connection;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function listQueues()
29
    {
30
        $statement = $this->connection->prepare('SELECT name FROM bernard_queues');
31
        $statement->execute();
32
33
        return $statement->fetchAll(\PDO::FETCH_COLUMN);
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function createQueue($queueName)
40
    {
41
        try {
42
            $this->connection->transactional(function () use ($queueName) {
43
                $queueExistsQb = $this->connection->createQueryBuilder();
44
45
                $queueExists = $queueExistsQb
46
                    ->select('name')
47
                    ->from('bernard_queues')
48
                    ->where($queueExistsQb->expr()->eq('name', ':name'))
49
                    ->setParameter('name', $queueName)
50
                    ->execute();
51
52
                if ($queueExists->fetch()) {
53
                    // queue was already created
54
                    return;
55
                }
56
57
                $this->connection->insert('bernard_queues', array('name' => $queueName));
58
            });
59
        } catch (ConstraintViolationException $ignored) {
60
            // Because SQL server does not support a portable INSERT ON IGNORE syntax
61
            // this ignores error based on primary key.
62
        }
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function countMessages($queueName)
69
    {
70
        $query = 'SELECT COUNT(id) FROM bernard_messages WHERE queue = :queue AND visible = :visible';
71
72
        return (integer) $this->connection->fetchColumn($query, [
73
            'queue' => $queueName,
74
            'visible' => true,
75
        ]);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function pushMessage($queueName, $message)
82
    {
83
        $types = ['string', 'string', 'datetime'];
84
        $data = [
85
            'queue' => $queueName,
86
            'message' => $message,
87
            'sentAt' => new \DateTime(),
88
        ];
89
90
        $this->createQueue($queueName);
91
        $this->connection->insert('bernard_messages', $data, $types);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function popMessage($queueName, $duration = 5)
98
    {
99
        $runtime = microtime(true) + $duration;
100
101
        while (microtime(true) < $runtime) {
102
            $this->connection->beginTransaction();
103
104
            try {
105
                $message = $this->doPopMessage($queueName);
106
107
                $this->connection->commit();
108
            } catch (\Exception $e) {
109
                $this->connection->rollback();
110
            }
111
112
            if (isset($message)) {
113
                return $message;
114
            }
115
116
            //sleep for 10 ms
117
            usleep(10000);
118
        }
119
    }
120
121 View Code Duplication
    protected function doPopMessage($queueName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
122
    {
123
        $query = 'SELECT id, message FROM bernard_messages
124
                  WHERE queue = :queue AND visible = :visible
125
                  ORDER BY sentAt, id LIMIT 1' . $this->connection->getDatabasePlatform()->getForUpdateSql();
126
127
        list($id, $message) = $this->connection->fetchArray($query, array(
128
            'queue' => $queueName,
129
            'visible' => true,
130
        ));
131
132
        if ($id) {
133
            $this->connection->update('bernard_messages', array('visible' => 0), compact('id'));
134
135
            return array($message, $id);
136
        }
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function acknowledgeMessage($queueName, $receipt)
143
    {
144
        $this->connection->delete('bernard_messages', ['id' => $receipt, 'queue' => $queueName]);
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function peekQueue($queueName, $index = 0, $limit = 20)
151
    {
152
        $parameters = [$queueName, $limit, $index];
153
        $types = ['string', 'integer', 'integer'];
154
155
        $query = 'SELECT message FROM bernard_messages WHERE queue = ? ORDER BY sentAt LIMIT ? OFFSET ?';
156
157
        return $this
158
            ->connection
159
            ->executeQuery($query, $parameters, $types)
160
            ->fetchAll(\PDO::FETCH_COLUMN)
161
        ;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function removeQueue($queueName)
168
    {
169
        $this->connection->delete('bernard_messages', ['queue' => $queueName]);
170
        $this->connection->delete('bernard_queues', ['name' => $queueName]);
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function info()
177
    {
178
        $params = $this->connection->getParams();
179
180
        unset($params['user'], $params['password']);
181
182
        return $params;
183
    }
184
185
    /**
186
     * Execute the actual query and process the response
187
     *
188
     * @param string $queueName
189
     *
190
     * @return array|null
191
     */
192 View Code Duplication
    protected function doPopMessage($queueName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
193
    {
194
        $query = 'SELECT id, message FROM bernard_messages
195
                  WHERE queue = :queue AND visible = :visible
196
                  ORDER BY sentAt LIMIT 1 ' . $this->connection->getDatabasePlatform()->getForUpdateSql();
197
198
        list($id, $message) = $this->connection->fetchArray($query, [
199
            'queue' => $queueName,
200
            'visible' => true,
201
        ]);
202
203
        if ($id) {
204
            $this->connection->update('bernard_messages', ['visible' => 0], compact('id'));
205
206
            return [$message, $id];
207
        }
208
    }
209
}
210