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 pathAttributePlugin.php
121 lines (107 loc) · 3.37 KB
/
AttributePlugin.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
<?php
namespace Ryvon\EventLog\Plugin;
use Magento\Catalog\Model\ResourceModel\Attribute;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Model\AbstractModel;
/**
* Plugin to monitor the saving and deletion of attributes.
*/
class AttributePlugin
{
/**
* @var ManagerInterface
*/
private $eventManager;
/**
* @var RequestInterface
*/
private $request;
/**
* @param ManagerInterface $eventManager
* @param RequestInterface $request
*/
public function __construct(ManagerInterface $eventManager, RequestInterface $request)
{
$this->eventManager = $eventManager;
$this->request = $request;
}
/**
* Wrap the save function to add an event log on save and create.
*
* @param Attribute $subject
* @param callable $proceed
* @param AbstractModel $object
* @return mixed
*/
public function aroundSave(
/** @noinspection PhpUnusedParameterInspection */ Attribute $subject,
callable $proceed,
AbstractModel $object
) {
if ($this->isEditingAttribute()) {
// We proceed with the save to obtain the ID in case this is a new attribute.
$return = $proceed($object);
$this->eventManager->dispatch('event_log_info', [
'group' => 'admin',
'message' => 'Attribute {attribute} {action}.',
'context' => [
'attribute' => [
'text' => $object->getData('attribute_code'),
'id' => $object->getData('attribute_id'),
],
'action' => $object->isObjectNew() ? 'created' : 'modified',
],
]);
return $return;
}
return $proceed($object);
}
/**
* Add an event log on delete.
*
* @param Attribute $subject
* @param AbstractModel $object
* @return array
*/
public function beforeDelete(
/** @noinspection PhpUnusedParameterInspection */ Attribute $subject,
AbstractModel $object
): array {
if ($this->isEditingAttribute()) {
$this->eventManager->dispatch('event_log_info', [
'group' => 'admin',
'message' => 'Attribute {attribute} {action}.',
'context' => [
'attribute' => [
'text' => $object->getData('attribute_code'),
'id' => $object->getData('attribute_id'),
],
'action' => 'deleted',
],
]);
}
return [$object];
}
/**
* Checks whether the current request is a post request to the attribute edit page.
*
* This is needed due to the configuration modifying the attributes on save (catalog settings).
*
* @return bool
*/
private function isEditingAttribute(): bool
{
if (!$this->request instanceof Http) {
return false;
}
if (!$this->request->isPost() || !in_array($this->request->getFullActionName(), [
'catalog_product_attribute_save',
'catalog_product_attribute_delete',
])) {
return false;
}
return true;
}
}