Skip to content

Commit 6c352c1

Browse files
committed
PHP 7.1+. Queue Interop typed interfaces.
1 parent 604bc14 commit 6c352c1

17 files changed

Lines changed: 244 additions & 647 deletions

‎bin/pre-commit‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ function runPhpLint()
7575
{
7676
global $phpBin, $projectRootDir;
7777

78-
$filesWithErrors = array();
78+
$output = [];
7979
foreach (getFilesToFix() as $file) {
80-
$output = '';
80+
$commandOutput = null;
8181
$returnCode = null;
82-
exec(sprintf('%s -l %s 2>/dev/null', $phpBin, $projectRootDir.'/'.$file), $output, $returnCode);
82+
exec(sprintf('%s -l %s', $phpBin, $projectRootDir.'/'.$file), $commandOutput, $returnCode);
8383

8484
if ($returnCode) {
85-
$filesWithErrors[] = $file;
85+
$output[] = $commandOutput;
8686
}
8787
}
8888

89-
return $filesWithErrors;
89+
return $output;
9090
}
9191

9292
function runPhpCsFixer()
@@ -145,9 +145,10 @@ echo sprintf('Found %s staged files', count(getFilesToFix())).PHP_EOL;
145145
$phpSyntaxErrors = runPhpLint();
146146
if ($phpSyntaxErrors) {
147147
echo "Php syntax errors were found in next files:" . PHP_EOL;
148-
149-
foreach ($phpSyntaxErrors as $error) {
150-
echo $error . PHP_EOL;
148+
foreach ($phpSyntaxErrors as $phpSyntaxErrors) {
149+
echo array_walk_recursive($phpSyntaxErrors, function($item) {
150+
echo $item.PHP_EOL;
151+
}) . PHP_EOL;
151152
}
152153

153154
exit(1);

‎pkg/dbal/DbalConnectionFactory.php‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\DBAL\Connection;
66
use Doctrine\DBAL\DriverManager;
77
use Interop\Queue\PsrConnectionFactory;
8+
use Interop\Queue\PsrContext;
89

910
class DbalConnectionFactory implements PsrConnectionFactory
1011
{
@@ -54,11 +55,9 @@ public function __construct($config = 'mysql:')
5455
}
5556

5657
/**
57-
* {@inheritdoc}
58-
*
5958
* @return DbalContext
6059
*/
61-
public function createContext()
60+
public function createContext(): PsrContext
6261
{
6362
if ($this->config['lazy']) {
6463
return new DbalContext(function () {
@@ -69,20 +68,14 @@ public function createContext()
6968
return new DbalContext($this->establishConnection(), $this->config);
7069
}
7170

72-
/**
73-
* {@inheritdoc}
74-
*/
75-
public function close()
71+
public function close(): void
7672
{
7773
if ($this->connection) {
7874
$this->connection->close();
7975
}
8076
}
8177

82-
/**
83-
* @return Connection
84-
*/
85-
private function establishConnection()
78+
private function establishConnection(): Connection
8679
{
8780
if (false == $this->connection) {
8881
$this->connection = DriverManager::getConnection($this->config['connection']);

‎pkg/dbal/DbalConsumer.php‎

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Interop\Queue\InvalidMessageException;
88
use Interop\Queue\PsrConsumer;
99
use Interop\Queue\PsrMessage;
10+
use Interop\Queue\PsrQueue;
1011

1112
class DbalConsumer implements PsrConsumer
1213
{
@@ -26,57 +27,44 @@ class DbalConsumer implements PsrConsumer
2627
private $queue;
2728

2829
/**
29-
* @var int microseconds
30+
* @var int
3031
*/
31-
private $pollingInterval = 1000000;
32+
private $pollingInterval;
3233

33-
/**
34-
* @param DbalContext $context
35-
* @param DbalDestination $queue
36-
*/
3734
public function __construct(DbalContext $context, DbalDestination $queue)
3835
{
3936
$this->context = $context;
4037
$this->queue = $queue;
4138
$this->dbal = $this->context->getDbalConnection();
39+
40+
$this->pollingInterval = 1000;
4241
}
4342

4443
/**
45-
* Set polling interval in milliseconds.
46-
*
47-
* @param int $msec
44+
* Polling interval is in milliseconds.
4845
*/
49-
public function setPollingInterval($msec)
46+
public function setPollingInterval(int $interval): void
5047
{
51-
$this->pollingInterval = $msec * 1000;
48+
$this->pollingInterval = $interval;
5249
}
5350

5451
/**
5552
* Get polling interval in milliseconds.
56-
*
57-
* @return int
5853
*/
59-
public function getPollingInterval()
54+
public function getPollingInterval(): int
6055
{
61-
return (int) $this->pollingInterval / 1000;
56+
return $this->pollingInterval;
6257
}
6358

6459
/**
65-
* {@inheritdoc}
66-
*
6760
* @return DbalDestination
6861
*/
69-
public function getQueue()
62+
public function getQueue(): PsrQueue
7063
{
7164
return $this->queue;
7265
}
7366

74-
/**
75-
* {@inheritdoc}
76-
*
77-
* @return DbalMessage|null
78-
*/
79-
public function receive($timeout = 0)
67+
public function receive(int $timeout = 0): ?PsrMessage
8068
{
8169
$timeout /= 1000;
8270
$startAt = microtime(true);
@@ -89,43 +77,34 @@ public function receive($timeout = 0)
8977
}
9078

9179
if ($timeout && (microtime(true) - $startAt) >= $timeout) {
92-
return;
80+
return null;
9381
}
9482

95-
usleep($this->pollingInterval);
83+
usleep($this->pollingInterval * 1000);
9684

9785
if ($timeout && (microtime(true) - $startAt) >= $timeout) {
98-
return;
86+
return null;
9987
}
10088
}
10189
}
10290

103-
/**
104-
* {@inheritdoc}
105-
*
106-
* @return DbalMessage|null
107-
*/
108-
public function receiveNoWait()
91+
public function receiveNoWait(): ?PsrMessage
10992
{
11093
return $this->receiveMessage();
11194
}
11295

11396
/**
114-
* {@inheritdoc}
115-
*
11697
* @param DbalMessage $message
11798
*/
118-
public function acknowledge(PsrMessage $message)
99+
public function acknowledge(PsrMessage $message): void
119100
{
120101
// does nothing
121102
}
122103

123104
/**
124-
* {@inheritdoc}
125-
*
126105
* @param DbalMessage $message
127106
*/
128-
public function reject(PsrMessage $message, $requeue = false)
107+
public function reject(PsrMessage $message, bool $requeue = false): void
129108
{
130109
InvalidMessageException::assertMessageInstanceOf($message, DbalMessage::class);
131110

@@ -136,10 +115,7 @@ public function reject(PsrMessage $message, $requeue = false)
136115
}
137116
}
138117

139-
/**
140-
* @return DbalMessage|null
141-
*/
142-
protected function receiveMessage()
118+
protected function receiveMessage(): ?DbalMessage
143119
{
144120
$this->dbal->beginTransaction();
145121
try {
@@ -149,7 +125,7 @@ protected function receiveMessage()
149125
if (false == $dbalMessage) {
150126
$this->dbal->commit();
151127

152-
return;
128+
return null;
153129
}
154130

155131
// remove message
@@ -173,13 +149,9 @@ protected function receiveMessage()
173149
}
174150
}
175151

176-
/**
177-
* @param array $dbalMessage
178-
*
179-
* @return DbalMessage
180-
*/
181-
protected function convertMessage(array $dbalMessage)
152+
protected function convertMessage(array $dbalMessage): DbalMessage
182153
{
154+
/** @var DbalMessage $message */
183155
$message = $this->context->createMessage();
184156

185157
$message->setBody($dbalMessage['body']);
@@ -198,12 +170,7 @@ protected function convertMessage(array $dbalMessage)
198170
return $message;
199171
}
200172

201-
/**
202-
* @param int $now
203-
*
204-
* @return array|null
205-
*/
206-
private function fetchPrioritizedMessage($now)
173+
private function fetchPrioritizedMessage(int $now): ?array
207174
{
208175
$query = $this->dbal->createQueryBuilder();
209176
$query
@@ -232,12 +199,7 @@ private function fetchPrioritizedMessage($now)
232199
)->fetch();
233200
}
234201

235-
/**
236-
* @param int $now
237-
*
238-
* @return array|null
239-
*/
240-
private function fetchMessage($now)
202+
private function fetchMessage(int $now): ?array
241203
{
242204
$query = $this->dbal->createQueryBuilder();
243205
$query

0 commit comments

Comments
 (0)