1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of GitterBot package. |
4
|
|
|
* |
5
|
|
|
* @author Serafim <[email protected]> |
6
|
|
|
* @date 26.01.2016 4:57 |
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\Console\CircleProgress; |
14
|
|
|
use App\User; |
15
|
|
|
use Gitter\Client; |
16
|
|
|
use React\EventLoop\LoopInterface; |
17
|
|
|
use Illuminate\Container\Container; |
18
|
|
|
use Gitter\Models\User as GitterUser; |
19
|
|
|
use Gitter\Models\Room as GitterRoom; |
20
|
|
|
use Gitter\Models\Message as GitterMessage; |
21
|
|
|
use Illuminate\Contracts\Config\Repository; |
22
|
|
|
use Gitter\Iterators\PromiseIterator\Controls; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class GitterRoomSyncCommand |
26
|
|
|
* @package App\Console\Commands |
27
|
|
|
*/ |
28
|
|
|
class GitterRoomSyncCommand extends AbstractCommand |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $signature = 'gitter:sync {room} {type=users}'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $description = 'Sync gitter room'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Execute the console command. |
42
|
|
|
* |
43
|
|
|
* @param Repository $config |
44
|
|
|
* @param Container $container |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function handle(Repository $config, Container $container) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
/** @var Client $client */ |
51
|
|
|
$client = $this->createClient($container, $config->get('gitter.token')); |
52
|
|
|
|
53
|
|
|
// Search room |
54
|
|
|
$this->findRoom($client, $this->argument('room'), function(GitterRoom $room) { |
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$this->listen($room); |
57
|
|
|
} catch (\Throwable $e) { |
58
|
|
|
$this->error($e->getMessage()); |
59
|
|
|
} |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
/** @var LoopInterface $loop */ |
63
|
|
|
$loop = $container->make(LoopInterface::class); |
64
|
|
|
$loop->run(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param GitterRoom $room |
69
|
|
|
*/ |
70
|
|
|
public function listen(GitterRoom $room) |
71
|
|
|
{ |
72
|
|
|
/** @var CircleProgress $circle */ |
73
|
|
|
$circle = new CircleProgress(); |
74
|
|
|
$this->output->write('Fetching...'); |
75
|
|
|
|
76
|
|
|
$room->getUsers()->fetch(function(GitterUser $gitterUser, Controls $controls) use ($circle) { |
77
|
|
|
$user = User::make($gitterUser)->login; |
78
|
|
|
|
79
|
|
|
$this->output->write(sprintf("\r[%s] %s %s", $circle->get(), $user, str_repeat(' ', 20))); |
80
|
|
|
flush(); |
81
|
|
|
|
82
|
|
|
$controls->next(); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
} |
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.