-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProcessor.php
More file actions
179 lines (156 loc) · 5.32 KB
/
Processor.php
File metadata and controls
179 lines (156 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Smartbox\Integration\FrameworkBundle\Core\Processors;
use JMS\Serializer\Annotation as JMS;
use Smartbox\CoreBundle\Type\SerializableArray;
use Smartbox\Integration\FrameworkBundle\Core\Exchange;
use Smartbox\Integration\FrameworkBundle\Core\Processors\Exceptions\ProcessingException;
use Smartbox\Integration\FrameworkBundle\Events\ProcessEvent;
use Smartbox\Integration\FrameworkBundle\Service;
use Smartbox\Integration\FrameworkBundle\DependencyInjection\Traits\UsesEventDispatcher;
use Smartbox\Integration\FrameworkBundle\DependencyInjection\Traits\UsesValidator;
/**
* Class Processor.
*/
abstract class Processor extends Service implements ProcessorInterface
{
const TYPE = 'Processor';
const CONTEXT_PROCESSOR_ID = 'processor_id';
const CONTEXT_PROCESSOR_DESCRIPTION = 'processor_description';
use UsesValidator;
/**
* @var string
*
* @JMS\Expose
* @JMS\Type("string")
* @JMS\Groups({"logs"})
*/
protected $description = '';
/**
* @var bool
*/
protected $runtimeBreakpoint = false;
/**
* @param Exchange $exchange
* @param SerializableArray $processingContext
*/
abstract protected function doProcess(Exchange $exchange, SerializableArray $processingContext);
/**
* @param Exchange $exchange
* @param SerializableArray $processingContext
*/
final protected function preProcess(Exchange $exchange, SerializableArray $processingContext)
{
$event = $this->createProcessEvent($exchange, $processingContext, ProcessEvent::TYPE_BEFORE);
$this->onPreProcessEvent($event);
$this->getEventDispatcher()->dispatch(ProcessEvent::TYPE_BEFORE, $event);
}
/**
* Method to customize pre process event to add more specific information.
* This method is the entry point to do these customizations for the pre process event.
*
* @param ProcessEvent $event
*/
protected function onPreProcessEvent(ProcessEvent $event)
{
return;
}
/**
* @param Exchange $exchange
* @param SerializableArray $processingContext
*/
final protected function postProcess(Exchange $exchange, SerializableArray $processingContext)
{
$event = $this->createProcessEvent($exchange, $processingContext, ProcessEvent::TYPE_AFTER);
$this->onPostProcessEvent($event);
$this->getEventDispatcher()->dispatch(ProcessEvent::TYPE_AFTER, $event);
}
/**
* Method to customize post process event to add more specific information.
* This method is the entry point to do these customizations for the post process event.
*
* @param ProcessEvent $event
*/
protected function onPostProcessEvent(ProcessEvent $event)
{
return;
}
/**
* Method to create a process event.
*
* @param Exchange $exchange
* @param SerializableArray $processingContext
* @param string $type Event type
*
* @return ProcessEvent
*/
final protected function createProcessEvent(Exchange $exchange, SerializableArray $processingContext, $type)
{
$event = new ProcessEvent($type);
$event->setId(uniqid('', true));
$event->setTimestampToCurrent();
$event->setProcessor($this);
$event->setExchange($exchange);
$event->setProcessingContext($processingContext);
return $event;
}
/**
* {@inheritdoc}
*/
public function getDescription()
{
return $this->description;
}
/**
* {@inheritdoc}
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public function setRuntimeBreakpoint($runtimeBreakpoint)
{
$this->runtimeBreakpoint = (bool) $runtimeBreakpoint;
}
/**
* @throws ProcessingException
*/
final public function process(Exchange $exchange)
{
if ($this->runtimeBreakpoint && function_exists('xdebug_break')) {
xdebug_break();
}
/*
*
* DEBUGGING HINTS
*
* If you add a runtime breakpoint in a processor xml definition inside a flow you will break here.
*
* The following function calls preProcess, doProcess and postProcess contain the real code you want to debug.
*
* To debug in that way you can add this to your xml flow file, as part of the processor you want to debug:
*
* <... runtime-breakpoint="1"/>
*
*/
$processingContext = new SerializableArray();
try {
// Pre process event
$this->preProcess($exchange, $processingContext);
// Process
$res = $this->doProcess($exchange, $processingContext);
// Post process event
$this->postProcess($exchange, $processingContext);
} catch (\Exception $ex) {
$processingException = new ProcessingException();
$processingException->setProcessingContext($processingContext);
$processingException->setExchange($exchange);
$processingException->setOriginalException($ex);
$processingException->setProcessor($this);
throw $processingException;
}
return $res;
}
}