Completed
Push — master ( c3ad39...fd7849 )
by Simonas
62:35
created

DependencyInjection/Compiler/FilterPass.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
                    new Reference('jms_serializer')
84
                ]
85
            );
86
87
            $container->setDefinition(ONGRFilterManagerExtension::getFilterManagerId($managerName), $managerDefinition);
88
        }
89
    }
90
91
    /**
92
     * Adds relation to filter.
93
     *
94
     * @param Definition $definition
95
     * @param array      $filter
96
     * @param string     $urlType
97
     * @param string     $relationType
98
     */
99
    private function addRelation(Definition $definition, $filter, $urlType, $relationType)
100
    {
101
        if (empty($filter['relations'][$urlType][$relationType])) {
102
            return;
103
        }
104
105
        $relation = new Definition(
106
            sprintf('ONGR\FilterManagerBundle\Relation\%sRelation', ucfirst($relationType)),
107
            [$filter['relations'][$urlType][$relationType]]
108
        );
109
        $definition->addMethodCall(
110
            'set' . ucfirst($urlType) . 'Relation',
111
            [$relation]
112
        );
113
    }
114
}
115