Skip to content

Commit 201a3a0

Browse files
committed
Implemented suggested changes.
Fixes #94
1 parent a790eab commit 201a3a0

File tree

8 files changed

+146
-160
lines changed

8 files changed

+146
-160
lines changed

src/EventManager.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use LotGD\Core\Exceptions\ClassNotFoundException;
1212
use LotGD\Core\Exceptions\SubscriptionNotFoundException;
1313
use LotGD\Core\Exceptions\WrongTypeException;
14-
use LotGD\Core\Events\EventContextDataContainer;
14+
use LotGD\Core\Events\EventContextData;
1515

1616
/**
1717
* Manages a simple publish/subscribe system based on regular expressions
@@ -35,9 +35,10 @@ public function __construct(Game $g)
3535
* are run.
3636
*
3737
* @param string $event The name of the event to publish.
38-
* @param EventContextDataContainer $contextData The Data context
38+
* @param EventContextData $contextData The Data context
39+
* @return EventContextData The changed data.
3940
*/
40-
public function publish(string $event, EventContextDataContainer $contextData): EventContextDataContainer
41+
public function publish(string $event, EventContextData $contextData): EventContextData
4142
{
4243
// For right now, implement the naive approach of iterating every entry
4344
// in the subscription database, checking the regular expression. We
@@ -56,9 +57,9 @@ public function publish(string $event, EventContextDataContainer $contextData):
5657
$eventContext = new EventContext($event, $s->getPattern(), $contextData);
5758

5859
$returnedEventContext = $class::handleEvent($this->g, $eventContext);
59-
if ($returnedEventContext->hasDataChanged($contextData)) {
60-
$contextData = $returnedEventContext->getData();
61-
}
60+
// Overwrite contextData - contextData might be the same if nothing has changed,
61+
// or might reference a completely new object the event handler changed a value.
62+
$contextData = $returnedEventContext->getData();
6263
}
6364
}
6465

src/Events/EventContext.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
declare(strict_types=1);
33

44
namespace LotGD\Core\Events;
5-
use LotGD\Core\Exceptions\ArgumentException;
65

76

87
/**
@@ -20,12 +19,12 @@ class EventContext
2019
* EventContext constructor.
2120
* @param string $event The published event
2221
* @param string $matchingPattern The matching pattern
23-
* @param EventContextDataContainer $data
22+
* @param EventContextData $data
2423
*/
2524
public function __construct(
2625
string $event,
2726
string $matchingPattern,
28-
EventContextDataContainer $data
27+
EventContextData $data
2928
) {
3029
$this->event = $event;
3130
$this->matchingPattern = $matchingPattern;
@@ -62,9 +61,9 @@ public function hasDataType(string $type): bool
6261

6362
/**
6463
* Returns the immutable data container.
65-
* @return EventContextDataContainer
64+
* @return EventContextData
6665
*/
67-
public function getData(): EventContextDataContainer
66+
public function getData(): EventContextData
6867
{
6968
return $this->data;
7069
}
@@ -100,10 +99,10 @@ public function setDataFields($data)
10099

101100
/**
102101
* Checks if given original data is the same as currently held within this context.
103-
* @param EventContextDataContainer $originalData
102+
* @param EventContextData $originalData
104103
* @return bool
105104
*/
106-
public function hasDataChanged(EventContextDataContainer $originalData): bool
105+
public function hasDataChanged(EventContextData $originalData): bool
107106
{
108107
return $this->data === $originalData ? false : true;
109108
}

src/Events/EventContextData.php

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,134 @@
33

44
namespace LotGD\Core\Events;
55

6+
use LotGD\Core\Exceptions\ArgumentException;
67

78
/**
8-
* Data container for default, unspecific data without any sanitizing on the given fields.
9+
* EventContextDataContainer to provide a basic structure for managing contextual data of an event.
10+
*
11+
* This class must be immutable and returns always a new instance of itself for any change.
912
* @package LotGD\Core\Events
13+
* @immutable
1014
*/
11-
class EventContextData extends EventContextDataContainer
15+
class EventContextData
1216
{
17+
private $data;
1318

19+
/**
20+
* Creates a new instance of a data container.
21+
*
22+
* Sub types can change this method to force certain parameters.
23+
* @param array $data
24+
* @return EventContextData
25+
*/
26+
public static function create(array $data): self
27+
{
28+
return new static($data);
29+
}
30+
31+
/**
32+
* protected constructor..
33+
* @see self::create
34+
* @param array $data
35+
*/
36+
protected function __construct(array $data)
37+
{
38+
$this->data = $data;
39+
}
40+
41+
/**
42+
* Returns true if container has a certain field.
43+
* @param string $field
44+
* @return bool
45+
*/
46+
public function has(string $field): bool
47+
{
48+
return array_key_exists($field, $this->data);
49+
}
50+
51+
/**
52+
* Returns the value of a field.
53+
* @param string $field
54+
* @return mixed
55+
*/
56+
public function get(string $field)
57+
{
58+
if ($this->has($field)) {
59+
return $this->data[$field];
60+
} else {
61+
$this->throwException($field);
62+
}
63+
}
64+
65+
/**
66+
* Sets a field to a new value and returns a new data container
67+
* @param string $field
68+
* @param $value
69+
* @return EventContextData
70+
*/
71+
public function set(string $field, $value): self
72+
{
73+
if ($this->has($field)) {
74+
$data = $this->data;
75+
$data[$field] = $value;
76+
77+
return new static($data);
78+
} else {
79+
$this->throwException($field);
80+
}
81+
}
82+
83+
/**
84+
* Sets multiple fields at once
85+
* @param array $data array of $field=>$value pairs
86+
* @return EventContextData
87+
*/
88+
public function setFields(array $data): self
89+
{
90+
$data = $this->data;
91+
92+
foreach ($data as $field => $value) {
93+
if ($this->has($field)) {
94+
$data[$field] = $value;
95+
} else {
96+
$this->throwException($field);
97+
}
98+
}
99+
100+
return new static($data);
101+
}
102+
103+
/**
104+
* Returns a list of fields in this context
105+
* @return array
106+
*/
107+
private function getListOfFields(): array
108+
{
109+
return array_keys($this->data);
110+
}
111+
112+
/**
113+
* Returns a comma separated string with all allowed fields, for debugging reasons.
114+
* @return string
115+
*/
116+
private function getFormattedListOfFields(): string
117+
{
118+
return substr(
119+
implode(", ", $this->getListOfFields()),
120+
0,
121+
-2
122+
);
123+
}
124+
125+
/**
126+
* internal use only - throws an ArgumentException a field is given that's not valid.
127+
* @param $field
128+
* @throws ArgumentException
129+
*/
130+
private function throwException($field)
131+
{
132+
throw new ArgumentException(
133+
"{$field} is not valid in this context, only {$this->getFormattedListOfFields()} are allowed."
134+
);
135+
}
14136
}

src/Events/EventContextDataContainer.php

Lines changed: 0 additions & 136 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* redirect Scene|null
2424
* @package LotGD\Core\Events
2525
*/
26-
class NavigateToScene extends EventContextDataContainer
26+
class NavigateToSceneData extends EventContextData
2727
{
2828
/**
2929
* NavigateToScene constructor.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* scene Scene|null
1717
* @package LotGD\Core\Events
1818
*/
19-
class NewViewpoint extends EventContextDataContainer
19+
class NewViewpointData extends EventContextData
2020
{
2121
/**
2222
* NewViewpoint constructor.

0 commit comments

Comments
 (0)