|
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 Gitter\Client; |
|
14
|
|
|
use React\EventLoop\Factory; |
|
15
|
|
|
use Illuminate\Console\Command; |
|
16
|
|
|
use React\EventLoop\LoopInterface; |
|
17
|
|
|
use Illuminate\Contracts\Container\Container; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AbstractCommand |
|
21
|
|
|
* @package App\Console\Commands |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractCommand extends Command |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param Container $container |
|
27
|
|
|
* @param $token |
|
28
|
|
|
* @return Client |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function createClient(Container $container, $token) |
|
31
|
|
|
{ |
|
32
|
|
|
$loop = Factory::create(); |
|
33
|
|
|
$client = new class($loop, $token) extends Client { |
|
34
|
|
|
/** |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $fallbackMode = false; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $fallbackSubscribers = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param bool $value |
|
46
|
|
|
* @return $this |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setFallbackMode(bool $value = true) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->fallbackMode !== $value) { |
|
51
|
|
|
foreach ($this->fallbackSubscribers as $subscriber) { |
|
52
|
|
|
$subscriber($value); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
$this->fallbackMode = $value; |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function isFallbackMode() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->fallbackMode; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param \Closure $callback |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
|
|
public function onChangeFallbackMode(\Closure $callback) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->fallbackSubscribers[] = $callback; |
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
// Bind container |
|
79
|
|
|
$container->instance(LoopInterface::class, $loop); |
|
80
|
|
|
$container->instance(Client::class, $client); |
|
81
|
|
|
|
|
82
|
|
|
return $client; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param Client $client |
|
87
|
|
|
* @param string $roomId |
|
88
|
|
|
* @param \Closure $fulfilled |
|
89
|
|
|
* @return \React\Promise\PromiseInterface |
|
90
|
|
|
*/ |
|
91
|
|
|
public function findRoom(Client $client, string $roomId, \Closure $fulfilled) |
|
92
|
|
|
{ |
|
93
|
|
|
return $client->getRoomByUri($roomId) |
|
94
|
|
|
->then($fulfilled, function(\Throwable $exception) use ($roomId, $client, $fulfilled) { |
|
|
|
|
|
|
95
|
|
|
$client->getRoomById($roomId)->then($fulfilled, function(\Throwable $exception) { |
|
|
|
|
|
|
96
|
|
|
$this->error(sprintf('Room "%s" not found.', $this->argument('room'))); |
|
97
|
|
|
}); |
|
98
|
|
|
}); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.