Completed
Push — develop ( 9773af...b9ad6f )
by Kirill
32:11 queued 50s
created

GitterRoomSyncCommand::messageRemember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
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\Message;
14
use App\User;
15
use Gitter\Client;
16
use Gitter\Iterators\PromiseIterator\Controls;
17
use Gitter\Models\Message as GitterMessage;
18
use Gitter\Models\Room as GitterRoom;
19
use Gitter\Models\User as GitterUser;
20
use Illuminate\Container\Container;
21
use Illuminate\Contracts\Config\Repository;
22
use React\EventLoop\LoopInterface;
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} ' .
34
        '{--U|users} ' .
35
        '{--M|messages} ' .
36
        '{--L|latest-messages}';
37
38
    /**
39
     * @var string
40
     */
41
    protected $description = 'Sync gitter room';
42
43
    /**
44
     * Execute the console command.
45
     *
46
     * @param Repository $config
47
     * @param Container $container
48
     *
49
     * @return mixed
50
     */
51
    public function handle(Repository $config, Container $container)
52
    {
53
        /** @var Client $client */
54
        $client = $this->createClient($container, $config->get('gitter.token'));
55
56
        // Search room
57
        $this->findRoom($client, $this->argument('room'), function (GitterRoom $room) {
0 ignored issues
show
Bug introduced by
It seems like $this->argument('room') targeting Illuminate\Console\Command::argument() can also be of type array; however, App\Console\Commands\AbstractCommand::findRoom() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
58
            try {
59
                if ($this->option('users')) {
60
                    $this->syncUsers($room);
61
                }
62
63
                if ($this->option('messages')) {
64
                    $this->syncMessages($room);
65
                }
66
67
                if ($this->option('latest-messages')) {
68
                    $this->syncLatestMessages($room);
69
                }
70
            } catch (\Throwable $e) {
71
                $this->error($e->getMessage());
72
            }
73
        });
74
75
        /** @var LoopInterface $loop */
76
        $loop = $container->make(LoopInterface::class);
77
        $loop->run();
78
    }
79
80
    /**
81
     * @param GitterRoom $room
82
     */
83
    public function syncUsers(GitterRoom $room)
84
    {
85
        $this->output->write('Fetching...');
86
87
        $room->getUsers()->fetch(function (GitterUser $gitterUser, Controls $controls) {
88
            /** @var User $user */
89
            $user = User::make($gitterUser);
90
91
            $this->info(sprintf("\r[%s] %s %s", $user->created_at->toDateTimeString(), $user->login, str_repeat(' ', 20)));
92
            flush();
93
94
            $controls->next();
95
        });
96
    }
97
98
    /**
99
     * @param GitterRoom $room
100
     */
101 View Code Duplication
    public function syncMessages(GitterRoom $room)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
102
    {
103
        /** @var CircleProgress $circle */
104
        $this->output->writeln('Fetching...');
105
106
        $room->getMessages()
107
            // Fetch all messages
108
            ->fetch(function (GitterMessage $message, Controls $controls) {
109
                $this->messageRemember($message, $controls);
110
            })
111
            ->then(function () use ($room) {
112
                $this->syncLatestMessages($room);
113
            });
114
    }
115
116
    /**
117
     * @param GitterMessage $gitterMessage
118
     * @param Controls $controls
119
     */
120
    protected function messageRemember(GitterMessage $gitterMessage, Controls $controls)
121
    {
122
        User::make($gitterMessage->fromUser);
123
        $message = Message::make($gitterMessage);
124
125
        $this->info('[' . $message->created_at->toDateTimeString() . '] ' . $message->text->onlyWords());
126
        flush();
127
128
        $controls->next();
129
    }
130
131
    /**
132
     * @param GitterRoom $room
133
     */
134
    public function syncLatestMessages(GitterRoom $room)
135
    {
136
        /** @var Message $lastMessage */
137
        $lastMessage = Message::where('room_id', $room->id)
138
            ->latest('created_at')
139
            ->first();
140
141
        if ($lastMessage) {
142
            $room->getMessages($lastMessage->gitter_id, GitterRoom::MESSAGE_FETCH_ASC)
143
                ->fetch(function (GitterMessage $message, Controls $controls) {
144
                    $this->messageRemember($message, $controls);
145
                });
146
        }
147
    }
148
}
149