Completed
Push — develop ( 2bfb76...2f90e1 )
by Kirill
04:03
created

AbstractCommand::createClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 54
rs 9.6716
c 2
b 0
f 0
cc 1
eloc 19
nc 1
nop 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A AbstractCommand.php$0 ➔ setFallbackMode() 0 10 3
A AbstractCommand.php$0 ➔ isFallbackMode() 0 4 1
A AbstractCommand.php$0 ➔ onChangeFallbackMode() 0 5 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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) {
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
                $client->getRoomById($roomId)->then($fulfilled, function(\Throwable $exception) {
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
                    $this->error(sprintf('Room "%s" not found.', $this->argument('room')));
97
                });
98
            });
99
    }
100
}
101