Skip to content

Commit d89d681

Browse files
authored
Merge pull request #40 from UFOMelkor/feature/di-enhancements
Small DI Fixes/Enhancements
2 parents a4885a1 + a9a3107 commit d89d681

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/DependencyInjection/Compiler/RoutePass.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ public function process(ContainerBuilder $container)
4848
foreach ($handlers as $id => $args) {
4949
// Safeguard to have only one tag per command / query
5050
if ($type !== 'event' && count($args) > 1) {
51-
throw CompilerPassException::tagCountExceeded($id, $id, $bus);
51+
throw CompilerPassException::tagCountExceeded($type, $id, $bus);
5252
}
5353
foreach ($args as $eachArgs) {
5454
if ((! isset($eachArgs['message_detection']) || $eachArgs['message_detection'] !== true) && ! isset($eachArgs['message'])) {
5555
throw CompilerPassException::messageTagMissing($id);
5656
}
5757

58-
$messageNames = isset($eachArgs['message']) ? [$eachArgs['message']] : $this->recognizeMessageNames($container, $container->getDefinition($id));
58+
$messageNames = isset($eachArgs['message'])
59+
? [$eachArgs['message']]
60+
: $this->recognizeMessageNames($container, $container->getDefinition($id), $id, $type);
5961

6062
if ($type === 'event') {
6163
$routerArguments[0] = array_merge_recursive(
@@ -84,9 +86,16 @@ public function process(ContainerBuilder $container)
8486
}
8587
}
8688

87-
private function recognizeMessageNames(ContainerBuilder $container, Definition $routeDefinition): array
88-
{
89+
private function recognizeMessageNames(
90+
ContainerBuilder $container,
91+
Definition $routeDefinition,
92+
string $routeId,
93+
string $busType
94+
): array {
8995
$handlerReflection = $container->getReflectionClass($routeDefinition->getClass());
96+
if (! $handlerReflection) {
97+
throw CompilerPassException::unknownHandlerClass($routeDefinition->getClass(), $routeId, $busType);
98+
}
9099

91100
$methodsWithMessageParameter = array_filter(
92101
$handlerReflection->getMethods(ReflectionMethod::IS_PUBLIC),

src/Exception/CompilerPassException.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,34 @@
44

55
namespace Prooph\Bundle\ServiceBus\Exception;
66

7-
class CompilerPassException extends \RuntimeException
7+
class CompilerPassException extends RuntimeException
88
{
99
public static function messageTagMissing(string $serviceId): self
1010
{
11-
throw new self(
12-
sprintf('The "message" tag key is missing from tag. Either provide a "message" tag or enable "message_detection" for service "%s"',
13-
$serviceId
14-
)
15-
);
11+
return new self(sprintf(
12+
'The "message" tag key is missing from tag. '
13+
. 'Either provide a "message" tag or enable "message_detection" for service "%s"',
14+
$serviceId
15+
));
1616
}
1717

1818
public static function tagCountExceeded(string $type, string $serviceId, string $busName): self
1919
{
20-
throw new self(
21-
sprintf(
22-
'More than 1 %s handler tagged on service "%s" with tag "%s". Only events can have multiple handlers',
23-
$type, $serviceId, $busName
24-
)
25-
);
20+
return new self(sprintf(
21+
'More than 1 %s handler tagged on service "%s" with tag "%s". Only events can have multiple handlers',
22+
$type,
23+
$serviceId,
24+
$busName
25+
));
26+
}
27+
28+
public static function unknownHandlerClass(string $className, string $serviceId, string $busName): self
29+
{
30+
return new self(sprintf(
31+
'Service %s has been tagged as %s handler, but its class %s does not exist',
32+
$serviceId,
33+
$busName,
34+
$className
35+
));
2636
}
2737
}

0 commit comments

Comments
 (0)