Ejemplo n.º 1
0
 public function its_listener_can_accept_a_callable_as_a_matcher(BotInterface $bot, CommandInterface $command, Packet $packet)
 {
     $packet->offsetGet('command')->willReturn($command);
     $packet->offsetSet('output', null)->shouldBeCalled();
     $packet->stopPropagation()->shouldBeCalled();
     $bot->execute($command)->shouldBeCalled();
     $listener = $this->getListener($bot, function ($command) {
         return true;
     });
     $listener($packet);
 }
Ejemplo n.º 2
0
 /**
  * @param BotInterface              $bot
  * @param MatcherInterface|callable $matcher If callable, it should accept a CommandInterface and return a boolean.
  *
  * @throws \Crummy\Phlack\Common\Exception\InvalidArgumentException When given an invalid matcher.
  *
  * @return \Closure An anonymous function to be attached to the internal cpu.
  */
 public function getListener(BotInterface $bot, $matcher)
 {
     if (!$matcher instanceof MatcherInterface && !is_callable($matcher)) {
         throw new InvalidArgumentException(sprintf('The matcher must be callable, or an instance of MatcherInterface. "%s" given.', is_object($matcher) ? get_class($matcher) : gettype($matcher)));
     }
     return function (Packet $packet) use($bot, $matcher) {
         if ($matcher instanceof MatcherInterface) {
             $isMatch = $matcher->matches($packet['command']);
         } else {
             $isMatch = call_user_func($matcher, $packet['command']);
         }
         if ($isMatch) {
             $packet->stopPropagation();
             $packet['output'] = $bot->execute($packet['command']);
         }
     };
 }