MessageMapperDispatcher   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A dispatch() 0 9 1
A assertThatMethodExist() 0 12 2
1
<?php
2
/*
3
 * This file is part of the Borobudur-Bus package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Bus\Message;
12
13
use Borobudur\Bus\Exception\InvalidArgumentException;
14
use ReflectionObject;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     10/12/15
19
 */
20
class MessageMapperDispatcher
21
{
22
    /**
23
     * @var array
24
     */
25
    private $data = array();
26
    
27
    /**
28
     * @var mixed
29
     */
30
    private $object;
31
    
32
    /**
33
     * @var ReflectionObject
34
     */
35
    private $reflection;
36
    
37
    /**
38
     * Constructor.
39
     *
40
     * @param mixed $data
41
     * @param mixed $object
42
     */
43
    public function __construct($data, $object)
44
    {
45
        $this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        $this->object = $object;
47
        $this->reflection = new ReflectionObject($object);
48
    }
49
    
50
    /**
51
     * Dispatch a method.
52
     *
53
     * @param string $method
54
     *
55
     * @return mixed
56
     */
57
    public function dispatch($method)
58
    {
59
        $this->assertThatMethodExist($method);
60
        
61
        $method = $this->reflection->getMethod($method);
62
        $arguments = MessageMapperBuilder::normalizeParameters($method->getParameters(), $this->data);
63
        
64
        return $method->invokeArgs($this->object, $arguments);
65
    }
66
    
67
    /**
68
     * Assert that method is exist.
69
     *
70
     * @param string $method
71
     */
72
    private function assertThatMethodExist($method)
73
    {
74
        if (!method_exists($this->object, $method)) {
75
            throw new InvalidArgumentException(
76
                sprintf(
77
                    'Method "%s" on class "%s" not exist.',
78
                    $method,
79
                    get_class($this->object)
80
                )
81
            );
82
        }
83
    }
84
}
85