Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
outgoingeventmanager.php
1<?php
2
4
13
15{
16 use Sync\Internals\HasContextTrait;
17
18 private array $map = [];
19
20 private EventManager $eventManager;
21
25 public function __construct(Office365Context $context)
26 {
27 $this->context = $context;
28 $this->eventManager = $this->context->getEventManager();
29 parent::__construct($context->getConnection());
30 }
31
44 public function export(
45 Sync\Entities\SyncEventMap $syncEventMap,
46 Sync\Entities\SyncSectionMap $syncSectionMap
47 ): Sync\Util\Result
48 {
49 $result = new Sync\Util\Result();
50 $result->setData([
51 'syncEventMap' => $syncEventMap,
52 ]);
53
55 foreach ($syncEventMap as $syncEvent)
56 {
57 if (
58 $syncEvent->getEventConnection()
59 && ($syncEvent->getEvent()->getVersion() === $syncEvent->getEventConnection()->getVersion())
60 )
61 {
62 continue;
63 }
64
65 if ($syncEvent->isRecurrence())
66 {
67 $this->saveRecurrence($syncEvent, $syncSectionMap);
68 }
69 else
70 {
71 $this->saveSingle($syncEvent, $syncSectionMap);
72 }
73 }
74
75 return new Result();
76 }
77
84 private function getSyncSection(SyncSectionMap $syncSectionMap, int $id): ?SyncSection
85 {
86 if (!array_key_exists($id, $this->map))
87 {
88 $this->map[$id] = null;
90 foreach ($syncSectionMap as $syncSection)
91 {
92 if ($syncSection->getSection()->getId() === $id)
93 {
94 $this->map[$id] = $syncSection;
95 break;
96 }
97 }
98 }
99
100 return $this->map[$id];
101 }
102
115 private function saveSingle(SyncEvent $syncEvent, SyncSectionMap $syncSectionMap)
116 {
117 if ($syncEvent->getEventConnection() && $syncEvent->getEventConnection()->getVendorEventId())
118 {
119 $context = (new Sync\Util\EventContext())
120 ->setEventConnection($syncEvent->getEventConnection())
121 ;
122 try
123 {
124 $result = $this->eventManager->update(
125 $syncEvent->getEvent(),
126 $context
127 );
128 if ($result->isSuccess())
129 {
130 $data = $result->getData();
131 $syncEvent
132 ->setAction(Sync\Dictionary::SYNC_STATUS['success'])
133 ->getEventConnection()
134 ->setEntityTag($data['event']['etag'])
135 ->setVendorVersionId($data['event']['version'])
136 ->setLastSyncStatus(Sync\Dictionary::SYNC_STATUS['success'])
137 ;
138 }
139 }
140 catch(Sync\Exceptions\NotFoundException $e)
141 {
142 $syncEvent
143 ->setAction(Sync\Dictionary::SYNC_STATUS['success'])
144 ->getEventConnection()
145 ->setLastSyncStatus(Sync\Dictionary::SYNC_STATUS['deleted'])
146 ;
147 }
148 }
149 else
150 {
151 $syncSection = $this->getSyncSection(
152 $syncSectionMap,
153 $syncEvent->getEvent()->getSection()->getId()
154 );
155 if ($syncSection && $syncSection->getSectionConnection()->isActive())
156 {
157 $context = (new Sync\Util\EventContext())
158 ->setSectionConnection($syncSection->getSectionConnection())
159 ;
160 try
161 {
162 $result = $this->eventManager->create(
163 $syncEvent->getEvent(),
164 $context
165 );
166 if ($result->isSuccess())
167 {
168 $data = $result->getData();
169 $eventConnection = (new Sync\Connection\EventConnection())
170 ->setEvent($syncEvent->getEvent())
171 ->setConnection($syncSection->getSectionConnection()->getConnection())
172 ->setVendorEventId($data['event']['id'])
173 ->setVendorVersionId($data['event']['version'])
174 ->setEntityTag($data['event']['etag'])
175 ->setVersion($syncEvent->getEvent()->getVersion())
176 ->setLastSyncStatus(Sync\Dictionary::SYNC_STATUS['success'])
177 ;
178 $syncEvent
179 ->setAction(Sync\Dictionary::SYNC_STATUS['success'])
180 ->setEventConnection($eventConnection);
181 }
182 }
183 catch(Sync\Exceptions\NotFoundException $e)
184 {
185 $syncSection->getSectionConnection()->setActive(false);
186 $syncEvent->setAction(Sync\Dictionary::SYNC_STATUS['success']);
187 }
188 }
189 }
190 }
191
202 private function saveRecurrence(SyncEvent $syncEvent, SyncSectionMap $syncSectionMap)
203 {
204 $syncSection = $this->getSyncSection(
205 $syncSectionMap,
206 $syncEvent->getEvent()->getSection()->getId()
207 );
208 $context = (new Sync\Util\EventContext())
209 ->setSectionConnection($syncSection->getSectionConnection());
210 if ($syncEvent->getEventConnection() && $syncEvent->getEventConnection()->getVendorEventId())
211 {
212 $context->setEventConnection($syncEvent->getEventConnection());
213 $this->eventManager->updateRecurrence(
214 $syncEvent,
215 $syncSection->getSectionConnection(),
216 $context
217 );
218 }
219 else
220 {
221 $this->eventManager->createRecurrence(
222 $syncEvent,
223 $syncSection->getSectionConnection(),
224 $context
225 );
226 }
227 }
228}
export(Sync\Entities\SyncEventMap $syncEventMap, Sync\Entities\SyncSectionMap $syncSectionMap)