Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ListenerProviderAggregateExtension
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __invoke
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 resolveListenerProvider
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of php-fast-forward/event-dispatcher.
7 *
8 * This source file is subject to the license bundled
9 * with this source code in the file LICENSE.
10 *
11 * @copyright Copyright (c) 2025-2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/event-dispatcher
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\EventDispatcher\ServiceProvider\Extension;
20
21use FastForward\EventDispatcher\Exception\RuntimeException;
22use FastForward\EventDispatcher\ServiceProvider\Configuration\ConfiguredListenerProviderCollection;
23use Phly\EventDispatcher\ListenerProvider\ListenerProviderAggregate;
24use Psr\Container\ContainerInterface;
25use Psr\EventDispatcher\ListenerProviderInterface;
26
27/**
28 * Attach configured listener providers to the aggregate provider.
29 *
30 * @internal
31 */
32  class ListenerProviderAggregateExtension
33{
34    /**
35     * Attach configured listener providers to the aggregate.
36     *
37     * @param ContainerInterface $container container used to resolve provider services
38     * @param ListenerProviderAggregate $listenerProviderAggregate aggregate provider to extend
39     *
40     * @throws RuntimeException thrown when a configured provider cannot be resolved
41     */
42    public function __invoke(
43        ContainerInterface $container,
44        ListenerProviderAggregate $listenerProviderAggregate,
45    ): void {
46        $configuredListeners = $container->get(ConfiguredListenerProviderCollection::class);
47
48        foreach ($configuredListeners->listenerProviders() as $listenerProvider) {
49            $listenerProviderAggregate->attach($this->resolveListenerProvider($container, $listenerProvider));
50        }
51    }
52
53    /**
54     * Resolve one configured listener provider to a concrete provider instance.
55     *
56     * @param ContainerInterface $container container used for service resolution
57     * @param ListenerProviderInterface|string $listenerProvider provider instance or service identifier
58     *
59     * @return ListenerProviderInterface resolved listener provider
60     *
61     * @throws RuntimeException thrown when the resolved value is not a listener provider
62     */
63    private function resolveListenerProvider(
64        ContainerInterface $container,
65        ListenerProviderInterface|string $listenerProvider,
66    ): ListenerProviderInterface {
67        if (\is_string($listenerProvider) && $container->has($listenerProvider)) {
68            $listenerProvider = $container->get($listenerProvider);
69        }
70
71        if (! $listenerProvider instanceof ListenerProviderInterface) {
72            throw RuntimeException::forUnsupportedType($listenerProvider);
73        }
74
75        return $listenerProvider;
76    }
77}