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

AbstractContextCompilerPass::buildServiceName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace KGzocha\Bundle\SearcherBundle\DependencyInjection\CompilerPass;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
/**
10
 * @author Krzysztof Gzocha <[email protected]>
11
 */
12
abstract class AbstractContextCompilerPass implements CompilerPassInterface
13
{
14
    const CLASS_PARAMETER = 'class';
15
    const SERVICE_PARAMETER = 'service';
16
    const NAME_PARAMETER = 'name';
17
    const TRANSFORMER_PARAMETER = 'transformer';
18
19
    const CRITERIA_COLLECTION_PARAMETER = 'criteria_collection';
20
    const BUILDER_COLLECTION_PARAMETER = 'builder_collection';
21
    const CRITERIA_PARAMETER = 'criteria';
22
    const CONTEXT_PARAMETER = 'context';
23
    const SEARCHER_PARAMETER = 'searcher';
24
    const WRAPPER_CLASS_PARAMETER = 'wrapper_class';
25
26
    /**
27
     * @var DefinitionBuilder
28
     */
29
    private $definitionBuilder;
30
31
    /**
32
     * @var string
33
     */
34
    protected $servicePrefix;
35
36
    /**
37
     * @param DefinitionBuilder $definitionBuilder
38
     * @param string            $servicePrefix
39
     */
40 22
    public function __construct(DefinitionBuilder $definitionBuilder, $servicePrefix)
41
    {
42 22
        $this->definitionBuilder = $definitionBuilder;
43 22
        $this->servicePrefix = $servicePrefix;
44 22
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 21
    public function process(ContainerBuilder $container)
50
    {
51 21
        $param = sprintf('%s.%s', $this->servicePrefix, $this->getParamToBeProcessed());
52 21
        $contexts = $container->getParameter($param);
53
54 21
        foreach ($contexts as $contextId => &$context) {
55 21
            $this->processParam($contextId, $context, $container);
56
        }
57 20
    }
58
59
    /**
60
     * @param string           $contextId
61
     * @param array            $paramConfig
62
     * @param ContainerBuilder $container
63
     */
64
    abstract protected function processParam(
65
        $contextId,
66
        array &$paramConfig,
67
        ContainerBuilder $container
68
    );
69
70
    /**
71
     * @return string
72
     */
73 20
    protected function getParamToBeProcessed()
74
    {
75 20
        return 'contexts';
76
    }
77
78
    /**
79
     * @param string $contextId
80
     * @param string $name
81
     *
82
     * @return string
83
     */
84 21
    protected function buildServiceName($contextId, $name)
85
    {
86 21
        return sprintf(
87 21
            '%s.%s.%s',
88 21
            $this->servicePrefix,
89
            $contextId,
90
            $name
91
        );
92
    }
93
94
    /**
95
     * @param ContainerBuilder $container
96
     * @param string           $contextId
97
     * @param string           $definitionName
98
     * @param array            $config
99
     *
100
     * @return \Symfony\Component\DependencyInjection\Definition
101
     */
102 21
    protected function buildDefinition(
103
        ContainerBuilder $container,
104
        $contextId,
105
        $definitionName,
106
        array &$config
107
    ) {
108
        return $this
109 21
            ->definitionBuilder
110 21
            ->buildDefinition(
111
                $container,
112
                $contextId,
113
                $definitionName,
114
                $config
115
            );
116
    }
117
118
    /**
119
     * @param ContainerBuilder $container
120
     * @param string           $contextId
121
     * @param string           $serviceName
122
     *
123
     * @return bool
124
     *
125
     * @throws InvalidDefinitionException
126
     */
127 2
    protected function checkIfServiceExists(
128
        ContainerBuilder $container,
129
        $contextId,
130
        $serviceName
131
    ) {
132 2
        if (!$container->hasDefinition($serviceName)) {
133 1
            throw new InvalidDefinitionException(sprintf(
134 1
                'Service "%s" configured in searching context "%s" does not exist',
135
                $serviceName,
136
                $contextId
137
            ));
138
        }
139 1
    }
140
}
141