Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
EventAggregator
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 put
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 triggerAggregatedEvents
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getEventManager
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2020 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\oatbox\event;
24
25use oat\oatbox\log\LoggerAwareTrait;
26use oat\oatbox\service\ConfigurableService;
27use Psr\Log\LoggerAwareInterface;
28
29class EventAggregator extends ConfigurableService implements LoggerAwareInterface
30{
31    use LoggerAwareTrait;
32
33    public const SERVICE_ID = 'generis/eventAggregator';
34
35    /** @var int */
36    private $numberOfAggregatedEvents;
37
38    /** @var array */
39    private $events = [];
40
41    public function __construct(array $options = [])
42    {
43        parent::__construct($options);
44
45        $this->numberOfAggregatedEvents = $this->getOption('numberOfAggregatedEvents', 10);
46    }
47
48    public function put(string $eventId, Event $event): void
49    {
50        $this->events[$eventId] = $event;
51
52        if (count($this->events) >= $this->numberOfAggregatedEvents) {
53            $this->triggerAggregatedEvents();
54        }
55    }
56
57    public function triggerAggregatedEvents(): void
58    {
59        $countEvents = count($this->events);
60
61        if ($countEvents < 1) {
62            return;
63        }
64
65        $this->logInfo(sprintf('Triggering %d aggregated events', $countEvents));
66
67        $eventManager = $this->getEventManager();
68        foreach ($this->events as $event) {
69            $eventManager->trigger($event);
70        }
71
72        $this->events = [];
73    }
74
75    public function getEventManager(): EventManager
76    {
77        return $this->getServiceLocator()->get(EventManager::SERVICE_ID);
78    }
79}