Completed
Pull Request — master (#35)
by Krzysztof
04:21
created

CellCompilerPass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 107
ccs 28
cts 28
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processParam() 0 17 2
A processCell() 0 20 2
A getTransformerDefinition() 0 18 2
A configureDefinition() 0 21 1
1
<?php
2
3
namespace KGzocha\Bundle\SearcherBundle\DependencyInjection\CompilerPass;
4
5
use KGzocha\Bundle\SearcherBundle\DependencyInjection\Configuration;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
9
/**
10
 * @author Krzysztof Gzocha <[email protected]>
11
 */
12
class CellCompilerPass extends AbstractChainsCompilerPass
13
{
14
    /**
15
     * @inheritDoc
16
     */
17 2
    protected function processParam(
18
        $contextId,
19
        array &$paramConfig,
20
        ContainerBuilder $container
21
    ) {
22 2
        $cellDefinitions = [];
23 2
        foreach ($paramConfig['cells'] as &$cellConfig) {
24 2
            $cellDefinitions[] = $this->processCell($contextId, $cellConfig, $container);
25
        }
26
27 2
        $chainSearch = $container->getDefinition($this->buildChainServiceName(
28
            $contextId,
29 2
            self::SEARCHER_PARAMETER
30
        ));
31
32 2
        $chainSearch->addArgument($cellDefinitions);
33 2
    }
34
35
    /**
36
     * @param string           $contextId
37
     * @param array            $cellConfig
38
     * @param ContainerBuilder $container
39
     *
40
     * @return Definition
41
     */
42 2
    private function processCell($contextId, array &$cellConfig, ContainerBuilder $container)
43
    {
44 2
        $definitionName = $this->buildChainServiceName(
45
            $contextId,
46 2
            sprintf('cell.%s', $cellConfig[self::NAME_PARAMETER])
47
        );
48
49 2
        $definition = $this->buildDefinition(
50
            $container,
51
            $contextId,
52
            $definitionName,
53
            $cellConfig
54
        );
55
56 2
        if ($cellConfig[self::SERVICE_PARAMETER]) {
57 1
            return $definition;
58
        }
59
60 2
        return $this->configureDefinition($contextId, $cellConfig, $container, $definition);
61
    }
62
63
    /**
64
     * @param ContainerBuilder $container
65
     * @param string           $contextId
66
     * @param array            $cellConfig
67
     *
68
     * @return Definition
69
     */
70 2
    private function getTransformerDefinition(
71
        ContainerBuilder $container,
72
        $contextId,
73
        array &$cellConfig
74
    ) {
75 2
        if (!$cellConfig[self::TRANSFORMER_PARAMETER]) {
76 1
            return new Definition(Configuration::END_TRANSFORMER_CLASS);
77
        }
78
79 2
        return $container->getDefinition($this->buildChainServiceName(
80
            $contextId,
81
            sprintf(
82 2
                '%s.%s',
83 2
                self::TRANSFORMER_PARAMETER,
84 2
                $cellConfig[self::TRANSFORMER_PARAMETER]
85
            )
86
        ));
87
    }
88
89
    /**
90
     * @param                  $contextId
91
     * @param array            $cellConfig
92
     * @param ContainerBuilder $container
93
     * @param Definition       $definition
94
     *
95
     * @return Definition
96
     */
97 2
    private function configureDefinition(
98
        $contextId,
99
        array &$cellConfig,
100
        ContainerBuilder $container,
101
        Definition $definition
102
    ) {
103
        // Add searcher as first argument
104 2
        $definition->addArgument($container->getDefinition(
105 2
            $this->buildServiceName($cellConfig[self::SEARCHER_PARAMETER], self::SEARCHER_PARAMETER)
106
        ));
107
108
        // Add transformer as second argument
109 2
        $definition->addArgument($this->getTransformerDefinition(
110
            $container, $contextId, $cellConfig
111
        ));
112
113
        // Add cell name as third argument
114 2
        $definition->addArgument($cellConfig[self::NAME_PARAMETER]);
115
116 2
        return $definition;
117
    }
118
}
119