Completed
Push — EZP-26057-permission-api ( abfa4f...880141 )
by
unknown
59:10
created

SearchEngineSignalSlotPass::process()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 28
rs 8.439
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Base\Container\Compiler\Search;
10
11
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use LogicException;
14
use Symfony\Component\DependencyInjection\Reference;
15
16
/**
17
 * This compiler pass will attach Search Engine slots to SignalDispatcher.
18
 */
19
class SearchEngineSignalSlotPass implements CompilerPassInterface
20
{
21
    /**
22
     * Name of a search engine for which Compiler Pass was instantiated.
23
     *
24
     * @var string
25
     */
26
    private $searchEngineAlias;
27
28
    public function __construct($searchEngineAlias)
29
    {
30
        $this->searchEngineAlias = $searchEngineAlias;
31
    }
32
33
    public function process(ContainerBuilder $container)
34
    {
35
        if (!$container->hasDefinition('ezpublish.signalslot.signal_dispatcher.factory')) {
36
            return;
37
        }
38
39
        $signalDispatcherFactoryDef = $container->getDefinition('ezpublish.signalslot.signal_dispatcher.factory');
40
        $searchEngineSlotTagName = sprintf('ezpublish.search.%s.slot', $this->searchEngineAlias);
41
        $tags = $container->findTaggedServiceIds('ezpublish.search.slot')
42
                + $container->findTaggedServiceIds($searchEngineSlotTagName);
43
44
        $searchEngineSignalSlots = [];
45
        foreach ($tags as $id => $attributes) {
46
            foreach ($attributes as $attribute) {
47
                if (!isset($attribute['signal'])) {
48
                    throw new LogicException(
49
                        "Could not find 'signal' attribute on '$id' service, " .
50
                        "which is mandatory for services tagged as 'ezpublish.search.slot', " .
51
                        "or the specific tags for given search engine, in this case '$searchEngineSlotTagName'."
52
                    );
53
                }
54
55
                $searchEngineSignalSlots[$attribute['signal']][] = new Reference($id);
56
            }
57
        }
58
59
        $signalDispatcherFactoryDef->addMethodCall('addSlotsForSearchEngine', [$this->searchEngineAlias, $searchEngineSignalSlots]);
60
    }
61
}
62