|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace R\Hive\Concrete\Factories; |
|
4
|
|
|
|
|
5
|
|
|
use R\Hive\Concrete\Data\Message; |
|
6
|
|
|
use R\Hive\Contracts\Data\MutatorInterface; |
|
7
|
|
|
use R\Hive\Contracts\Data\ValidatorInterface; |
|
8
|
|
|
use R\Hive\Contracts\Factories\FactoryInterface; |
|
9
|
|
|
use R\Hive\Contracts\Handlers\OnCreateInterface; |
|
10
|
|
|
use R\Hive\Contracts\Handlers\OnUpdateInterface; |
|
11
|
|
|
use R\Hive\Contracts\Instances\InstanceInterface; |
|
12
|
|
|
use R\Hive\Contracts\Observers\ObservatoryInterface; |
|
13
|
|
|
|
|
14
|
|
|
abstract class Factory implements FactoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Report success to the observatory and call the handler event. |
|
18
|
|
|
* |
|
19
|
|
|
* @param InstanceInterface $instance The instance. |
|
20
|
|
|
* @param bool $is_update If this event was part of an update. |
|
21
|
|
|
* @param ObservatoryInterface|null $observatory Optional observatory. |
|
22
|
|
|
* |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function reportSuccess( |
|
26
|
|
|
InstanceInterface $instance, |
|
27
|
|
|
$is_update, |
|
28
|
|
|
ObservatoryInterface $observatory = null |
|
29
|
|
|
) { |
|
30
|
|
|
// Notify the observatory if one exists. |
|
31
|
|
|
if ($observatory !== null) { |
|
32
|
|
|
if ($is_update) { |
|
33
|
|
|
$observatory->notifyOnUpdateSucceeded($instance); |
|
34
|
|
|
} else { |
|
35
|
|
|
$observatory->notifyOnCreateSucceeded($instance); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Validate the given data. |
|
42
|
|
|
* |
|
43
|
|
|
* @param ValidatorInterface $validator The associated validator. |
|
44
|
|
|
* @param MutatorInterface $mutator The data mutator. |
|
45
|
|
|
* @param bool $is_update Whether this data is for an update event. |
|
46
|
|
|
* @param ObservatoryInterface|null $observatory An optional observatory. |
|
47
|
|
|
* |
|
48
|
|
|
* @return Message|null A failure message or null. |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function validate( |
|
51
|
|
|
ValidatorInterface $validator, |
|
52
|
|
|
MutatorInterface $mutator, |
|
53
|
|
|
$is_update, |
|
54
|
|
|
ObservatoryInterface $observatory = null |
|
55
|
|
|
) { |
|
56
|
|
|
// First, validate the supplied data for either and update |
|
57
|
|
|
// or create event. |
|
58
|
|
|
if ($is_update) { |
|
59
|
|
|
$validator->markAsUpdate()->validate($mutator); |
|
60
|
|
|
} else { |
|
61
|
|
|
$validator->validate($mutator); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// If there are validation errors, create the associated |
|
65
|
|
|
// failure message, notify the observatory if one exsits |
|
66
|
|
|
// and return the message. |
|
67
|
|
|
if ($validator->hasErrors()) { |
|
68
|
|
|
$message = new Message('Failed to validate supplied data.', $validator); |
|
69
|
|
|
|
|
70
|
|
|
if ($observatory !== null) { |
|
71
|
|
|
if ($is_update) { |
|
72
|
|
|
$observatory->notifyOnUpdateFailed($message); |
|
73
|
|
|
} else { |
|
74
|
|
|
$observatory->notifyOnCreateFailed($message); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $message; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// If everything went well, return null. |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
abstract public function make( |
|
86
|
|
|
OnCreateInterface $handler, |
|
87
|
|
|
MutatorInterface $mutator, |
|
88
|
|
|
ObservatoryInterface $observatory = null |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
abstract public function update( |
|
92
|
|
|
OnUpdateInterface $handler, |
|
93
|
|
|
InstanceInterface $instance, |
|
94
|
|
|
MutatorInterface $mutator, |
|
95
|
|
|
ObservatoryInterface $observatory = null |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|