GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Mf4PhpMessageBus   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 8
dl 0
loc 93
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A builder() 0 4 1
A registerObjectMessageFactory() 0 4 1
A onMessage() 0 9 2
A findObjectMessageFactory() 0 10 3
A dispatch() 0 9 2
1
<?php
2
/*
3
 * Copyright (c) 2013 Janos Szurovecz
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6
 * this software and associated documentation files (the "Software"), to deal in
7
 * the Software without restriction, including without limitation the rights to
8
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is furnished to do
10
 * so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in all
13
 * copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
 * SOFTWARE.
22
 */
23
24
namespace predaddy\messagehandling\mf4php;
25
26
use InvalidArgumentException;
27
use mf4php\DefaultQueue;
28
use mf4php\Message as Mf4phpMessage;
29
use mf4php\MessageDispatcher;
30
use mf4php\MessageListener;
31
use mf4php\ObjectMessage;
32
use precore\lang\NullPointerException;
33
use precore\util\Preconditions;
34
use predaddy\messagehandling\MessageCallback;
35
use predaddy\messagehandling\SimpleMessageBus;
36
use Serializable;
37
38
/**
39
 * {@link MessageBus} implementation which uses mf4php to forward messages.
40
 *
41
 * If you use a proper MessageDispatcher it is possible to
42
 * handle messages after the transaction has been committed.
43
 *
44
 * With an asynchronous {@link MessageDispatcher} implementation message
45
 * handling can be asynchronous.
46
 *
47
 * Do not register it to the given {@link MessageDispatcher},
48
 * it is handled by default.
49
 *
50
 * Does not support {@link MessageCallback}!
51
 *
52
 * @author Janos Szurovecz <[email protected]>
53
 */
54
class Mf4PhpMessageBus extends SimpleMessageBus implements MessageListener
0 ignored issues
show
Complexity introduced by
The class Mf4PhpMessageBus has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13.
Loading history...
55
{
56
    private $dispatcher;
57
    private $queue;
58
    private $objectMessageFactories = [];
59
    private $defaultObjectMessageFactory;
60
61
    /**
62
     * @param Mf4PhpMessageBusBuilder $builder
63
     */
64
    public function __construct(Mf4PhpMessageBusBuilder $builder)
65
    {
66
        parent::__construct($builder);
67
        $this->dispatcher = $builder->getDispatcher();
68
        $this->queue = new DefaultQueue($builder->getIdentifier());
69
        $this->defaultObjectMessageFactory = new DefaultObjectMessageFactory();
70
        $this->dispatcher->addListener($this->queue, $this);
71
    }
72
73
    /**
74
     * @param MessageDispatcher $dispatcher the default value is due to PHP restrictions
75
     * @return Mf4PhpMessageBusBuilder
76
     * @throws NullPointerException if $dispatcher is null
77
     */
78
    public static function builder(MessageDispatcher $dispatcher = null)
79
    {
80
        return new Mf4PhpMessageBusBuilder(Preconditions::checkNotNull($dispatcher));
81
    }
82
83
    /**
84
     * Only full matching class names are used, you should not register
85
     * a factory for an abstract message class/interface!
86
     *
87
     * If there is no registered factory for a particular message class,
88
     * DefaultObjectMessageFactory will be used.
89
     *
90
     * @param ObjectMessageFactory $factory
91
     * @param $messageClass
92
     */
93
    public function registerObjectMessageFactory(ObjectMessageFactory $factory, $messageClass)
94
    {
95
        $this->objectMessageFactories[$messageClass] = $factory;
96
    }
97
98
    /**
99
     * Forward incoming message to handlers.
100
     *
101
     * @param Mf4phpMessage $message
102
     * @throws InvalidArgumentException
103
     */
104
    public function onMessage(Mf4phpMessage $message)
105
    {
106
        Preconditions::checkArgument($message instanceof ObjectMessage, "Message must be an instance of ObjectMessage");
107
        $object = $message->getObject();
108
        if ($object instanceof MessageWrapper) {
109
            $object = $object->getMessage();
110
        }
111
        parent::dispatch($object, self::emptyCallback());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (dispatch() instead of onMessage()). Are you sure this is correct? If so, you might want to change this to $this->dispatch().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
112
    }
113
114
    /**
115
     * Finds the appropriate message factory for the given message.
116
     *
117
     * @param $message
118
     * @return ObjectMessageFactory
119
     */
120
    protected function findObjectMessageFactory($message)
121
    {
122
        $messageClass = get_class($message);
123
        foreach ($this->objectMessageFactories as $class => $factory) {
124
            if ($class === $messageClass) {
125
                return $factory;
126
            }
127
        }
128
        return $this->defaultObjectMessageFactory;
129
    }
130
131
    /**
132
     * Send the message to the message queue.
133
     *
134
     * @param $message
135
     * @param MessageCallback $callback
136
     */
137
    protected function dispatch($message, MessageCallback $callback)
138
    {
139
        $sendable = $message;
140
        if (!($message instanceof Serializable)) {
141
            $sendable = new MessageWrapper($message);
142
        }
143
        $mf4phpMessage = $this->findObjectMessageFactory($message)->createMessage($sendable);
144
        $this->dispatcher->send($this->queue, $mf4phpMessage);
145
    }
146
}
147