Skip to content

Commit f70a728

Browse files
committed
Refactor the consumed config keys to use CommandBusInterface::class instead of ConfigProvider::class
Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent 3f1017f commit f70a728

6 files changed

Lines changed: 36 additions & 32 deletions

File tree

src/CommandHandlerResolver.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
use Override;
88
use Psr\Container\ContainerInterface;
9+
use Webware\CommandBus\CommandBusInterface;
910
use Webware\CommandBus\CommandInterface;
1011
use Webware\CommandBus\ConfigProvider;
1112
use Webware\CommandBus\Exception\InvalidConfigurationException;
1213

1314
use function array_key_exists;
1415

1516
/**
16-
* @phpstan-import-type CmdBusConfig from ConfigProvider
17+
* @phpstan-import-type CommandBusConfig from ConfigProvider
1718
* @phpstan-import-type CommandMap from ConfigProvider
1819
*/
1920
final class CommandHandlerResolver implements CommandHandlerResolverInterface
@@ -31,10 +32,10 @@ public function __invoke(CommandInterface $command): CommandHandlerInterface
3132
#[Override]
3233
public function resolve(CommandInterface $command): CommandHandlerInterface
3334
{
34-
/** @phpstan-var array<CmdBusConfig> */
35+
/** @phpstan-var array<CommandBusConfig> */
3536
$config = $this->container->get('config');
36-
/** @phpstan-var CmdBusConfig $config */
37-
$config = $config[ConfigProvider::class] ?? [];
37+
/** @phpstan-var CommandBusConfig $config */
38+
$config = $config[CommandBusInterface::class] ?? [];
3839
/** @phpstan-var CommandMap $map */
3940
$map = $config[ConfigProvider::COMMAND_MAP_KEY] ?? [];
4041
if (! array_key_exists($command::class, $map)) {

src/ConfigProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* priority: int
1616
* }
1717
* @phpstan-type MiddlewarePipeSpec array<MiddlewareSpec>
18-
* @phpstan-type CmdBusConfig array{
18+
* @phpstan-type CommandBusConfig array{
1919
* command-map: CommandMap,
2020
* middleware_pipeline: MiddlewarePipeSpec
2121
* }
@@ -73,14 +73,14 @@ final class ConfigProvider
7373
/**
7474
* @phpstan-return array{
7575
* dependencies: ServiceManagerConfiguration,
76-
* Webware\CommandBus\ConfigProvider: CmdBusConfig,
76+
* Webware\CommandBus\CommandBusInterface: CommandBusConfig,
7777
* }
7878
*/
7979
public function __invoke(): array
8080
{
8181
return [
82-
'dependencies' => $this->getDependencies(),
83-
static::class => [
82+
'dependencies' => $this->getDependencies(),
83+
CommandBusInterface::class => [
8484
self::COMMAND_MAP_KEY => $this->getCommandMap(),
8585
self::MIDDLEWARE_PIPELINE_KEY => $this->getMiddleware(),
8686
],

src/Container/MiddlewarePipeFactory.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Psr\Container\ContainerInterface;
88
use SplPriorityQueue;
9+
use Webware\CommandBus\CommandBusInterface;
910
use Webware\CommandBus\ConfigProvider;
1011
use Webware\CommandBus\Exception;
1112
use Webware\CommandBus\MiddlewareInterface;
@@ -17,7 +18,7 @@
1718
use function sprintf;
1819

1920
/**
20-
* @phpstan-import-type CmdBusConfig from ConfigProvider
21+
* @phpstan-import-type CommandBusConfig from ConfigProvider
2122
* @phpstan-import-type MiddlewarePipeSpec from ConfigProvider
2223
* @phpstan-import-type MiddlewareSpec from ConfigProvider
2324
* @phpstan-import-type CommandMap from ConfigProvider
@@ -30,16 +31,16 @@ public function __invoke(ContainerInterface $container): MiddlewarePipelineInter
3031
throw Exception\ServiceNotFoundException::fromService('config');
3132
}
3233

33-
/** @phpstan-var array<CmdBusConfig> $config */
34+
/** @phpstan-var array<CommandBusConfig> $config */
3435
$config = $container->get('config');
35-
/** @phpstan-var CmdBusConfig $config */
36-
$config = $config[ConfigProvider::class] ?? [];
36+
/** @phpstan-var CommandBusConfig $config */
37+
$config = $config[CommandBusInterface::class] ?? [];
3738

3839
if ($config === []) {
3940
throw Exception\InvalidConfigurationException::fromMissingKey(
4041
sprintf(
4142
'Configuration for key: %s was not found in the config service.',
42-
'$config[' . ConfigProvider::class . ']'
43+
'$config[' . CommandBusInterface::class . ']'
4344
)
4445
);
4546
}
@@ -56,9 +57,9 @@ public function __invoke(ContainerInterface $container): MiddlewarePipelineInter
5657
}
5758

5859
/**
59-
* Pipe middleware into the CmdBus middleware pipeline.
60+
* Pipe middleware into the CommandBus middleware pipeline.
6061
*
61-
* @phpstan-param CmdBusConfig $config
62+
* @phpstan-param CommandBusConfig $config
6263
*/
6364
private static function pipeMiddleware(
6465
ContainerInterface $container,
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @phpstan-import-type CmdBusConfig from ConfigProvider
2626
* @phpstan-import-type CommandMap from ConfigProvider
2727
*/
28-
final class CmdBusTest extends TestCase
28+
final class CommandBusTest extends TestCase
2929
{
3030
private ContainerInterface&ServiceManager $container;
3131

@@ -40,10 +40,10 @@ protected function setUp(): void
4040
TestAssets\TestMiddlewareFirst::class => InvokableFactory::class,
4141
TestAssets\TestMiddlewareSecond::class => InvokableFactory::class,
4242
];
43-
$config[ConfigProvider::class][ConfigProvider::COMMAND_MAP_KEY] = [
43+
$config[CommandBusInterface::class][ConfigProvider::COMMAND_MAP_KEY] = [
4444
TestAssets\Command::class => TestAssets\CommandHandler::class,
4545
];
46-
$middleware = $config[ConfigProvider::class][ConfigProvider::MIDDLEWARE_PIPELINE_KEY];
46+
$middleware = $config[CommandBusInterface::class][ConfigProvider::MIDDLEWARE_PIPELINE_KEY];
4747
$testMiddleware = [
4848
[
4949
'middleware' => TestAssets\TestMiddlewareFirst::class,
@@ -54,11 +54,11 @@ protected function setUp(): void
5454
'priority' => -1,
5555
],
5656
];
57-
$config[ConfigProvider::class][ConfigProvider::MIDDLEWARE_PIPELINE_KEY] = array_merge(
57+
$config[CommandBusInterface::class][ConfigProvider::MIDDLEWARE_PIPELINE_KEY] = array_merge(
5858
$middleware,
5959
$testMiddleware
6060
);
61-
$dependencies['services']['config'] = $config;
61+
$dependencies['services']['config'] = $config;
6262

6363
// @phpstan-ignore-next-line
6464
$this->container = new ServiceManager($dependencies);

test/unit/ConfigProviderTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\Attributes\CoversMethod;
99
use PHPUnit\Framework\TestCase;
10+
use Webware\CommandBus\CommandBusInterface;
1011
use Webware\CommandBus\ConfigProvider;
1112
use Webware\CommandBus\Middleware\CommandHandlerMiddleware;
1213

@@ -42,13 +43,13 @@ public function testInvokeReturnsCorrectStructure(): void
4243
$config = ($this->configProvider)();
4344

4445
$this->assertArrayHasKey('dependencies', $config);
45-
$this->assertArrayHasKey(ConfigProvider::class, $config);
46+
$this->assertArrayHasKey(CommandBusInterface::class, $config);
4647

4748
$dependencies = $config['dependencies'];
4849
$this->assertArrayHasKey('aliases', $dependencies);
4950
$this->assertArrayHasKey('factories', $dependencies);
5051

51-
$cmdBusConfig = $config[ConfigProvider::class];
52+
$cmdBusConfig = $config[CommandBusInterface::class];
5253
$this->assertArrayHasKey(ConfigProvider::COMMAND_MAP_KEY, $cmdBusConfig);
5354
$this->assertArrayHasKey(ConfigProvider::MIDDLEWARE_PIPELINE_KEY, $cmdBusConfig);
5455
}

test/unit/Container/MiddlewarePipeFactoryTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPUnit\Framework\TestCase;
1010
use Psr\Container\ContainerInterface;
1111
use ReflectionClass;
12+
use Webware\CommandBus\CommandBusInterface;
1213
use Webware\CommandBus\ConfigProvider;
1314
use Webware\CommandBus\Container\MiddlewarePipeFactory;
1415
use Webware\CommandBus\Exception\InvalidConfigurationException;
@@ -44,7 +45,7 @@ protected function setUp(): void
4445
public function testInvokeReturnsMiddlewarePipelineInterface(): void
4546
{
4647
$config = [
47-
ConfigProvider::class => [
48+
CommandBusInterface::class => [
4849
ConfigProvider::MIDDLEWARE_PIPELINE_KEY => [],
4950
],
5051
];
@@ -81,7 +82,7 @@ public function testInvokeThrowsExceptionWhenConfigServiceNotFound(): void
8182
($this->factory)($this->container);
8283
}
8384

84-
public function testInvokeThrowsExceptionWhenConfigProviderKeyMissing(): void
85+
public function testInvokeThrowsExceptionWhenCommandBusInterfaceKeyMissing(): void
8586
{
8687
$config = [];
8788

@@ -99,7 +100,7 @@ public function testInvokeThrowsExceptionWhenConfigProviderKeyMissing(): void
99100

100101
$this->expectException(InvalidConfigurationException::class);
101102
$this->expectExceptionMessage(
102-
'Configuration for key: $config[Webware\CommandBus\ConfigProvider] was not found in the config service.'
103+
'Configuration for key: $config[Webware\CommandBus\CommandBusInterface] was not found in the config service.' // phpcs:ignore
103104
);
104105

105106
($this->factory)($this->container);
@@ -108,7 +109,7 @@ public function testInvokeThrowsExceptionWhenConfigProviderKeyMissing(): void
108109
public function testInvokeWithEmptyMiddlewarePipelineConfig(): void
109110
{
110111
$config = [
111-
ConfigProvider::class => [
112+
CommandBusInterface::class => [
112113
ConfigProvider::MIDDLEWARE_PIPELINE_KEY => [],
113114
],
114115
];
@@ -133,7 +134,7 @@ public function testInvokeWithEmptyMiddlewarePipelineConfig(): void
133134
public function testInvokeWithMiddlewarePipelineConfiguration(): void
134135
{
135136
$config = [
136-
ConfigProvider::class => [
137+
CommandBusInterface::class => [
137138
ConfigProvider::MIDDLEWARE_PIPELINE_KEY => [
138139
[
139140
'middleware' => 'TestMiddleware1',
@@ -177,7 +178,7 @@ public function testInvokeWithMiddlewarePipelineConfiguration(): void
177178
public function testInvokeSkipsMiddlewareNotAvailableInContainer(): void
178179
{
179180
$config = [
180-
ConfigProvider::class => [
181+
CommandBusInterface::class => [
181182
ConfigProvider::MIDDLEWARE_PIPELINE_KEY => [
182183
[
183184
'middleware' => 'TestMiddleware1',
@@ -220,7 +221,7 @@ public function testInvokeSkipsMiddlewareNotAvailableInContainer(): void
220221
public function testInvokeThrowsExceptionWhenMiddlewareConfigMissingMiddlewareKey(): void
221222
{
222223
$config = [
223-
ConfigProvider::class => [
224+
CommandBusInterface::class => [
224225
ConfigProvider::MIDDLEWARE_PIPELINE_KEY => [
225226
[
226227
'priority' => 10,
@@ -253,7 +254,7 @@ public function testInvokeThrowsExceptionWhenMiddlewareConfigMissingMiddlewareKe
253254
public function testInvokeWithMiddlewareDefaultPriority(): void
254255
{
255256
$config = [
256-
ConfigProvider::class => [
257+
CommandBusInterface::class => [
257258
ConfigProvider::MIDDLEWARE_PIPELINE_KEY => [
258259
[
259260
'middleware' => 'TestMiddleware1',
@@ -291,7 +292,7 @@ public function testInvokeWithMiddlewareDefaultPriority(): void
291292
public function testFactoryCanBeInvokedMultipleTimes(): void
292293
{
293294
$config = [
294-
ConfigProvider::class => [
295+
CommandBusInterface::class => [
295296
ConfigProvider::MIDDLEWARE_PIPELINE_KEY => [],
296297
],
297298
];
@@ -325,7 +326,7 @@ public function testFactoryIsCallable(): void
325326
public function testInvokeWithNonIntegerPriorityDefaultsToOne(): void
326327
{
327328
$config = [
328-
ConfigProvider::class => [
329+
CommandBusInterface::class => [
329330
ConfigProvider::MIDDLEWARE_PIPELINE_KEY => [
330331
[
331332
'middleware' => 'TestMiddleware1',

0 commit comments

Comments
 (0)