-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathManager.php
140 lines (117 loc) · 4.21 KB
/
Manager.php
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
<?php
/**
* @author Willem Poortman <[email protected]>
* @package MageHook_Hook
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace MageHook\Hook;
use Exception;
use MageHook\Hook\Helper\Config\Data as ConfigHelper;
use MageHook\Hook\Helper\Events as EventsHelper;
use MageHook\Hook\Registry\DispatchTimestamp as DispatchTimestampRegistry;
use Magento\Framework\DataObjectFactory;
use Magento\Framework\Event\InvokerInterface;
use Magento\Framework\Event\WrapperFactory;
use Magento\Framework\EventFactory;
use Magento\Framework\Profiler;
use Psr\Log\LoggerInterface;
/**
* Class Manager
*
* @package MageHook\Hook
*/
class Manager implements ManagerInterface
{
/** @var InvokerInterface $invoker */
protected $invoker;
/** @var EventFactory $eventFactory */
protected $eventFactory;
/** @var DataObjectFactory $dataObjectFactory */
protected $dataObjectFactory;
/** @var WrapperFactory $wrapperFactory */
protected $wrapperFactory;
/** @var ConfigHelper $configHelper */
protected $configHelper;
/** @var EventsHelper $eventsHelper */
protected $eventsHelper;
/** @var DispatchTimestampRegistry $dispatchTimestampRegistry */
protected $dispatchTimestampRegistry;
/** @var LoggerInterface $loggerInterface */
protected $loggerInterface;
/**
* Manager constructor.
*
* @param InvokerInterface $invoker
* @param EventFactory $eventFactory
* @param DataObjectFactory $dataObjectFactory
* @param WrapperFactory $wrapperFactory
* @param ConfigHelper $configHelper
* @param EventsHelper $eventsHelper
* @param DispatchTimestampRegistry $dispatchTimestampRegistry
* @param LoggerInterface $loggerInterface
*/
public function __construct(
InvokerInterface $invoker,
EventFactory $eventFactory,
DataObjectFactory $dataObjectFactory,
WrapperFactory $wrapperFactory,
ConfigHelper $configHelper,
EventsHelper $eventsHelper,
DispatchTimestampRegistry $dispatchTimestampRegistry,
LoggerInterface $loggerInterface
) {
$this->invoker = $invoker;
$this->eventFactory = $eventFactory;
$this->dataObjectFactory = $dataObjectFactory;
$this->wrapperFactory = $wrapperFactory;
$this->configHelper = $configHelper;
$this->eventsHelper = $eventsHelper;
$this->dispatchTimestampRegistry = $dispatchTimestampRegistry;
$this->loggerInterface = $loggerInterface;
}
/**
* {@inheritDoc}
*/
public function dispatch($event, $data, $metadata = []): void
{
if (!$this->configHelper->isActive()) {
return;
}
$this->dispatchTimestampRegistry->set();
$eventName = mb_strtolower($event);
Profiler::start('WEBHOOK:' . $event, [
'group' => 'WEBHOOK', 'name' => $event,
]);
$searchEvent = $this->eventsHelper->getByEvent($eventName);
if ($searchEvent) {
$hook = $this->eventFactory->create(['converter_data' => $data]);
$hook->setName($event);
$wrapper = $this->wrapperFactory->create();
$wrapper->setData([
self::WRAPPER_DK_EVENT => $searchEvent,
self::WRAPPER_DK_WEBHOOK => $hook,
self::WRAPPER_DK_BODY => $data,
self::WRAPPER_DK_RESOURCE => $data,
self::WRAPPER_DK_METADATA => $metadata
]);
Profiler::start('WEBHOOK:' . $searchEvent[EventsHelper::EVENT_NAME]);
try {
$this->invoker->dispatch($searchEvent, $wrapper);
} catch (Exception $exception) {
$this->loggerInterface->critical($exception->getMessage());
}
Profiler::stop('WEBHOOK:' . $searchEvent[EventsHelper::EVENT_NAME]);
}
Profiler::stop('WEBHOOK:' . $event);
}
/**
* {@inheritDoc}
*/
public function fire($event, $data, $metadata = []): void
{
$this->dispatch($event, $data, $metadata);
}
}