EventCollection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addEvent() 0 17 4
A getEvents() 0 3 1
A setEvents() 0 8 3
1
<?php
2
/**
3
 * @link https://github.com/Izumi-kun/yii2-longpoll
4
 * @copyright Copyright (c) 2017 Viktor Khokhryakov
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace izumi\longpoll;
9
10
use Yii;
11
use yii\base\BaseObject;
12
use yii\base\InvalidConfigException;
13
14
/**
15
 * Class EventCollection
16
 * @property EventInterface[] $events array of events.
17
 * @author Viktor Khokhryakov <[email protected]>
18
 */
19
class EventCollection extends BaseObject implements EventCollectionInterface
20
{
21
    /**
22
     * @var EventInterface[] array of events (key => event).
23
     */
24
    private $_events = [];
25
    /**
26
     * @var string event class name.
27
     */
28
    public $eventClass = Event::class;
29
30
    /**
31
     * @inheritdoc
32
     * @throws InvalidConfigException
33
     */
34 24
    public function addEvent($event)
35
    {
36 24
        if (!$event instanceof EventInterface) {
37 16
            if (!is_array($event)) {
38
                $event = [
39 14
                    'class' => $this->eventClass,
40 14
                    'key' => $event,
41
                ];
42
            }
43 16
            $event = Yii::createObject($event);
44 16
            if (!$event instanceof EventInterface) {
45 1
                throw new InvalidConfigException('The event should be an instance of "\izumi\longpoll\EventInterface".');
46
            }
47
        }
48 23
        $this->_events[$event->getKey()] = $event;
49
50 23
        return $event;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 24
    public function getEvents()
57
    {
58 24
        return $this->_events;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     * @throws InvalidConfigException
64
     */
65 26
    public function setEvents($events)
66
    {
67 26
        $this->_events = [];
68 26
        if (!is_array($events)) {
69 7
            $events = [$events];
70
        }
71 26
        foreach ($events as $event) {
72 18
            $this->addEvent($event);
73
        }
74 25
    }
75
}
76