Completed
Push — master ( 53783e...ee648a )
by Olivier
22s queued 20s
created

02-consumer-and-provider-using-callbacks.php (1 issue)

1
<?php
2
3
require_once __DIR__.'/../vendor/autoload.php';
4
5
use Swarrot\Broker\Message;
6
use Swarrot\Broker\MessageProvider\CallbackMessageProvider;
7
use Swarrot\Consumer;
8
use Swarrot\Processor\Callback\CallbackProcessor;
9
10
$i = 0;
11
$messageProvider = new CallbackMessageProvider(function () use (&$i) {
12
    $isPair = 0 == $i++ % 2;
13
14
    return $isPair ?
15
        new Message(
16
            file_get_contents('https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20%3D%20615702&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys')
17
        ) :
18
        null;
19
});
20
21
$processor = new CallbackProcessor(function (Message $message, array $options) {
0 ignored issues
show
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

21
$processor = new CallbackProcessor(function (Message $message, /** @scrutinizer ignore-unused */ array $options) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    $weather = json_decode($message->getBody(), true);
23
    if (empty($weather['query']['results']['channel']['item']['condition']['text'])) {
24
        echo 'Invalid Input';
25
26
        return;
27
    }
28
29
    printf("The weather is %s in Paris\n", $weather['query']['results']['channel']['item']['condition']['text']);
30
});
31
32
$consumer = new Consumer($messageProvider, $processor);
33
34
$consumer->consume([]);
35