Passed
Push — master ( c86b81...7621ad )
by Valentin
04:59 queued 02:13
created

ProxyCompilerPass   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 69
ccs 34
cts 34
cp 1
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A reservedName() 0 9 1
B process() 0 52 7
1
<?php
2
3
/*
4
 * (c) 2013 Wozbe
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Pyrowman\PheanstalkBundle\DependencyInjection\Compiler;
10
11
use Pyrowman\PheanstalkBundle\Exceptions\PheanstalkException;
12
use Pheanstalk\Pheanstalk;
13
use Pheanstalk\PheanstalkInterface;
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
18
/**
19
 * Description of ProxyCompilerPass.
20
 *
21
 * @author Thomas Tourlourat <[email protected]>
22
 */
23
class ProxyCompilerPass implements CompilerPassInterface
24
{
25 11
    protected function reservedName()
26
    {
27
        return [
28 11
            'pheanstalks',
29
            'pheanstalk_locator',
30
            'proxy',
31
            'data_collector',
32
            'listener',
33
            'event',
34
        ];
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40 11
    public function process(ContainerBuilder $container)
41
    {
42 11
        $defaultPheanstalkName = null;
43 11
        $pheanstalks           = $container->getParameter('pyrowman.pheanstalk.pheanstalks');
44
45 11
        $pheanstalkLocatorDef = $container->getDefinition('pyrowman.pheanstalk.pheanstalk_locator');
46
47
        // For each connection in the configuration file
48 11
        foreach ($pheanstalks as $name => $pheanstalk) {
49 11
            if (in_array($name, $this->reservedName())) {
50 1
                throw new \RuntimeException('Reserved pheanstalk name: '.$name);
51
            }
52
53
            $pheanstalkConfig = [
54 10
                $pheanstalk['server'],
55 10
                $pheanstalk['user'] ?? null,
56 10
                $pheanstalk['password'] ?? null,
57 10
                $pheanstalk['port'],
58 10
                $pheanstalk['timeout']
59
            ];
60 10
            $isDefault = $pheanstalk['default'];
61
62
            # @see https://github.com/armetiz/pyrowmanPheanstalkBundle/issues/61
63 10
            $pheanstalkDef = clone $container->getDefinition($pheanstalk['proxy']);
64
65 9
            $pheanstalkDef->addMethodCall('setPheanstalk', [new Definition(Pheanstalk::class, $pheanstalkConfig)]);
66 9
            $pheanstalkDef->addMethodCall('setName', [$name]);
67 9
            $pheanstalkDef->setPublic(true);
68
69 9
            $container->setDefinition('pyrowman.pheanstalk.'.$name, $pheanstalkDef);
70
71
            // Register the connection in the connection locator
72 9
            $pheanstalkLocatorDef->addMethodCall('addPheanstalk', [
73 9
                $name,
74 9
                $container->getDefinition('pyrowman.pheanstalk.'.$name),
75 9
                $isDefault,
76
            ]);
77
78 9
            if ($isDefault) {
79 5
                if (null !== $defaultPheanstalkName) {
80 1
                    throw new PheanstalkException(sprintf('Default pheanstalk already defined. "%s" & "%s"', $defaultPheanstalkName, $name));
0 ignored issues
show
Bug introduced by
$defaultPheanstalkName of type void is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                    throw new PheanstalkException(sprintf('Default pheanstalk already defined. "%s" & "%s"', /** @scrutinizer ignore-type */ $defaultPheanstalkName, $name));
Loading history...
81
                }
82
83 5
                $defaultPheanstalkName = $name;
84 5
                $legacyAlias = $container->setAlias('pyrowman.pheanstalk', 'pyrowman.pheanstalk.'.$name);
85 5
                if ($legacyAlias) {
86 5
                    $legacyAlias->setPublic(true);
87
                }
88
89 5
                $autoWiringAlias = $container->setAlias(PheanstalkInterface::class, 'pyrowman.pheanstalk');
90 5
                if ($autoWiringAlias) {
91 9
                    $autoWiringAlias->setPublic(true);
92
                }
93
            }
94
        }
95
    }
96
}
97