AbstractRoom::middleware()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author butschster <[email protected]>
6
 * @date   20.07.2016 15:27
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Domains\Room;
13
14
use Domains\Bot\ClientInterface;
15
use Illuminate\Foundation\Application;
16
use Interfaces\Gitter\Client;
17
18
abstract class AbstractRoom implements RoomInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $id;
24
25
    /**
26
     * @var string
27
     */
28
    protected $alias;
29
30
    /**
31
     * @var array
32
     */
33
    protected $groups;
34
35
    /**
36
     * @var \Domains\Middleware\Storage
37
     */
38
    protected $middleware;
39
40
    /**
41
     * @var ClientInterface
42
     */
43
    protected $client;
44
45
    /**
46
     * @var Application
47
     */
48
    protected $app;
49
50
    public function __construct()
51
    {
52
        $this->app = \App::make('app');
53
54
        $this->middleware = new \Domains\Middleware\Storage(
55
            $this->app
56
        );
57
58
        $this->client = app('bot.manager')->driver($this->driver());
59
60
        $this->createSubscribersStorage();
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function id()
67
    {
68
        return $this->id;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function alias()
75
    {
76
        return $this->alias;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function groups()
83
    {
84
        return (array) $this->groups;
85
    }
86
87
    /**
88
     * @return ClientInterface
89
     */
90
    public function client()
91
    {
92
        return $this->client;
93
    }
94
95
    /**
96
     * @return \Domains\Middleware\Storage
97
     */
98
    public function middleware()
99
    {
100
        return $this->middleware;
101
    }
102
103
    /**
104
     * Create subscribers storage
105
     */
106
    protected function createSubscribersStorage()
107
    {
108
        $subscribers = \Config::get('gitter.subscribers');
109
110
        $storage = new \Domains\Subscriber\Storage(
111
            $this->app,
112
            $this
113
        );
114
115
        foreach ($subscribers as $subscriber) {
116
            $storage->add($subscriber);
117
        }
118
    }
119
120
    /**
121
     * Create middleware storage
122
     *
123
     * @param array $groups
124
     */
125
    protected function setMiddleware(array $groups)
126
    {
127
        $registerAll = in_array('*', $this->groups());
128
129
        foreach ($groups as $group => $middleware) {
130
            if (is_array($middleware)) {
131
                if ($registerAll or in_array($group, $this->groups())) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
132
                    foreach ($middleware as $class) {
133
                        $this->middleware->add($class);
134
                    }
135
                }
136
            } else {
137
138
                $this->middleware->add($group);
139
            }
140
        }
141
    }
142
143
    /**
144
     * @return Client
145
     */
146
    public function listen()
147
    {
148
        $this->client()->listen($this);
149
    }
150
151
    /**
152
     * @param string $message
153
     * @return $this
154
     */
155
    public function sendMessage($message)
156
    {
157
        $this->client()->sendMessage($this, $message);
158
    }
159
}