Passed
Push — master ( 04bd86...953339 )
by Marcel
06:54
created

DatasourceEvent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace OCA\Analytics\Datasource;
4
5
use OCP\EventDispatcher\Event;
6
7
/**
8
 * Class CommentsEntityEvent
9
 *
10
 * @since 9.1.0
11
 */
12
class DatasourceEvent extends Event
13
{
14
    public const EVENT_ENTITY = 'OCA\Analytics\Datasource\ICommentsManager::registerEntity';
15
16
    /** @var string */
17
    protected $event;
18
    /** @var \Closure[] */
19
    protected $collections;
20
21
    /**
22
     * DispatcherEvent constructor.
23
     *
24
     * @param string $event
25
     * @since 9.1.0
26
     */
27
    public function __construct($event)
28
    {
29
        $this->event = $event;
30
        $this->collections = [];
31
    }
32
33
    /**
34
     * @param string $name
35
     * @param \Closure $entityExistsFunction The closure should take one
36
     *                 argument, which is the id of the entity, that comments
37
     *                 should be handled for. The return should then be bool,
38
     *                 depending on whether comments are allowed (true) or not.
39
     * @throws \OutOfBoundsException when the entity name is already taken
40
     * @since 9.1.0
41
     */
42
    public function addEntityCollection($name, \Closure $entityExistsFunction)
43
    {
44
        if (isset($this->collections[$name])) {
45
            throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"');
46
        }
47
48
        $this->collections[$name] = $entityExistsFunction;
49
    }
50
51
    /**
52
     * @return \Closure[]
53
     * @since 9.1.0
54
     */
55
    public function getEntityCollections()
56
    {
57
        return $this->collections;
58
    }
59
}