MetaDataRoutingStrategy::doResolveRoutingKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * The software is based on the Axon Framework project which is
16
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
17
 * see <http://www.axonframework.org/>.
18
 *
19
 * This software consists of voluntary contributions made by many individuals
20
 * and is licensed under the MIT license. For more information, see
21
 * <http://www.governor-framework.org/>.
22
 */
23
24
namespace Governor\Framework\CommandHandling\Distributed;
25
26
use Governor\Framework\CommandHandling\CommandMessageInterface;
27
28
29
/**
30
 * RoutingStrategy implementation that uses the value in the MetaData of a CommandMessage assigned to a given key. The
31
 * value's <code>toString()</code> is used to convert the MetaData value to a String.
32
 *
33
 * @author Allard Buijze
34
 * @since 2.0
35
 */
36
class MetaDataRoutingStrategy extends AbstractRoutingStrategy
37
{
38
39
    /**
40
     * @var string
41
     */
42
    private $metaDataKey;
43
44
45
    /**
46
     * Initializes the MetaDataRoutingStrategy where the given <code>metaDataKey</code> is used to get the Meta Data
47
     * value. The given <code>unresolvedRoutingKeyPolicy</code> presecribes what to do when the Meta Data properties
48
     * cannot be found.
49
     *
50
     * @param string $metaDataKey The key on which the value is retrieved from the MetaData.
51
     * @param int $unresolvedRoutingKeyPolicy The policy prescribing behavior when the routing key cannot be resolved
52
     */
53
    public function __construct($metaDataKey, $unresolvedRoutingKeyPolicy = UnresolvedRoutingKeyPolicy::ERROR)
54
    {
55
        parent::__construct($unresolvedRoutingKeyPolicy);
56
        $this->metaDataKey = $metaDataKey;
57
    }
58
59
    /**
60
     * @param CommandMessageInterface $command
61
     * @return null|string
62
     */
63
    protected function doResolveRoutingKey(CommandMessageInterface $command)
64
    {
65
        $value = $command->getMetaData()->get($this->metaDataKey);
66
        return isset($value) ? (string)$value : null;
67
    }
68
}