Completed
Push — develop ( b4fba5...9773af )
by Kirill
12:38
created

GitterRoomSyncCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 31.03 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 3
c 6
b 0
f 3
lcom 1
cbo 10
dl 18
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 18 18 2
A listen() 0 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
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...
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) {
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...
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
}