Mainframe::getListener()   B
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 8.6737
cc 6
eloc 13
nc 2
nop 2
crap 6
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);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->cpu->dispa...CUTE_COMMAND, $packet); (Symfony\Component\EventDispatcher\Event) is incompatible with the return type declared by the interface Crummy\Phlack\Common\Executable::execute of type Crummy\Phlack\Common\Encodable.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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