Completed
Push — master ( 8386c2...ffd2be )
by Simonas
61:26
created

FilterPass::process()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 29
rs 6.7272
cc 7
eloc 16
nc 10
nop 1
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\Exception\InvalidArgumentException;
20
21
/**
22
 * Compiles custom filters.
23
 */
24
class FilterPass implements CompilerPassInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function process(ContainerBuilder $container)
30
    {
31
        foreach ($container->findTaggedServiceIds('ongr_filter_manager.filter') as $filterId => $filterTags) {
32
            foreach ($filterTags as $tag) {
33
                if (!array_key_exists('filter_name', $tag)) {
34
                    throw new InvalidConfigurationException(
35
                        sprintf('Filter tagged with `%s` must have `filter_name` set.', $filterId)
36
                    );
37
                }
38
39
                $filterLabel = ONGRFilterManagerExtension::getFilterId($tag['filter_name']);
40
                if ($filterLabel === $filterId) {
41
                    continue;
42
                }
43
44
                if ($container->has($filterLabel)) {
45
                    throw new InvalidConfigurationException(
46
                        "Found duplicate filter name `{$tag['filter_name']}`"
47
                    );
48
                }
49
                $container->setAlias($filterLabel, $filterId);
50
            }
51
        }
52
53
        foreach ($container->findTaggedServiceIds('es.filter_manager') as $managerId => $managerTags) {
54
            $managerDefinition = $container->getDefinition($managerId);
55
            $this->checkManager($managerDefinition, "Manager '{$managerId}' does not have any filters.");
56
        }
57
    }
58
59
    /**
60
     * Checks if manager definition has any filters set.
61
     *
62
     * @param Definition $filterManager
63
     * @param string     $message
64
     *
65
     * @throws InvalidArgumentException
66
     */
67
    private function checkManager(Definition $filterManager, $message = '')
68
    {
69
        $filterContainer = $filterManager->getArgument(0);
70
        if (!$filterContainer->hasMethodCall('set')) {
71
            throw new InvalidArgumentException($message);
72
        }
73
    }
74
}
75