Completed
Push — split-repo-in-bundle-and-socia... ( 660229 )
by Martin
18:10
created

AllInOnePass::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\SocialPostBundle\DependencyInjection\Compiler;
6
7
use MartinGeorgiev\SocialPost\Publisher;
8
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
11
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
12
use Symfony\Component\DependencyInjection\Reference;
13
14
/**
15
 * @since 1.0.0
16
 * @license https://opensource.org/licenses/MIT
17
 * @link https://github.com/martin-georgiev/social-post-bundle
18
 */
19
class AllInOnePass implements CompilerPassInterface
20
{
21
    public function process(ContainerBuilder $container): void
22
    {
23
        $definition = $container->getDefinition('social_post');
24
        $publishOn = $container->getParameter('social_post.configuration.publish_on');
25
        foreach ($publishOn as $provider) {
26
            $serviceName = 'social_post.'.$provider;
27
            if (!$container->has($serviceName)) {
28
                throw new OutOfBoundsException(sprintf('Cannot find service %s when injecting dependencies for "social_post"', $serviceName));
29
            }
30
            if (!$container->get($serviceName) instanceof Publisher) {
31
                throw new InvalidArgumentException(sprintf('Service %s should be an instance of %s', $serviceName, Publisher::class));
32
            }
33
            $definition->addArgument(new Reference($serviceName));
34
        }
35
    }
36
}
37