This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionMonitorPlugin.php
62 lines (55 loc) · 1.61 KB
/
ActionMonitorPlugin.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
<?php
namespace Ryvon\EventLog\Plugin;
use Exception;
use Magento\Backend\App\AbstractAction;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Psr\Log\LoggerInterface;
use Ryvon\EventLog\Observer\Action\ActionObserverInterface;
/**
* Plugin to monitor for specific actions to be handled by ActionObserverInterface instances.
*/
class ActionMonitorPlugin
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var array
*/
private $monitors;
/**
* @param LoggerInterface $logger
* @param array $monitors
*/
public function __construct(LoggerInterface $logger, $monitors = [])
{
$this->logger = $logger;
$this->monitors = $monitors;
}
/**
* Calls the handle function of any ActionObserverInterface found for the current request's getFullActionName.
*
* @param AbstractAction $subject
* @param RequestInterface $request
* @return array
*/
public function beforeDispatch(
/** @noinspection PhpUnusedParameterInspection */ AbstractAction $subject,
RequestInterface $request
): array {
try {
if (($request instanceof Http) && $request->isPost()) {
$action = $request->getFullActionName();
$monitor = $this->monitors[$action] ?? null;
if ($monitor && $monitor instanceof ActionObserverInterface) {
$monitor->handle($request);
}
}
} catch (Exception $e) {
$this->logger->critical($e);
}
return [$request];
}
}