Completed
Push — develop ( 9659b8...659b85 )
by Mathias
31:55 queued 13:03
created

EntityEraserEvents   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setEventPrototype() 0 8 2
B triggerListeners() 0 21 6
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2018 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Service\EntityEraser;
12
13
use Core\EventManager\EventManager as CoreEventManager;
14
use Zend\EventManager\EventInterface;
15
use Zend\EventManager\SharedEventManagerInterface;
16
17
/**
18
 * EventManager for Dependency events.
19
 *
20
 * Handles the repsonses from the listeners and add them to the events' dependency result collection.
21
 * 
22
 * @author Mathias Gelhausen <[email protected]>
23
 * @todo write test 
24
 */
25
class EntityEraserEvents extends CoreEventManager
26
{
27
    /**
28
     * Set the event prototype.
29
     *
30
     * Only instances of DependencyResultEvent are allowed.
31
     *
32
     * @param EventInterface $prototype
33
     * @throws \InvalidArgumentException
34
     */
35
    public function setEventPrototype(EventInterface $prototype)
36
    {
37
        if (!$prototype instanceOf DependencyResultEvent) {
38
            throw new \InvalidArgumentException('This event manager only accepts events of the type ' . DependencyResultEvent::class);
39
        }
40
41
        parent::setEventPrototype($prototype);
42
    }
43
44
    /**
45
     * Triggers listeners.
46
     *
47
     * Loops over the responses and tries to add any non empty response as a DepnendecyResult to the collection.
48
     *
49
     * @param EventInterface $event
50
     * @param callable|null  $callback
51
     *
52
     * @return \Zend\EventManager\ResponseCollection
53
     * @throws \InvalidArgumentException if the event is not an instance of DependencyResultEvent
54
     */
55
    protected function triggerListeners(EventInterface $event, callable $callback = null)
56
    {
57
        if (!$event instanceOf DependencyResultEvent) {
58
            throw new \InvalidArgumentException('This event manager only accepts events of the type ' . DependencyResultEvent::class);
59
        }
60
61
        $results = parent::triggerListeners($event, $callback);
62
63
        $dependencies = $event->getDependencyResultCollection();
64
65
        foreach ($results as $result) {
66
            if (null !== $result) {
67
                try { $dependencies->add($result); }
68
                /* silently ignore all invalid results */
69
                catch (\UnexpectedValueException $e) {}
70
                catch (\InvalidArgumentException $e) {}
71
            }
72
        }
73
74
        return $results;
75
    }
76
77
78
}
79