Completed
Push — master ( a8e426...cdcd92 )
by Simonas
62:46
created

FilterPass::process()   B

Complexity

Conditions 8
Paths 15

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 7.4033
c 0
b 0
f 0
cc 8
eloc 31
nc 15
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\FilterManagerBundle\DependencyInjection\Compiler;
13
14
use ONGR\FilterManagerBundle\DependencyInjection\ONGRFilterManagerExtension;
15
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
16
use Symfony\Component\DependencyInjection\ChildDefinition;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\DefinitionDecorator;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
/**
24
 * Compiles custom filters.
25
 */
26
class FilterPass implements CompilerPassInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function process(ContainerBuilder $container)
32
    {
33
        $filters = [];
34
        foreach ($container->findTaggedServiceIds('ongr_filter_manager.filter') as $filterId => $filterTags) {
35
            $tagOptions = array_shift($filterTags);
36
37
            if (!array_key_exists('type', $tagOptions)) {
38
                throw new InvalidConfigurationException(
39
                    sprintf('Filter service `%s` must have `type` set.', $filterId)
40
                );
41
            }
42
43
            $filters[$tagOptions['type']] = $filterId;
44
        }
45
46
        foreach ($container->getParameter('ongr_filter_manager.filters') as $filterName => $filterOptions) {
47
            if (!array_key_exists($filterOptions['type'], $filters)) {
48
                throw new InvalidConfigurationException(
49
                    "There is no `{$filterOptions['type']}` type filter registered."
50
                );
51
            }
52
53
            $definition = new ChildDefinition($filters[($filterOptions['type'])]);
54
            $definition->addMethodCall('setRequestField', [$filterOptions['request_field']]);
55
            $definition->addMethodCall('setDocumentField', [$filterOptions['document_field']]);
56
            $definition->addMethodCall('setTags', [$filterOptions['tags']]);
57
            $definition->addMethodCall('setOptions', [$filterOptions['options']]);
58
59
            $container->setDefinition(ONGRFilterManagerExtension::getFilterId($filterName), $definition);
60
        }
61
62
        foreach ($container->getParameter('ongr_filter_manager.managers') as $managerName => $managerOptions) {
63
            $filterContainer = new Definition('ONGR\FilterManagerBundle\Search\FilterContainer');
64
65
            if (isset($managerOptions['filters'])) {
66
                foreach ($managerOptions['filters'] as $filter) {
67
                    $filterContainer->addMethodCall(
68
                        'set',
69
                        [$filter, new Reference(ONGRFilterManagerExtension::getFilterId($filter))]
70
                    );
71
                }
72
            }
73
74
            $managerDefinition = new Definition(
75
                'ONGR\FilterManagerBundle\Search\FilterManager',
76
                [
77
                    $filterContainer,
78
                    new Reference($managerOptions['repository']),
79
                    new Reference('event_dispatcher'),
80
                ]
81
            );
82
83
            $container->setDefinition(ONGRFilterManagerExtension::getFilterManagerId($managerName), $managerDefinition);
84
        }
85
    }
86
}
87