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.
Completed
Push — bugfix/5.4build ( 31a631 )
by Szurovecz
08:38
created

DirectCommandBus::forwardCommand()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 33
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 8.5806
cc 4
eloc 27
nc 6
nop 1
1
<?php
2
/*
3
 * Copyright (c) 2012-2014 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\commandhandling;
25
26
use ArrayObject;
27
use Exception;
28
use precore\lang\ObjectClass;
29
use precore\util\Preconditions;
30
use predaddy\domain\GenericAggregateId;
31
use predaddy\domain\Repository;
32
use predaddy\domain\StateHashAware;
33
use predaddy\messagehandling\ClosureWrapper;
34
use predaddy\messagehandling\MessageHandlerDescriptorFactory;
35
use predaddy\messagehandling\util\SimpleMessageCallback;
36
37
/**
38
 * This class acts as a {@link CommandBus} expect one case. If the posted command is a {@link DirectCommand}
39
 * and there is no registered handler which could process that, it will load the appropriate AR from the given
40
 * repository and passes the command to that. This bus should be used if business method parameters
41
 * in the aggregates are {@link Command} objects.
42
 *
43
 * If you have specialized repositories for your aggregates, it is recommended to use {@link RepositoryDelegate}.
44
 *
45
 * @author Janos Szurovecz <[email protected]>
46
 */
47
class DirectCommandBus extends CommandBus
0 ignored issues
show
Complexity introduced by
The class DirectCommandBus has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13.
Loading history...
48
{
49
    /**
50
     * @var Repository
51
     */
52
    private $repository;
53
54
    /**
55
     * @var MessageHandlerDescriptorFactory
56
     */
57
    private $handlerDescriptorFactory;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
58
59
    /**
60
     * @param DirectCommandBusBuilder $builder
61
     */
62
    public function __construct(DirectCommandBusBuilder $builder)
63
    {
64
        parent::__construct($builder);
65
        $this->repository = $builder->getRepository();
66
        $this->handlerDescriptorFactory = $builder->getHandlerDescriptorFactory();
67
    }
68
69
    /**
70
     * The given repository cannot be null, the default value is due to PHP restrictions.
71
     *
72
     * @param Repository $repository Is being passed to the registered DirectCommandForwarder
73
     * @return DirectCommandBusBuilder
74
     */
75
    public static function builder(Repository $repository = null)
76
    {
77
        return new DirectCommandBusBuilder(Preconditions::checkNotNull($repository));
78
    }
79
80
    protected function callableWrappersFor($message)
81
    {
82
        $wrappers = parent::callableWrappersFor($message);
83
        if (($message instanceof DirectCommand) && $wrappers->count() === 0) {
84
            $wrappers = new ArrayObject([new ClosureWrapper(
85
                function (DirectCommand $command) {
86
                    return $this->forwardCommand($command);
87
                }
88
            )]);
89
        }
90
        return $wrappers;
91
    }
92
93
    /**
94
     * @param DirectCommand $command
95
     * @throws \Exception If the handler throws any
96
     * @return mixed The return value of the last handler (should be one handler per aggregate)
97
     */
98
    private function forwardCommand(DirectCommand $command)
99
    {
100
        $aggregateClass = $command->aggregateClass();
101
        $aggregateId = $command->aggregateId();
102
        if ($aggregateId === null) {
103
            $aggregate = ObjectClass::forName($aggregateClass)->newInstanceWithoutConstructor();
104
            self::getLogger()->debug('New aggregate [{}] has been created', [$aggregateClass]);
105
        } else {
106
            $aggregate = $this->repository->load(new GenericAggregateId($aggregateId, $aggregateClass));
107
            self::getLogger()->debug(
108
                'Aggregate [{}] with ID [{}] has been successfully loaded',
109
                [$aggregateClass, $aggregateId]
110
            );
111
            if ($command instanceof StateHashAware) {
112
                $aggregate->failWhenStateHashViolation($command->stateHash());
113
            }
114
        }
115
        $forwarderBus = CommandBus::builder()
116
            ->withIdentifier($aggregateClass)
117
            ->withHandlerDescriptorFactory($this->handlerDescriptorFactory)
118
            ->build();
119
        $forwarderBus->register($aggregate);
120
        $callback = new SimpleMessageCallback();
121
        $forwarderBus->post($command, $callback);
122
        $thrownException = $callback->getException();
123
        if ($thrownException instanceof Exception) {
124
            self::getLogger()->debug('Error occurred when command has been applied [{}]', [$command], $thrownException);
125
            throw $thrownException;
126
        }
127
        $this->repository->save($aggregate);
128
        self::getLogger()->info("Command [{}] has been applied", [$command]);
129
        return $callback->getResult();
130
    }
131
}
132