Completed
Pull Request — master (#230)
by
unknown
65:23
created

FilterPass::process()   B

Complexity

Conditions 8
Paths 15

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 7.132
c 0
b 0
f 0
cc 8
eloc 35
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\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\DefinitionDecorator;
20
use Symfony\Component\DependencyInjection\Reference;
21
22
/**
23
 * Compiles custom filters.
24
 */
25
class FilterPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function process(ContainerBuilder $container)
31
    {
32
        $filters = [];
33
        foreach ($container->findTaggedServiceIds('ongr_filter_manager.filter') as $filterId => $filterTags) {
34
            $tagOptions = array_shift($filterTags);
35
36
            if (!array_key_exists('type', $tagOptions)) {
37
                throw new InvalidConfigurationException(
38
                    sprintf('Filter service `%s` must have `type` set.', $filterId)
39
                );
40
            }
41
42
            $filters[$tagOptions['type']] = $filterId;
43
        }
44
45
        foreach ($container->getParameter('ongr_filter_manager.filters') as $filterName => $filterOptions) {
46
            if (!array_key_exists($filterOptions['type'], $filters)) {
47
                throw new InvalidConfigurationException(
48
                    "There is no `{$filterOptions['type']}` type filter registered."
49
                );
50
            }
51
52
            $definition = new DefinitionDecorator($filters[($filterOptions['type'])]);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
53
            $definition->addMethodCall('setRequestField', [$filterOptions['request_field']]);
54
            $definition->addMethodCall('setDocumentField', [$filterOptions['document_field']]);
55
            $definition->addMethodCall('setTags', [$filterOptions['tags']]);
56
            $definition->addMethodCall('setOptions', [$filterOptions['options']]);
57
            $this->addRelation($definition, $filterOptions, 'search', 'include');
58
            $this->addRelation($definition, $filterOptions, 'search', 'exclude');
59
            $this->addRelation($definition, $filterOptions, 'reset', 'include');
60
            $this->addRelation($definition, $filterOptions, 'reset', 'exclude');
61
62
            $container->setDefinition(ONGRFilterManagerExtension::getFilterId($filterName), $definition);
63
        }
64
65
        foreach ($container->getParameter('ongr_filter_manager.managers') as $managerName => $managerOptions) {
66
            $filterContainer = new Definition('ONGR\FilterManagerBundle\Search\FilterContainer');
67
68
            if (isset($managerOptions['filters'])) {
69
                foreach ($managerOptions['filters'] as $filter) {
70
                    $filterContainer->addMethodCall(
71
                        'set',
72
                        [$filter, new Reference(ONGRFilterManagerExtension::getFilterId($filter))]
73
                    );
74
                }
75
            }
76
77
            $managerDefinition = new Definition(
78
                'ONGR\FilterManagerBundle\Search\FilterManager',
79
                [
80
                    $filterContainer,
81
                    new Reference($managerOptions['repository']),
82
                    new Reference('event_dispatcher'),
83
                ]
84
            );
85
86
            $container->setDefinition(ONGRFilterManagerExtension::getFilterManagerId($managerName), $managerDefinition);
87
        }
88
    }
89
90
    /**
91
     * Adds relation to filter.
92
     *
93
     * @param Definition $definition
94
     * @param array      $filter
95
     * @param string     $urlType
96
     * @param string     $relationType
97
     */
98
    private function addRelation(Definition $definition, $filter, $urlType, $relationType)
99
    {
100
        if (empty($filter['relations'][$urlType][$relationType])) {
101
            return;
102
        }
103
104
        $relation = new Definition(
105
            sprintf('ONGR\FilterManagerBundle\Relation\%sRelation', ucfirst($relationType)),
106
            [$filter['relations'][$urlType][$relationType]]
107
        );
108
        $definition->addMethodCall(
109
            'set' . ucfirst($urlType) . 'Relation',
110
            [$relation]
111
        );
112
    }
113
}
114