Completed
Pull Request — master (#169)
by Mikołaj
05:25
created

MediaProviderPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 26 5
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\DependencyInjection\Compiler;
14
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
final class MediaProviderPass implements CompilerPassInterface
20
{
21
    public function process(ContainerBuilder $container): void
22
    {
23
        if (!$container->hasDefinition('bitbag_sylius_cms_plugin.registry.media_provider')) {
24
            return;
25
        }
26
27
        $providerRegistry = $container->getDefinition('bitbag_sylius_cms_plugin.registry.media_provider');
28
        $providers = [];
29
30
        foreach ($container->findTaggedServiceIds('bitbag_sylius_cms_plugin.media_provider') as $id => $attributes) {
31
            if (!isset($attributes[0]['type']) || !isset($attributes[0]['label'])) {
32
                throw new \InvalidArgumentException('Tagged media provider needs to have `type` and `label` attribute.');
33
            }
34
35
            $name = $attributes[0]['label'];
36
            $type = $attributes[0]['type'];
37
38
            $providers[$name] = $type;
39
40
            $providerRegistry->addMethodCall('register', [$type, new Reference($id)]);
41
        }
42
43
        ksort($providers);
44
45
        $container->setParameter('bitbag_sylius_cms_plugin.media_providers', $providers);
46
    }
47
}
48