1
|
|
|
<?php |
2
|
|
|
namespace App\Console\Commands; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use App\Room; |
6
|
|
|
use Gitter\Client; |
7
|
|
|
use React\EventLoop\LoopInterface; |
8
|
|
|
use Gitter\Models\Room as GitterRoom; |
9
|
|
|
use Gitter\Models\Message as GitterMessage; |
10
|
|
|
use Illuminate\Contracts\Config\Repository; |
11
|
|
|
use Illuminate\Contracts\Container\Container; |
12
|
|
|
use Gitter\Iterators\PromiseIterator\Controls; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class GitterRoomListenCommand |
18
|
|
|
* @package App\Console\Commands |
19
|
|
|
* |
20
|
|
|
* Прослушивание сообщений требуемой комнаты |
21
|
|
|
*/ |
22
|
|
|
class GitterRoomListenCommand extends AbstractCommand |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $signature = 'gitter:listen {room}'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Listen gitter room'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute the console command. |
36
|
|
|
* |
37
|
|
|
* @param Repository $config |
38
|
|
|
* @param Container $container |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function handle(Repository $config, Container $container) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
/** @var Client $client */ |
45
|
|
|
$client = $this->createClient($container, $config->get('gitter.token')); |
46
|
|
|
|
47
|
|
|
$this->findRoom($client, $this->argument('room'), function(GitterRoom $room) { |
|
|
|
|
48
|
|
|
try { |
49
|
|
|
$this->listen($room); |
50
|
|
|
} catch (\Throwable $e) { |
51
|
|
|
$this->error($e->getMessage()); |
52
|
|
|
} |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** @var LoopInterface $loop */ |
57
|
|
|
$loop = $container->make(LoopInterface::class); |
58
|
|
|
$loop->run(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param GitterRoom $gitter |
63
|
|
|
*/ |
64
|
|
|
protected function listen(GitterRoom $gitter) |
65
|
|
|
{ |
66
|
|
|
/** @var Room $room */ |
67
|
|
|
$room = Room::make($gitter, function(Room $room) { |
|
|
|
|
68
|
|
|
// |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
/** @var LoopInterface $loop */ |
72
|
|
|
$loop = app(LoopInterface::class); |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$gitter->onMessage(function(GitterMessage $msg) { |
76
|
|
|
$this->onMessage($msg); |
77
|
|
|
}, function(\Throwable $e) use ($loop, $gitter) { |
|
|
|
|
78
|
|
|
$this->onMessageFallback($loop, $gitter); |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Message stream throw http api |
84
|
|
|
* |
85
|
|
|
* @param LoopInterface $loop |
86
|
|
|
* @param GitterRoom $gitter |
87
|
|
|
*/ |
88
|
|
|
protected function onMessageFallback(LoopInterface $loop, GitterRoom $gitter) |
89
|
|
|
{ |
90
|
|
|
$this->warn('Stream not available. Use message fallback fetch mode.'); |
91
|
|
|
|
92
|
|
|
$gitter->getMessages() |
93
|
|
|
->fetch(function(GitterMessage $message, Controls $controls) { |
94
|
|
|
$this->onMessage($message); |
95
|
|
|
|
96
|
|
|
if ($controls->index() <= 100) { |
97
|
|
|
$controls->next(); |
98
|
|
|
} |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
$loop->addTimer(10, function() use ($gitter) { |
|
|
|
|
102
|
|
|
$this->warn('Connection retries.'); |
103
|
|
|
$this->listen($gitter); |
104
|
|
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function onMessage(GitterMessage $gitter) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
// |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
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.