1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Crummy\Phlack\Bot; |
4
|
|
|
|
5
|
|
|
use Crummy\Phlack\Common\Exception\InvalidArgumentException; |
6
|
|
|
use Crummy\Phlack\Common\Iterocitor; |
7
|
|
|
use Crummy\Phlack\Common\Matcher; |
8
|
|
|
use Crummy\Phlack\Common\Responder\ResponderInterface; |
9
|
|
|
use Crummy\Phlack\Message\MessageInterface; |
10
|
|
|
|
11
|
|
|
abstract class AbstractBot implements ResponderAware, Matcher\MatcherAggregate |
12
|
|
|
{ |
13
|
|
|
private $matcher; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Crummy\Phlack\Common\Responder\ResponderInterface |
17
|
|
|
*/ |
18
|
|
|
protected $responder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Matcher\MatcherInterface|\Closure $matcher |
22
|
|
|
* @param ResponderInterface $responder |
23
|
|
|
*/ |
24
|
11 |
|
public function __construct($matcher = null, ResponderInterface $responder = null) |
25
|
|
|
{ |
26
|
11 |
|
$matcher = $matcher ?: new Matcher\DefaultMatcher(); |
27
|
|
|
|
28
|
11 |
|
$this->setMatcher($matcher); |
|
|
|
|
29
|
|
|
|
30
|
11 |
|
if ($responder) { |
31
|
11 |
|
$this->setResponder($responder); |
32
|
|
|
} |
33
|
11 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Matcher\MatcherInterface $matcher |
37
|
|
|
* |
38
|
|
|
* @throws \Crummy\Phlack\Common\Exception\InvalidArgumentException When given an invalid matcher. |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
11 |
|
public function setMatcher($matcher) |
43
|
|
|
{ |
44
|
11 |
|
if (!$matcher instanceof Matcher\MatcherInterface && !is_callable($matcher)) { |
45
|
1 |
|
throw new InvalidArgumentException(sprintf( |
46
|
1 |
|
'The matcher must be callable, or implement \Crummy\Phlack\Common\Matcher\MatcherInterface. "%" given.', |
47
|
1 |
|
is_object($matcher) ? get_class($matcher) : gettype($matcher) |
48
|
|
|
)); |
49
|
|
|
} |
50
|
|
|
|
51
|
11 |
|
$this->matcher = $matcher; |
52
|
|
|
|
53
|
11 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Matcher\MatcherInterface|callable |
58
|
|
|
*/ |
59
|
2 |
|
public function getMatcher() |
60
|
|
|
{ |
61
|
2 |
|
return $this->matcher; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ResponderInterface $responder |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
11 |
|
public function setResponder(ResponderInterface $responder) |
69
|
|
|
{ |
70
|
11 |
|
$this->responder = $responder; |
71
|
11 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $text |
76
|
|
|
* |
77
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
78
|
|
|
*/ |
79
|
2 |
|
protected function say($text) |
80
|
|
|
{ |
81
|
2 |
|
return $this->responder->say($text); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $text |
86
|
|
|
* |
87
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
88
|
|
|
*/ |
89
|
|
|
protected function emote($text) |
90
|
|
|
{ |
91
|
|
|
return $this->responder->emote($text); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $user The user_id to tell |
96
|
|
|
* @param string $text |
97
|
|
|
* |
98
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
99
|
|
|
*/ |
100
|
|
|
protected function tell($user, $text) |
101
|
|
|
{ |
102
|
|
|
return $this->responder->tell($user, $text); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \Crummy\Phlack\WebHook\CommandInterface $user The user_id, or a CommandInterface to inspect. |
107
|
|
|
* @param string $text |
108
|
|
|
* |
109
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
110
|
|
|
*/ |
111
|
4 |
|
protected function reply($user, $text) |
112
|
|
|
{ |
113
|
4 |
|
return $this->responder->reply($user, $text); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $text |
118
|
|
|
* |
119
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
120
|
|
|
*/ |
121
|
|
|
protected function shout($text) |
122
|
|
|
{ |
123
|
|
|
return $this->responder->shout($text); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param MessageInterface $message |
128
|
|
|
* |
129
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\EmptyReply |
130
|
|
|
*/ |
131
|
|
|
protected function send(MessageInterface $message) |
132
|
|
|
{ |
133
|
|
|
return $this->responder->send($message); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.