SagaMethodMessageHandlerInspector   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 78.05%

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 11
c 7
b 1
f 1
lcom 1
cbo 6
dl 0
loc 91
ccs 32
cts 41
cp 0.7805
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getMessageHandlers() 0 41 6
B findHandlerMethod() 0 29 3
A getSagaType() 0 4 1
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * The software is based on the Axon Framework project which is
17
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
18
 * see <http://www.axonframework.org/>.
19
 * 
20
 * This software consists of voluntary contributions made by many individuals
21
 * and is licensed under the MIT license. For more information, see
22
 * <http://www.governor-framework.org/>.
23
 */
24
25
namespace Governor\Framework\Saga\Annotation;
26
27
use Doctrine\Common\Annotations\AnnotationReader;
28
use Governor\Framework\Domain\EventMessageInterface;
29
use Governor\Framework\Common\ReflectionUtils;
30
use Governor\Framework\Annotations\SagaEventHandler;
31
32
/**
33
 * The SagaMethodMessageHandlerInspector is using annotations to find the correct message handlers of a Saga.
34
 *
35
 * @author    "David Kalosi" <[email protected]>
36
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a>
37
 */
38
class SagaMethodMessageHandlerInspector
39
{
40
41
    private $targetSaga;
42
    private $reader;
43
44 40
    public function __construct($targetSaga)
45
    {
46 40
        $this->targetSaga = $targetSaga;
47 40
        $this->reader = new AnnotationReader();
48 40
    }
49
50
    // !!! TODO use the inspector for this
51 20
    public function getMessageHandlers(EventMessageInterface $event)
52
    {
53 20
        $found = array();
54 20
        $reflectionClass = ReflectionUtils::getClass($this->targetSaga);
55
56 20
        foreach (ReflectionUtils::getMethods($reflectionClass) as $method) {
57 20
            $annot = $this->reader->getMethodAnnotation(
58 20
                $method,
59
                SagaEventHandler::class
60 20
            );
61
62 20
            if (null === $annot) {
63 20
                continue;
64
            }
65
66 20
            if (0 === count($method->getParameters())) {
67
                throw new \RuntimeException(
68
                    sprintf(
69
                        "Invalid method signature detected of %s::%s. ".
70
                        "Methods annotated with @SagaEventHandler must have exatly one parameter with the type of the message they handle. ",
71
                        $reflectionClass->name,
72
                        $method->name
73
                    )
74
                );
75
            }
76
77 20
            $parameter = current($method->getParameters());
78
79 20
            if (null !== $parameter->getClass() &&
80 20
                $parameter->getClass()->name === $event->getPayloadType()
81 20
            ) {
82 18
                $found[] = SagaMethodMessageHandler::getInstance(
83 18
                    $event,
84 18
                    $method,
85
                    $annot
0 ignored issues
show
Unused Code introduced by
The call to SagaMethodMessageHandler::getInstance() has too many arguments starting with $annot.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
86 18
                );
87 18
            }
88 20
        }
89
90 20
        return $found;
91
    }
92
93 16
    public function findHandlerMethod(
94
        AbstractAnnotatedSaga $target,
95
        EventMessageInterface $event
96
    ) {
97 16
        foreach ($this->getMessageHandlers($event) as $handler) {
98 16
            $associationValue = $handler->getAssociationValue($event);
99 16
            if ($target->getAssociationValues()->contains($associationValue)) {
100 16
                return $handler;
101
            }
102 5
        }
103
104 5
        return SagaMethodMessageHandler::noHandlers();
105
        /*   for (SagaMethodMessageHandler handler : getMessageHandlers(event)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
          final AssociationValue associationValue = handler.getAssociationValue(event);
107
          if (target.getAssociationValues().contains(associationValue)) {
108
          return handler;
109
          } else if (logger.isDebugEnabled()) {
110
          logger.debug(
111
          "Skipping handler [{}], it requires an association value [{}:{}] that this Saga is not associated with",
112
          handler.getName(),
113
          associationValue.getKey(),
114
          associationValue.getValue());
115
          }
116
          }
117
          if (logger.isDebugEnabled()) {
118
          logger.debug("No suitable handler was found for event of type", event.getPayloadType().getName());
119
          }
120
          return SagaMethodMessageHandler.noHandler(); */
121
    }
122
123
    public function getSagaType()
124
    {
125
        return $this->sagaType;
0 ignored issues
show
Bug introduced by
The property sagaType does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
126
    }
127
128
}
129