Scene::__construct()   A
last analyzed

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
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * NextFlow (http://github.com/nextflow)
4
 *
5
 * @link http://github.com/nextflow/nextflow-php for the canonical source repository
6
 * @copyright Copyright (c) 2014-2016 NextFlow (http://github.com/nextflow)
7
 * @license https://raw.github.com/nextflow/nextflow-php/master/LICENSE MIT
8
 */
9
10
namespace NextFlow\Core\Scene;
11
12
use NextFlow\Core\Event\EventInterface;
13
14
/**
15
 * A scene that contains event nodes that can be executed.
16
 */
17
final class Scene implements SceneInterface
18
{
19
    /**
20
     * The name of the scene.
21
     *
22
     * @var string
23
     */
24
    private $name;
25
26
    /**
27
     * The list with events that can be executed.
28
     *
29
     * @var EventInterface[]
30
     */
31
    private $events;
32
33
    /**
34
     * Initializes a new instance of this class.
35
     */
36
    public function __construct()
37
    {
38
        $this->events = array();
39
    }
40
41
    /**
42
     * Executes the event with the given name.
43
     *
44
     * @param string $eventName The name of the event to execute.
45
     * @return int Returns the amount of events that are executed.
46
     */
47
    public function execute($eventName)
48
    {
49
        $result = 0;
50
51
        foreach ($this->events as $event) {
52
            if ($event->getName() == $eventName) {
53
                $event->execute();
54
                $result++;
55
            }
56
        }
57
58
        return $result;
59
    }
60
61
    /**
62
     * Adds the given event to this scene.
63
     *
64
     * @param EventInterface $event
65
     */
66
    public function addEvent(EventInterface $event)
67
    {
68
        if (!in_array($event, $this->events)) {
69
            $this->events[] = $event;
70
        }
71
    }
72
73
    /**
74
     * Clears all the events within the scene.
75
     */
76
    public function clearEvents()
77
    {
78
        $this->events = array();
79
    }
80
81
    /**
82
     * Gets a list with all events that are registered.
83
     *
84
     * @return EventInterface[]
85
     */
86
    public function getEvents()
87
    {
88
        return $this->events;
89
    }
90
91
    /**
92
     * Gets the name of the scene.
93
     *
94
     * @return string
95
     */
96
    public function getName()
97
    {
98
        return $this->name;
99
    }
100
101
    /**
102
     * Removes the given event from the scene.
103
     *
104
     * @param EventInterface $event The event to remove.
105
     */
106
    public function removeEvent(EventInterface $event)
107
    {
108
        $key = array_search($event, $this->events, true);
109
        if ($key !== false) {
110
            unset($this->events[$key]);
111
        }
112
    }
113
114
    /**
115
     * Sets the name of the scene.
116
     *
117
     * @param string $name The name to set.
118
     */
119
    public function setName($name)
120
    {
121
        $this->name = (string)$name;
122
    }
123
}
124