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 pathAttributeSetPlugin.php
118 lines (106 loc) · 3.41 KB
/
AttributeSetPlugin.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
<?php
namespace Ryvon\EventLog\Plugin;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set;
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 attribute sets.
*/
class AttributeSetPlugin
{
/**
* @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 Set $subject
* @param callable $proceed
* @param AbstractModel $object
* @return mixed
*/
public function aroundSave(
/** @noinspection PhpUnusedParameterInspection */ Set $subject,
callable $proceed,
AbstractModel $object
) {
// When an attribute set is created save ends up being called twice, the first time without an ID. We check
// for ID and ignore the first save rather than proceeding and obtaining the new ID to prevent duplicate
// entries in the log.
$id = $object->getData('attribute_set_id');
if ($id && $this->isEditingAttributeSet()) {
$this->eventManager->dispatch('event_log_info', [
'group' => 'admin',
'message' => 'Attribute Set {attribute-set} {action}.',
'context' => [
'attribute-set' => [
'text' => $object->getData('attribute_set_name'),
'id' => $id,
],
'action' => $object->isObjectNew() ? 'created' : 'modified',
],
]);
}
return $proceed($object);
}
/**
* Add an event log on delete.
*
* @param Set $subject
* @param AbstractModel $object
* @return array
*/
public function beforeDelete(
/** @noinspection PhpUnusedParameterInspection */ Set $subject,
AbstractModel $object
): array {
if ($this->isEditingAttributeSet()) {
$this->eventManager->dispatch('event_log_info', [
'group' => 'admin',
'message' => 'Attribute Set {attribute-set} {action}.',
'context' => [
'attribute-set' => [
'text' => $object->getData('attribute_set_name'),
'id' => $object->getData('attribute_set_id'),
],
'action' => 'deleted',
],
]);
}
return [$object];
}
/**
* Checks whether the current request is a post request to the attribute set edit page.
*
* @return bool
*/
private function isEditingAttributeSet(): bool
{
if (!$this->request instanceof Http) {
return false;
}
if (!$this->request->isPost() || !in_array($this->request->getFullActionName(), [
'catalog_product_set_save',
'catalog_product_set_delete',
])) {
return false;
}
return true;
}
}