Completed
Pull Request — master (#4)
by dan
14:39
created

AdapterPass   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 25 4
1
<?php
2
3
4
namespace IrishDan\NotificationBundle\DependencyInjection;
5
6
use IrishDan\NotificationBundle\Adapter\MessageAdapterInterface;
7
use Symfony\Bundle\MakerBundle\MakerInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\Reference;
11
// use App\Mail\TransportChain;
12
13
class AdapterPass implements CompilerPassInterface
14
{
15
    public function process(ContainerBuilder $container)
16
    {
17
        if (!$container->has('notification.channel_subscriber')) {
18
            return;
19
        }
20
21
        // Add all adapters to the subscriber so ot can handle
22
        // any type of message.
23
        $subscriberDefinition = $container->findDefinition('notification.channel_subscriber');
24
25
        // find all service IDs with the app.mail_transport tag.
26
        // @TODO: Inject only the adapters that are used
27
        $taggedServices = $container->findTaggedServiceIds('notification.adapter');
28
29
        foreach ($taggedServices as $id => $tags) {
30
            $def = $container->getDefinition($id);
31
            $class = $container->getParameterBag()->resolveValue($def->getClass());
32
33
            if (!is_subclass_of($class, MessageAdapterInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \IrishDan\NotificationBu...AdapterInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
34
                throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, MakerInterface::class));
35
            }
36
37
            $subscriberDefinition->addMethodCall('addAdapter', [$id, new Reference($id)]);
38
        }
39
    }
40
}