Passed
Push — master ( 7621ad...1d8c5a )
by Valentin
03:29
created

ProxyCompilerPass::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
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 Pyrowman\PheanstalkBundle\PheanstalkLocator;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
19
/**
20
 * Description of ProxyCompilerPass.
21
 *
22
 * @author Thomas Tourlourat <[email protected]>
23
 */
24
class ProxyCompilerPass implements CompilerPassInterface
25
{
26
    protected $defaultPheanstalkName = null;
27
28 11
    protected function reservedName()
29
    {
30
        return [
31 11
            'pheanstalks',
32
            'pheanstalk_locator',
33
            'proxy',
34
            'data_collector',
35
            'listener',
36
            'event',
37
        ];
38
    }
39
40
    /**
41
     * @param ContainerBuilder $container
42
     *
43
     * @throws PheanstalkException
44
     */
45 11
    public function process(ContainerBuilder $container)
46
    {
47 11
        $pheanstalks = $container->getParameter('pyrowman.pheanstalk.pheanstalks');
48
49
        // For each connection in the configuration file
50 11
        foreach ($pheanstalks as $name => $pheanstalk) {
51 11
            $this->addServer($name, $pheanstalk, $container);
52
        }
53
    }
54
55
    /**
56
     * @param string           $name
57
     * @param array            $pheanstalk
58
     * @param ContainerBuilder $container
59
     *
60
     * @throws PheanstalkException
61
     */
62 11
    protected function addServer($name, array $pheanstalk, ContainerBuilder $container)
63
    {
64 11
        $pheanstalkLocatorDef = $container->getDefinition('pyrowman.pheanstalk.pheanstalk_locator');
65 11
        if (in_array($name, $this->reservedName())) {
66 1
            throw new \RuntimeException('Reserved pheanstalk name: ' . $name);
67
        }
68
69
        $pheanstalkConfig = [
70 10
            $pheanstalk['server'],
71 10
            $pheanstalk['user'] ?? null,
72 10
            $pheanstalk['password'] ?? null,
73 10
            $pheanstalk['port'],
74 10
            $pheanstalk['timeout']
75
        ];
76 10
        $isDefault = $pheanstalk['default'];
77
78
        # @see https://github.com/armetiz/pyrowmanPheanstalkBundle/issues/61
79 10
        $pheanstalkDef = clone $container->getDefinition($pheanstalk['proxy']);
80
81 9
        $pheanstalkDef->addMethodCall('setPheanstalk', [new Definition(Pheanstalk::class, $pheanstalkConfig)]);
82 9
        $pheanstalkDef->addMethodCall('setName', [$name]);
83 9
        $pheanstalkDef->setPublic(true);
84
85 9
        $container->setDefinition('pyrowman.pheanstalk.' . $name, $pheanstalkDef);
86
87
        // Register the connection in the connection locator
88 9
        $pheanstalkLocatorDef->addMethodCall('addPheanstalk', [
89 9
            $name,
90 9
            $container->getDefinition('pyrowman.pheanstalk.' . $name),
91 9
            $isDefault,
92
        ]);
93
94 9
        if ($isDefault) {
95 5
            $this->autowireDefaultConfig($name, $container);
96
        }
97
    }
98
99
    /**
100
     * @param string $name
101
     * @param ContainerBuilder $container
102
     *
103
     * @throws PheanstalkException
104
     */
105 5
    protected function autowireDefaultConfig($name, ContainerBuilder $container)
106
    {
107 5
        if (null !== $this->defaultPheanstalkName) {
108 1
            throw new PheanstalkException(sprintf('Default pheanstalk already defined. "%s" & "%s"', $this->defaultPheanstalkName, $name));
109
        }
110
111 5
        $this->defaultPheanstalkName = $name;
112 5
        $legacyAlias = $container->setAlias('pyrowman.pheanstalk', 'pyrowman.pheanstalk.' . $name);
113 5
        if ($legacyAlias) {
0 ignored issues
show
introduced by
$legacyAlias is of type Symfony\Component\DependencyInjection\Alias, thus it always evaluated to true.
Loading history...
114 5
            $legacyAlias->setPublic(true);
115
        }
116
117 5
        $autoWiringAlias = $container->setAlias(PheanstalkInterface::class, 'pyrowman.pheanstalk');
118 5
        if ($autoWiringAlias) {
0 ignored issues
show
introduced by
$autoWiringAlias is of type Symfony\Component\DependencyInjection\Alias, thus it always evaluated to true.
Loading history...
119 5
            $autoWiringAlias->setPublic(true);
120
        }
121
    }
122
}
123