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 | class WeatherIntervalMessageProvider |
||
11 | { |
||
12 | private $date; |
||
13 | private $interval; |
||
14 | |||
15 | public function __construct(DateInterval $interval) |
||
16 | { |
||
17 | $this->interval = $interval; |
||
18 | } |
||
19 | |||
20 | public function __invoke() |
||
21 | { |
||
22 | if (null === $this->date || (new DateTime())->sub($this->interval) > $this->date) { |
||
23 | $this->date = new DateTime(); |
||
24 | |||
25 | return new Message( |
||
26 | 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') |
||
27 | ); |
||
28 | } |
||
29 | |||
30 | return null; |
||
31 | } |
||
32 | } |
||
33 | |||
34 | class WeatherMessageProcessor |
||
35 | { |
||
36 | public function process(Message $message, array $options) |
||
0 ignored issues
–
show
|
|||
37 | { |
||
38 | $weather = json_decode($message->getBody(), true); |
||
39 | if (empty($weather['query']['results']['channel']['item']['condition']['text'])) { |
||
40 | echo 'Invalid Input'; |
||
41 | |||
42 | return; |
||
43 | } |
||
44 | |||
45 | printf("The weather is %s in Paris\n", $weather['query']['results']['channel']['item']['condition']['text']); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | $intervalMessageProvider = new WeatherIntervalMessageProvider(new DateInterval('PT5S')); |
||
50 | $weatherMessageProcessor = new WeatherMessageProcessor(); |
||
51 | $messageProvider = new CallbackMessageProvider($intervalMessageProvider); |
||
52 | $processor = new CallbackProcessor([$weatherMessageProcessor, 'process']); |
||
53 | |||
54 | $consumer = new Consumer($messageProvider, $processor); |
||
55 | |||
56 | $consumer->consume([]); |
||
57 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.