MessageFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A getMessageFromRaw() 0 7 1
B deconstructRaw() 0 27 4
A make() 0 15 3
A constructPrivmsg() 0 4 1
A constructPing() 0 4 1
1
<?php
2
namespace Buttress\IRC\Message;
3
4
use Buttress\IRC\Connection\ConnectionInterface;
5
6
class MessageFactory
7
{
8
9
    protected $constructors;
10
11
    public function register($command, callable $generator)
12
    {
13
        $this->constructors[$command] = $generator;
14
    }
15
16
    /**
17
     * @param string              $raw
18
     * @param ConnectionInterface $connection
19
     * @return MessageInterface
20
     */
21
    public function getMessageFromRaw($raw, ConnectionInterface $connection = null)
22
    {
23
        list($prefix, $command, $params) = $this->deconstructRaw($raw);
24
        $message = $this->make($command, $prefix, $params, $connection);
25
26
        return $message;
27
    }
28
29
    /**
30
     * @param string $raw_message
31
     */
32
    public function deconstructRaw($raw_message)
33
    {
34
        $prefix = "";
35
36
        if (substr($raw_message, 0, 1) == ':') {
37
            $prefix_end = strpos($raw_message, ' ');
38
            $prefix = substr($raw_message, 1, $prefix_end - 1);
39
            $raw = trim(substr($raw_message, $prefix_end + 1));
40
        } else {
41
            $raw = trim($raw_message);
42
        }
43
44
        $last_param = null;
45
        if ($last_param_pos = strpos($raw, ' :')) {
46
            $last_param = substr($raw, $last_param_pos + 2);
47
            $raw = substr($raw, 0, $last_param_pos);
48
        }
49
50
        $exploded = explode(' ', $raw);
51
        $command = array_shift($exploded);
52
53
        if ($last_param) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $last_param of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
54
            $exploded[] = $last_param;
55
        }
56
57
        return array($prefix, $command, $exploded);
58
    }
59
60
    /**
61
     * @param string              $command
62
     * @param string              $prefix
63
     * @param array               $params
64
     * @param ConnectionInterface $connection
65
     * @return GenericMessage
66
     */
67
    public function make($command, $prefix = "", array $params = array(), ConnectionInterface $connection = null)
68
    {
69
        $command = strtoupper($command);
70
71
        if (isset($this->constructors[$command])) {
72
            return $this->constructors[$command]($command, $prefix, $params, $connection);
73
        } else {
74
            $method = "construct" . substr($command, 0, 1) . substr($command, 1);
75
            if (method_exists($this, $method)) {
76
                return $this->{$method}($command, $prefix, $params, $connection);
77
            }
78
        }
79
80
        return new GenericMessage($command, $prefix, $params, $connection);
81
    }
82
83
    protected function constructPrivmsg($command, $prefix, $params, $connection)
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        return new PrivmsgMessage($prefix, $params, $connection);
86
    }
87
88
    protected function constructPing($command, $prefix, $params, $connection)
89
    {
90
        return new PingMessage($command, $prefix, $params, $connection);
91
    }
92
93
}
94