forked from mageplaza/magento-2-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AfterSave.php
173 lines (160 loc) · 5.05 KB
/
AfterSave.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
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
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Webhook
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Webhook\Observer;
use Exception;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Mageplaza\Webhook\Helper\Data;
use Mageplaza\Webhook\Model\Config\Source\Schedule;
use Mageplaza\Webhook\Model\CronScheduleFactory;
use Mageplaza\Webhook\Model\HookFactory;
/**
* Class AfterSave
* @package Mageplaza\Webhook\Observer
*/
abstract class AfterSave implements ObserverInterface
{
/**
* @var HookFactory
*/
protected $hookFactory;
/**
* @var CronScheduleFactory
*/
protected $scheduleFactory;
/**
* @var Data
*/
protected $helper;
/**
* @var string
*/
protected $hookType = '';
/**
* @var string
*/
protected $hookTypeUpdate = '';
/**
* @var ManagerInterface
*/
protected $messageManager;
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* AfterSave constructor.
*
* @param HookFactory $hookFactory
* @param CronScheduleFactory $cronScheduleFactory
* @param ManagerInterface $messageManager
* @param StoreManagerInterface $storeManager
* @param Data $helper
*/
public function __construct(
HookFactory $hookFactory,
CronScheduleFactory $cronScheduleFactory,
ManagerInterface $messageManager,
StoreManagerInterface $storeManager,
Data $helper
) {
$this->hookFactory = $hookFactory;
$this->helper = $helper;
$this->scheduleFactory = $cronScheduleFactory;
$this->messageManager = $messageManager;
$this->storeManager = $storeManager;
}
/**
* @param Observer $observer
*
* @throws Exception
*/
public function execute(Observer $observer)
{
$item = $observer->getDataObject();
$schedule = $this->helper->getCronSchedule();
if ($schedule !== Schedule::DISABLE && $schedule !== null) {
$hookCollection = $this->hookFactory->create()->getCollection()
->addFieldToFilter('hook_type', $this->hookType)
->addFieldToFilter('status', 1)
->addFieldToFilter('store_ids', [
['finset' => Store::DEFAULT_STORE_ID],
['finset' => $this->storeManager->getStore()->getId()]
])
->setOrder('priority', 'ASC');
if ($hookCollection->getSize() > 0) {
$schedule = $this->scheduleFactory->create();
$data = [
'hook_type' => $this->hookType,
'event_id' => $item->getId(),
'status' => '0'
];
try {
$schedule->addData($data);
$schedule->save();
} catch (Exception $exception) {
$this->messageManager->addError($exception->getMessage());
}
}
} else {
$this->helper->send($item, $this->hookType);
}
}
/**
* @param $observer
*
* @throws Exception
*/
protected function updateObserver($observer)
{
$item = $observer->getDataObject();
$schedule = $this->helper->getCronSchedule();
if ($schedule !== Schedule::DISABLE && $schedule !== null) {
$hookCollection = $this->hookFactory->create()->getCollection()
->addFieldToFilter('hook_type', $this->hookType)
->addFieldToFilter('status', 1)
->addFieldToFilter('store_ids', [
['finset' => Store::DEFAULT_STORE_ID],
['finset' => $this->storeManager->getStore()->getId()]
])
->setOrder('priority', 'ASC');
if ($hookCollection->getSize() > 0) {
$schedule = $this->scheduleFactory->create();
$data = [
'hook_type' => $this->hookTypeUpdate,
'event_id' => $item->getId(),
'status' => '0'
];
try {
$schedule->addData($data);
$schedule->save();
} catch (Exception $exception) {
$this->messageManager->addError($exception->getMessage());
}
}
} else {
$this->helper->send($item, $this->hookTypeUpdate);
}
}
}