Completed
Push — master ( 21b783...e431c6 )
by Paweł
46:07 queued 35:24
created

EventDispatcher::dispatchMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ResourceBundle\Controller;
15
16
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
17
use Sylius\Component\Resource\Model\ResourceInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
19
20
/**
21
 * @author Paweł Jędrzejewski <[email protected]>
22
 */
23
final class EventDispatcher implements EventDispatcherInterface
24
{
25
    /**
26
     * @var SymfonyEventDispatcherInterface
27
     */
28
    private $eventDispatcher;
29
30
    /**
31
     * @param SymfonyEventDispatcherInterface $eventDispatcher
32
     */
33
    public function __construct(SymfonyEventDispatcherInterface $eventDispatcher)
34
    {
35
        $this->eventDispatcher = $eventDispatcher;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function dispatch(
42
        string $eventName,
43
        RequestConfiguration $requestConfiguration,
44
        ResourceInterface $resource
45
    ):  ResourceControllerEvent {
46
        $eventName = $requestConfiguration->getEvent() ?: $eventName;
47
        $metadata = $requestConfiguration->getMetadata();
48
        $event = new ResourceControllerEvent($resource);
49
50
        $this->eventDispatcher->dispatch(sprintf('%s.%s.%s', $metadata->getApplicationName(), $metadata->getName(), $eventName), $event);
51
52
        return $event;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function dispatchMultiple(
59
        string $eventName,
60
        RequestConfiguration $requestConfiguration,
61
        $resources
62
    ):  ResourceControllerEvent {
63
        $eventName = $requestConfiguration->getEvent() ?: $eventName;
64
        $metadata = $requestConfiguration->getMetadata();
65
        $event = new ResourceControllerEvent($resources);
66
67
        $this->eventDispatcher->dispatch(sprintf('%s.%s.%s', $metadata->getApplicationName(), $metadata->getName(), $eventName), $event);
68
69
        return $event;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function dispatchPreEvent(
76
        string $eventName,
77
        RequestConfiguration $requestConfiguration,
78
        ResourceInterface $resource
79
    ): ResourceControllerEvent {
80
        $eventName = $requestConfiguration->getEvent() ?: $eventName;
81
        $metadata = $requestConfiguration->getMetadata();
82
        $event = new ResourceControllerEvent($resource);
83
84
        $this->eventDispatcher->dispatch(sprintf('%s.%s.pre_%s', $metadata->getApplicationName(), $metadata->getName(), $eventName), $event);
85
86
        return $event;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function dispatchPostEvent(
93
        string $eventName,
94
        RequestConfiguration $requestConfiguration,
95
        ResourceInterface $resource
96
    ): ResourceControllerEvent {
97
        $eventName = $requestConfiguration->getEvent() ?: $eventName;
98
        $metadata = $requestConfiguration->getMetadata();
99
        $event = new ResourceControllerEvent($resource);
100
101
        $this->eventDispatcher->dispatch(sprintf('%s.%s.post_%s', $metadata->getApplicationName(), $metadata->getName(), $eventName), $event);
102
103
        return $event;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function dispatchInitializeEvent(
110
        string $eventName,
111
        RequestConfiguration $requestConfiguration,
112
        ResourceInterface $resource
113
    ): ResourceControllerEvent {
114
        $eventName = $requestConfiguration->getEvent() ?: $eventName;
115
        $metadata = $requestConfiguration->getMetadata();
116
        $event = new ResourceControllerEvent($resource);
117
118
        $this->eventDispatcher->dispatch(
119
            sprintf('%s.%s.initialize_%s', $metadata->getApplicationName(), $metadata->getName(), $eventName),
120
            $event
121
        );
122
123
        return $event;
124
    }
125
}
126