1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Crummy\Phlack\Bot\Mainframe; |
4
|
|
|
|
5
|
|
|
use Crummy\Phlack\Bot\BotInterface; |
6
|
|
|
use Crummy\Phlack\Common\Events; |
7
|
|
|
use Crummy\Phlack\Common\Exception\InvalidArgumentException; |
8
|
|
|
use Crummy\Phlack\Common\Executable; |
9
|
|
|
use Crummy\Phlack\Common\Matcher\MatcherAggregate; |
10
|
|
|
use Crummy\Phlack\Common\Matcher\MatcherInterface; |
11
|
|
|
use Crummy\Phlack\WebHook\CommandInterface; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
13
|
|
|
|
14
|
|
|
class Mainframe implements Executable |
15
|
|
|
{ |
16
|
|
|
private $cpu; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor. |
20
|
|
|
*/ |
21
|
9 |
|
public function __construct() |
22
|
1 |
|
{ |
23
|
9 |
|
$this->cpu = new EventDispatcher(); |
24
|
|
|
|
25
|
9 |
|
$this->cpu->addSubscriber(new Plugin\EncoderPlugin()); |
26
|
9 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param CommandInterface $command |
30
|
|
|
* |
31
|
|
|
* @return Packet |
32
|
|
|
*/ |
33
|
|
|
public function execute(CommandInterface $command) |
34
|
|
|
{ |
35
|
|
|
$packet = $this->cpu->dispatch(Events::RECEIVED_COMMAND, new Packet(['command' => $command])); |
36
|
|
|
|
37
|
|
|
return $this->cpu->dispatch(Events::AFTER_EXECUTE_COMMAND, $packet); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param BotInterface $bot |
42
|
|
|
* @param MatcherInterface|callable $matcher If callable, it should accept a CommandInterface and return a boolean. |
43
|
|
|
* @param int $priority |
44
|
|
|
* |
45
|
|
|
* @return self |
46
|
|
|
*/ |
47
|
1 |
|
public function attach(BotInterface $bot, $matcher = null, $priority = 0) |
48
|
|
|
{ |
49
|
1 |
|
if (!$matcher && $bot instanceof MatcherAggregate) { |
50
|
|
|
$matcher = $bot->getMatcher(); |
51
|
|
|
} else { |
52
|
|
|
$matcher = function () { |
53
|
|
|
return true; |
54
|
1 |
|
}; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$this->cpu->addListener(Events::RECEIVED_COMMAND, $this->getListener($bot, $matcher), $priority); |
58
|
|
|
|
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param BotInterface $bot |
64
|
|
|
* @param MatcherInterface|callable $matcher If callable, it should accept a CommandInterface and return a boolean. |
65
|
|
|
* |
66
|
|
|
* @throws \Crummy\Phlack\Common\Exception\InvalidArgumentException When given an invalid matcher. |
67
|
|
|
* |
68
|
|
|
* @return \Closure An anonymous function to be attached to the internal cpu. |
69
|
|
|
*/ |
70
|
6 |
|
public function getListener(BotInterface $bot, $matcher) |
71
|
|
|
{ |
72
|
6 |
|
if (!$matcher instanceof MatcherInterface && !is_callable($matcher)) { |
73
|
1 |
|
throw new InvalidArgumentException(sprintf( |
74
|
1 |
|
'The matcher must be callable, or an instance of MatcherInterface. "%s" given.', |
75
|
1 |
|
is_object($matcher) ? get_class($matcher) : gettype($matcher) |
76
|
1 |
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
return function (Packet $packet) use ($bot, $matcher) { |
80
|
3 |
|
if ($matcher instanceof MatcherInterface) { |
81
|
2 |
|
$isMatch = $matcher->matches($packet['command']); |
82
|
2 |
|
} else { |
83
|
1 |
|
$isMatch = call_user_func($matcher, $packet['command']); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
if ($isMatch) { |
87
|
2 |
|
$packet->stopPropagation(); |
88
|
2 |
|
$packet['output'] = $bot->execute($packet['command']); |
89
|
2 |
|
} |
90
|
5 |
|
}; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.