|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of GitterBot package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Serafim <[email protected]> |
|
6
|
|
|
* @date 26.01.2016 4:59 |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace App\Console\Commands; |
|
12
|
|
|
|
|
13
|
|
|
use App\Room; |
|
14
|
|
|
use App\Gitter\Client; |
|
15
|
|
|
use React\EventLoop\Factory; |
|
16
|
|
|
use Illuminate\Console\Command; |
|
17
|
|
|
use React\EventLoop\LoopInterface; |
|
18
|
|
|
use Gitter\Models\Room as GitterRoom; |
|
19
|
|
|
use Illuminate\Contracts\Container\Container; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class AbstractCommand |
|
23
|
|
|
* @package App\Console\Commands |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class AbstractCommand extends Command |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @param Container $container |
|
29
|
|
|
* @param $token |
|
30
|
|
|
* @return Client |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function createClient(Container $container, $token) |
|
33
|
|
|
{ |
|
34
|
|
|
$loop = Factory::create(); |
|
35
|
|
|
$client = new Client($loop, $token); |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
// Bind container |
|
39
|
|
|
$container->instance(LoopInterface::class, $loop); |
|
40
|
|
|
$container->instance(Client::class, $client); |
|
41
|
|
|
|
|
42
|
|
|
return $client; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Client $client |
|
47
|
|
|
* @param string $roomId |
|
48
|
|
|
* @param \Closure $fulfilled |
|
49
|
|
|
* @return \React\Promise\PromiseInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
public function findRoom(Client $client, string $roomId, \Closure $fulfilled) |
|
52
|
|
|
{ |
|
53
|
|
|
$found = function(GitterRoom $room) use ($client, $fulfilled) { |
|
54
|
|
|
$client->onChangeFallbackMode(function($state) use ($room) { |
|
55
|
|
|
if ($state) { |
|
56
|
|
|
$room->sendMessage('_Подключение к Gitter Stream API восстановлено_'); |
|
57
|
|
|
} else { |
|
58
|
|
|
$room->sendMessage('_Подключение к Gitter Stream API потеряно. Слоупок mode enabled._'); |
|
59
|
|
|
} |
|
60
|
|
|
}); |
|
61
|
|
|
return $fulfilled($room); |
|
62
|
|
|
}; |
|
63
|
|
|
|
|
64
|
|
|
return $client->getRoomByUri($roomId) |
|
65
|
|
|
->then($found, function(\Throwable $exception) use ($roomId, $client, $found) { |
|
|
|
|
|
|
66
|
|
|
$client->getRoomById($roomId)->then($found); |
|
67
|
|
|
}); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.