Completed
Push — develop ( 0a4bcc...0131cc )
by Kirill
25:10
created

MessageBus   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 2
cbo 2
dl 0
loc 85
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A enableAll() 0 7 2
A disableAll() 0 7 2
A __construct() 0 5 1
A enable() 0 5 1
A disable() 0 5 1
B send() 0 13 6
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 27.01.2016 18:12
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\Gitter\Extensions\Middlewares;
12
13
use Gitter\Models\Room as GitterRoom;
14
use Illuminate\Contracts\Support\Renderable;
15
16
/**
17
 * Class MessageBus
18
 * @package App\Gitter\Extensions\Middlewares
19
 */
20
class MessageBus
21
{
22
    /**
23
     * @var array|MessageBus[]
24
     */
25
    protected static $items = [];
26
27
    /**
28
     * @var bool
29
     */
30
    protected static $enableStatus = true;
31
32
    /**
33
     * @return void
34
     */
35
    public static function enableAll()
36
    {
37
        foreach (static::$items as $bus) {
38
            $bus->enable();
39
            static::$enableStatus = true;
40
        }
41
    }
42
43
    /**
44
     * @return void
45
     */
46
    public static function disableAll()
47
    {
48
        foreach (static::$items as $bus) {
49
            $bus->disable();
50
            static::$enableStatus = false;
51
        }
52
    }
53
54
    /**
55
     * @var bool
56
     */
57
    protected $enabled = true;
58
59
    /**
60
     * MessageBus constructor.
61
     */
62
    public function __construct()
63
    {
64
        $this->enabled = static::$enableStatus;
65
        static::$items[] = $this;
66
    }
67
68
    /**
69
     * @return $this
70
     */
71
    public function enable()
72
    {
73
        $this->enabled = true;
74
        return $this;
75
    }
76
77
    /**
78
     * @return $this
79
     */
80
    public function disable()
81
    {
82
        $this->enabled = false;
83
        return $this;
84
    }
85
86
    /**
87
     * @param GitterRoom $room
88
     * @param $text
89
     * @param bool $force
90
     */
91
    public function send(GitterRoom $room, $text, $force = false)
92
    {
93
        if ($this->enabled || $force) {
94
            if (is_object($text)) {
95
                if ($text instanceof Renderable) {
96
                    $text = $text->render();
97
                } elseif (method_exists($text, '__toString')) {
98
                    $text = (string)$text;
99
                }
100
            }
101
            $room->sendMessage($text);
102
        }
103
    }
104
}
105