AnnotationRoutingStrategy   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 36.9 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 5
dl 31
loc 84
ccs 20
cts 30
cp 0.6667
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getAnnotatedTargetValue() 31 31 5
A findIdentifier() 0 10 1
A doResolveRoutingKey() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\CommandHandling\Distributed;
26
27
use Governor\Framework\Common\Annotation\AnnotationReaderFactoryInterface;
28
use Governor\Framework\Common\ReflectionUtils;
29
use Governor\Framework\Annotations\TargetAggregateIdentifier;
30
use Governor\Framework\CommandHandling\CommandMessageInterface;
31
32
class AnnotationRoutingStrategy extends AbstractRoutingStrategy
33
{
34
    /**
35
     * @var \Doctrine\Common\Annotations\Reader
36
     */
37
    private $reader;
38
39
    /**
40
     * @param AnnotationReaderFactoryInterface $annotationReaderFactory
41
     * @param int $unresolvedRoutingKeyPolicy
42
     */
43 3
    public function __construct(
44
        AnnotationReaderFactoryInterface $annotationReaderFactory,
45
        $unresolvedRoutingKeyPolicy = UnresolvedRoutingKeyPolicy::ERROR
46
    ) {
47 3
        parent::__construct($unresolvedRoutingKeyPolicy);
48 3
        $this->reader = $annotationReaderFactory->getReader();
49 3
    }
50
51
    /**
52
     * @param $annotationName
53
     * @param CommandMessageInterface $command
54
     * @param \ReflectionClass $reflectionClass
55
     * @return mixed|null
56
     */
57 3 View Code Duplication
    private function getAnnotatedTargetValue(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
        $annotationName,
59
        CommandMessageInterface $command,
60
        \ReflectionClass $reflectionClass
61
    ) {
62 3
        foreach (ReflectionUtils::getProperties($reflectionClass) as $property) {
63 3
            if (null !== $annotation = $this->reader->getPropertyAnnotation(
64 3
                    $property,
65
                    $annotationName
66 3
                )
67 3
            ) {
68 3
                $property->setAccessible(true);
69
70 3
                return $property->getValue($command->getPayload());
71
            }
72
        }
73
74
        foreach (ReflectionUtils::getMethods($reflectionClass) as $method) {
75
            if (null !== $annotation = $this->reader->getMethodAnnotation(
76
                    $method,
77
                    $annotationName
78
                )
79
            ) {
80
                $method->setAccessible(true);
81
82
                return $method->invoke($command->getPayload());
83
            }
84
        }
85
86
        return null;
87
    }
88
89
    /**
90
     * @param CommandMessageInterface $command
91
     * @param \ReflectionClass $reflectionClass
92
     * @return mixed|null
93
     */
94 3
    private function findIdentifier(
95
        CommandMessageInterface $command,
96
        \ReflectionClass $reflectionClass
97
    ) {
98 3
        return $this->getAnnotatedTargetValue(
99 3
            TargetAggregateIdentifier::class,
100 3
            $command,
101
            $reflectionClass
102 3
        );
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 3
    protected function doResolveRoutingKey(CommandMessageInterface $command)
109
    {
110 3
        $reflectionClass = new \ReflectionClass($command->getPayload());
111
112 3
        return $this->findIdentifier($command, $reflectionClass);
113
    }
114
115
}