Completed
Push — master ( 1a6be8...e04a88 )
by Craig
09:51
created

HookHandlerPass::process()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 6
nop 1
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\Bundle\HookBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
use Zikula\Bundle\HookBundle\Dispatcher\Storage\Doctrine\Entity\HookRuntimeEntity;
19
20
class HookHandlerPass implements CompilerPassInterface
21
{
22
    public function process(ContainerBuilder $container)
23
    {
24
        if (!$container->hasDefinition('doctrine') || true !== $container->getParameter('installed')) {
25
            return;
26
        }
27
        $doctrine = $container->get('doctrine');
28
        $dispatcherDefinition = $container->findDefinition('event_dispatcher');
29
30
        $handlers = $doctrine->getManager()->createQueryBuilder()->select('t')
31
                ->from(HookRuntimeEntity::class, 't')
32
                ->getQuery()
33
                ->getArrayResult();
34
        foreach ($handlers as $handler) {
35
            $callable = [$handler['classname'], $handler['method']];
36
            if (is_callable($callable)) {
37
                if ($handler['serviceid']) {
38
                    $callable = $this->buildService($container, $handler['serviceid'], $handler['classname'], $handler['method']);
39
                    $dispatcherDefinition->addMethodCall('addListenerService', [$handler['eventname'], $callable, 0]);
40
                } else {
41
                    try {
42
                        $dispatcherDefinition->addMethodCall('addListener', [$handler['eventname'], $callable, 0]);
43
                    } catch (\InvalidArgumentException $e) {
44
                        throw new \RuntimeException("Hook event handler could not be attached because %s", $e->getMessage(), 0, $e);
45
                    }
46
                }
47
            }
48
        }
49
    }
50
51
    /**
52
     * Build service.
53
     *
54
     * Builds event servicehandlers.  If the service does not exist, it creates it
55
     * and adds it to the DI container.
56
     *
57
     * @param ContainerBuilder $container
58
     * @param string $id
59
     * @param string $className
60
     * @param string $method
61
     *
62
     * @return array [$id, $method]
63
     */
64
    private function buildService(ContainerBuilder $container, $id, $className, $method)
65
    {
66
        if (!$container->has($id)) {
67
            $definition = new Definition($className, [new Reference('event_dispatcher')]);
68
            $container->setDefinition($id, $definition);
69
        }
70
71
        return [$id, $method];
72
    }
73
}
74