ActionManager::handleRaw()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Buttress\IRC\Action;
3
4
use Buttress\IRC\Connection\ConnectionInterface;
5
use Buttress\IRC\Message\MessageFactory;
6
7
class ActionManager implements ActionManagerInterface
8
{
9
10
    /**
11
     * @var MessageFactory
12
     */
13
    protected $factory;
14
15
    /**
16
     * @var array
17
     */
18
    protected $actions = array(
19
        "*" => array()
20
    );
21
22
    public function __construct(
23
        MessageFactory $factory,
24
        ActionInterface $ping_action = null,
25
        ActionInterface $error_action = null
26
    ) {
27
        $this->factory = $factory;
28
29
        $this->add('ping', $ping_action ?: new PingAction());
30
        $this->add('error', $error_action ?: new ErrorAction());
31
    }
32
33
    public function add($command, ActionInterface $action)
34
    {
35
        $this->actions[strtoupper($command)][] = $action;
36
    }
37
38
    public function handleConnect(ConnectionInterface $connection)
39
    {
40
        foreach ($this->actions as $action_group) {
41
            /** @var ActionInterface $action */
42
            foreach ($action_group as $action) {
43
                $action->handleConnect($connection);
44
            }
45
        }
46
    }
47
48
    /**
49
     * @param ConnectionInterface $connection
50
     * @param string              $string
51
     * @return void
52
     */
53
    public function handleRaw(ConnectionInterface $connection, $string)
54
    {
55
        $message = $this->factory->getMessageFromRaw($string);
56
        $message->setConnection($connection);
57
58
        foreach ($this->getActions($message->getCommand()) as $action) {
59
            $action->handleMessage($message);
60
        }
61
62
        foreach ($this->getActions('*') as $action) {
63
            $action->handleMessage($message);
64
        }
65
    }
66
67
    public function handleTick(ConnectionInterface $connection)
68
    {
69
        foreach ($this->getActions('TICK') as $action) {
70
            $action->handleTick($connection);
71
        }
72
    }
73
74
    /**
75
     * @param string $command
76
     * @return ActionInterface[]
77
     */
78
    protected function getActions($command)
79
    {
80
        return isset($this->actions[$command]) ? $this->actions[$command] : array();
81
    }
82
83
}
84