|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 SURFnet bv |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddlewareClientBundle\Service; |
|
20
|
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
|
22
|
|
|
use Surfnet\StepupMiddlewareClient\Exception\CommandExecutionFailedException; |
|
23
|
|
|
use Surfnet\StepupMiddlewareClient\Service\CommandService as LibraryCommandService; |
|
24
|
|
|
use Surfnet\StepupMiddlewareClient\Service\ExecutionResult; |
|
25
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Command\Command; |
|
26
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Command\Metadata; |
|
27
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidArgumentException; |
|
28
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Uuid\Uuid; |
|
29
|
|
|
|
|
30
|
|
|
class CommandService |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var LibraryCommandService |
|
34
|
|
|
*/ |
|
35
|
|
|
private $commandService; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var LoggerInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $logger; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param LibraryCommandService $commandService |
|
44
|
|
|
* @param LoggerInterface $logger |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(LibraryCommandService $commandService, LoggerInterface $logger) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->commandService = $commandService; |
|
49
|
|
|
$this->logger = $logger; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Command $command |
|
54
|
|
|
* @param Metadata $metadata |
|
55
|
|
|
* @return ExecutionResult |
|
56
|
|
|
*/ |
|
57
|
|
|
public function execute(Command $command, Metadata $metadata) |
|
58
|
|
|
{ |
|
59
|
|
|
$commandName = $this->getCommandName($command); |
|
60
|
|
|
$payload = $command->serialise(); |
|
61
|
|
|
$metadataPayload = $metadata->serialise(); |
|
62
|
|
|
|
|
63
|
|
|
// Only set the command's UUID if it hasn't already been set. Allows pre-setting of UUID, if needed. |
|
64
|
|
|
if (!$command->getUuid()) { |
|
65
|
|
|
$command->setUuid(Uuid::generate()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->logger->info(sprintf("Command '%s' with UUID '%s' is executing", $commandName, $command->getUuid())); |
|
69
|
|
|
|
|
70
|
|
|
try { |
|
71
|
|
|
$result = $this->commandService->execute($commandName, $command->getUuid(), $payload, $metadataPayload); |
|
72
|
|
|
|
|
73
|
|
|
if ($result->isSuccessful()) { |
|
74
|
|
|
$this->logger->info(sprintf( |
|
75
|
|
|
"Command '%s' with UUID '%s' was processed successfully by '%s'", |
|
76
|
|
|
$commandName, |
|
77
|
|
|
$command->getUuid(), |
|
78
|
|
|
$result->getProcessedBy() |
|
79
|
|
|
)); |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->logger->warning( |
|
82
|
|
|
sprintf( |
|
83
|
|
|
"Command '%s' with UUID '%s' could not be executed (%s)", |
|
84
|
|
|
$commandName, |
|
85
|
|
|
$command->getUuid(), |
|
86
|
|
|
join('; ', $result->getErrors()) |
|
87
|
|
|
) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
} catch (CommandExecutionFailedException $e) { |
|
91
|
|
|
$this->logger->error( |
|
92
|
|
|
sprintf( |
|
93
|
|
|
"Command '%s' with UUID '%s' could not be executed (%s)", |
|
94
|
|
|
$commandName, |
|
95
|
|
|
$command->getUuid(), |
|
96
|
|
|
$e->getMessage() |
|
97
|
|
|
), |
|
98
|
|
|
['exception' => $e] |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$result = new ExecutionResult(null, null, [$e->getMessage()]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $result; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param Command $command |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
private function getCommandName(Command $command) |
|
112
|
|
|
{ |
|
113
|
|
|
if (!preg_match('~(\\w+)\\\\Command\\\\((\\w+\\\\)*\\w+)Command$~', get_class($command), $commandNameParts)) { |
|
114
|
|
|
throw new InvalidArgumentException( |
|
115
|
|
|
"Given command's class name cannot be expressed using command name notation." |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$commandName = sprintf('%s:%s', $commandNameParts[1], str_replace('\\', '.', $commandNameParts[2])); |
|
120
|
|
|
|
|
121
|
|
|
return $commandName; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|