AnnotationCommandTargetResolver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 26.96 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 10
c 5
b 1
f 1
lcom 1
cbo 5
dl 31
loc 115
ccs 44
cts 44
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveTarget() 0 20 2
B getAnnotatedTargetValue() 31 31 5
A findIdentifier() 0 10 1
A findVersion() 0 10 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;
26
27
use Governor\Framework\Common\Annotation\AnnotationReaderFactoryInterface;
28
use Governor\Framework\Annotations\TargetAggregateIdentifier;
29
use Governor\Framework\Annotations\TargetAggregateVersion;
30
use Governor\Framework\Common\ReflectionUtils;
31
32
/**
33
 * Implementation of the {@see CommandTargetResolverInterface} that uses annotations to resolve its target.
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 AnnotationCommandTargetResolver implements CommandTargetResolverInterface
39
{
40
41
    /**
42
     * @var \Doctrine\Common\Annotations\Reader
43
     */
44
    private $reader;
45
46
    /**
47
     * @param AnnotationReaderFactoryInterface $annotationReaderFactory
48
     */
49 4
    public function __construct(AnnotationReaderFactoryInterface $annotationReaderFactory)
50
    {
51 4
        $this->reader = $annotationReaderFactory->getReader();
52 4
    }
53
54
    /**
55
     *
56
     * @param CommandMessageInterface $command
57
     * @return VersionedAggregateIdentifier
58
     * @throws \InvalidArgumentException
59
     */
60 4
    public function resolveTarget(CommandMessageInterface $command)
61
    {
62 4
        $reflectionClass = new \ReflectionClass($command->getPayload());
63
64 4
        $id = $this->findIdentifier($command, $reflectionClass);
65 4
        $version = $this->findVersion($command, $reflectionClass);
66
67 4
        if (null === $id) {
68 1
            throw new \InvalidArgumentException(
69 1
                sprintf(
70
                    "Invalid command. It does not identify the target aggregate. ".
71 1
                    "Make sure at least one of the fields or methods in the [%s] class contains the ".
72 1
                    "@TargetAggregateIdentifier annotation and that it returns a non-null value.",
73 1
                    $command->getPayloadType()
74 1
                )
75 1
            );
76
        }
77
78 3
        return new VersionedAggregateIdentifier($id, $version);
79
    }
80
81
    /**
82
     * @param $annotationName
83
     * @param CommandMessageInterface $command
84
     * @param \ReflectionClass $reflectionClass
85
     * @return mixed|null
86
     */
87 4 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...
88
        $annotationName,
89
        CommandMessageInterface $command,
90
        \ReflectionClass $reflectionClass
91
    ) {
92 4
        foreach (ReflectionUtils::getProperties($reflectionClass) as $property) {
93 3
            if (null !== $annotation = $this->reader->getPropertyAnnotation(
94 3
                    $property,
95
                    $annotationName
96 3
                )
97 3
            ) {
98 1
                $property->setAccessible(true);
99
100 1
                return $property->getValue($command->getPayload());
101
            }
102 4
        }
103
104 3
        foreach (ReflectionUtils::getMethods($reflectionClass) as $method) {
105 2
            if (null !== $annotation = $this->reader->getMethodAnnotation(
106 2
                    $method,
107
                    $annotationName
108 2
                )
109 2
            ) {
110 2
                $method->setAccessible(true);
111
112 2
                return $method->invoke($command->getPayload());
113
            }
114 3
        }
115
116 1
        return null;
117
    }
118
119
    /**
120
     * @param CommandMessageInterface $command
121
     * @param \ReflectionClass $reflectionClass
122
     * @return mixed|null
123
     */
124 4
    private function findIdentifier(
125
        CommandMessageInterface $command,
126
        \ReflectionClass $reflectionClass
127
    ) {
128 4
        return $this->getAnnotatedTargetValue(
129 4
            TargetAggregateIdentifier::class,
130 4
            $command,
131
            $reflectionClass
132 4
        );
133
    }
134
135
136
    /**
137
     * @param CommandMessageInterface $command
138
     * @param \ReflectionClass $reflectionClass
139
     * @return mixed|null
140
     */
141 4
    private function findVersion(
142
        CommandMessageInterface $command,
143
        \ReflectionClass $reflectionClass
144
    ) {
145 4
        return $this->getAnnotatedTargetValue(
146 4
            TargetAggregateVersion::class,
147 4
            $command,
148
            $reflectionClass
149 4
        );
150
    }
151
152
}
153