Completed
Push — develop ( 9659b8...659b85 )
by Mathias
13:02
created

EntityEraser::checkDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Controller\Plugin;
12
13
use Core\Entity\EntityInterface;
14
use Core\EventManager\EventManager;
15
use Core\Repository\RepositoryService;
16
use Core\Service\EntityEraser\DependencyResultEvent;
17
use Core\Service\EntityEraser\EntityEraserEvents;
18
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
19
20
/**
21
 * Plugin to load entities, check their dependencies and delete these entities with all their dependencies.
22
 *
23
 * It's meant to use three steps:
24
 *
25
 * 1. Load the entities to delete.
26
 *    Using the event manager "Core/EntityEraser/Load/Events", it loads entities to delete by
27
 *    triggering the event with the event named after a passed in key.
28
 *    This way all kind of entities can be loaded (mostly relevant for the console action "purge".)
29
 *
30
 * 2. Check dependencies.
31
 *    Using the event manager "Core/EntityEraser/Dependencies/Events", it triggers an event for
32
 *    each entity to delete. This way any listener can add dependencies to the DependencyResultCollection.
33
 *    These dependencies can then be used to show which entities will be affected in which way, if the entities
34
 *    will be deleted.
35
 *
36
 * 3. Delete the entities.
37
 *    Using the event manager "Core/EntityEraser/Dependencies/Events", for each entity to delete, the event
38
 *    DependencyResultEvent::DELETE will be fired. Allowing listeners to act acording to the deletion of the entity.
39
 *    After that, each entity will be removed from the database.
40
 * 
41
 * @author Mathias Gelhausen <[email protected]>
42
 * @todo write test 
43
 */
44
class EntityEraser extends AbstractPlugin
45
{
46
    /**
47
     * @var EntityEraserEvents
48
     */
49
    private $entityEraserEvents;
50
51
    /**
52
     * EventManager for the loading of entities.
53
     *
54
     * @var EventManager
55
     */
56
    private $loadEntitiesEvents;
57
58
    /**
59
     * RepositoryService
60
     *
61
     * @var \Core\Repository\RepositoryService
62
     */
63
    private $repositories;
64
65
    /**
66
     * Array of options to be passed along to listeners as event parameters.
67
     *
68
     * @var array
69
     */
70
    private $options = [];
71
72
    /**
73
     * EntityEraser constructor.
74
     *
75
     * @param EntityEraserEvents $entityEraserEvents
76
     * @param EventManager       $loadEntitiesEvents
77
     * @param RepositoryService  $repositories
78
     */
79
    public function __construct(EntityEraserEvents $entityEraserEvents, EventManager $loadEntitiesEvents, RepositoryService $repositories)
80
    {
81
        $this->entityEraserEvents = $entityEraserEvents;
82
        $this->loadEntitiesEvents = $loadEntitiesEvents;
83
        $this->repositories = $repositories;
84
    }
85
86
    /**
87
     * Set options to be passed along to listeners as event parameters.
88
     *
89
     * @param array $options
90
     */
91
    public function setOptions(array $options)
92
    {
93
        $this->options = $options;
94
    }
95
96
    /**
97
     * Loads entities to be deleted.
98
     *
99
     * Triggers an event on the "Core/EntityEraser/Load/Events" event manager.
100
     *
101
     * @param string      $entity Used as event name.
102
     * @param string|null $id
103
     *
104
     * @return array|\Traversable|null
105
     */
106
    public function loadEntities($entity, $id = null)
107
    {
108
        $params = $this->options;
109
        $params['id'] = $id;
110
        $params['repositories'] = $this->repositories;
111
112
        $event = $this->loadEntitiesEvents->getEvent($entity, $this, $params);
113
        $responses = $this->loadEntitiesEvents->triggerEventUntil(
114
            function ($response) { return (is_array($response) || $response instanceOf \Traversable) && count($response); },
115
            $event
116
        );
117
118
        $entities = $responses->last();
119
120
        return $entities;
121
    }
122
123
    /**
124
     * Checks dependencies for an entity.
125
     *
126
     * Triggers the DependencyResultEvent::CHECK_DEPENDENCIES event on the
127
     * "Core/EntityEraser/Dependencies/Events" event manager.
128
     *
129
     * @param EntityInterface $entity
130
     *
131
     * @return \Core\Service\EntityEraser\DependencyResultCollection
132
     * @uses triggerEvent
133
     */
134
    public function checkDependencies(EntityInterface $entity)
135
    {
136
        return $this->triggerEvent(DependencyResultEvent::CHECK_DEPENDENCIES, $entity);
137
    }
138
139
    /**
140
     * Deletes an entity.
141
     *
142
     * Triggers the DependencyResultEvent::DELETE event on the
143
     * "Core/EntityEraser/Dependencies/Events" event manager.
144
     *
145
     * Removes the entity from the database.
146
     *
147
     * @param EntityInterface $entity
148
     *
149
     * @return \Core\Service\EntityEraser\DependencyResultCollection
150
     * @uses triggerEvent
151
     */
152
    public function erase(EntityInterface $entity)
153
    {
154
        $dependencies = $this->triggerEvent(DependencyResultEvent::DELETE, $entity);
155
        $this->repositories->remove($entity);
156
157
        return $dependencies;
158
    }
159
160
    /**
161
     * Helper function to trigger a DependecyResultEvent.
162
     *
163
     * @param string $name
164
     * @param EntityInterface $entity
165
     *
166
     * @return \Core\Service\EntityEraser\DependencyResultCollection
167
     */
168
    private function triggerEvent($name, EntityInterface $entity)
169
    {
170
        $params = $this->options;
171
        $params['entity'] = $entity;
172
        $params['repositories'] = $this->repositories;
173
174
        /* @var DependencyResultEvent $event */
175
        $event = $this->entityEraserEvents->getEvent($name, $this, $params);
176
177
        $this->entityEraserEvents->triggerEvent($event);
178
        $dependencies = $event->getDependencyResultCollection();
179
180
        return $dependencies;
181
    }
182
}
183