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())) { |
|
|
|
|
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
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.