Delete.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Sync\Contact;
  3. /**
  4. * Handle delete data for importer.
  5. */
  6. class Delete extends \Dotdigitalgroup\Email\Model\Sync\Contact\Bulk
  7. {
  8. /**
  9. * Sync.
  10. *
  11. * @param mixed $collection
  12. *
  13. * @return null
  14. */
  15. public function sync($collection)
  16. {
  17. foreach ($collection as $item) {
  18. $websiteId = $item->getWebsiteId();
  19. $email = $this->serializer->unserialize($item->getImportData());
  20. if ($this->helper->isEnabled($websiteId)) {
  21. $this->client = $this->helper->getWebsiteApiClient($websiteId);
  22. if ($this->client) {
  23. $apiContact = $this->client->postContacts($email);
  24. //apicontact found and can be removed using the contact id!
  25. if (! isset($apiContact->message) && isset($apiContact->id)) {
  26. //will assume that the request is done
  27. $this->client->deleteContact($apiContact->id);
  28. }
  29. $this->_handleSingleItemAfterSync($item, $apiContact);
  30. }
  31. }
  32. }
  33. }
  34. /**
  35. * @param mixed $item
  36. * @param mixed $apiContact
  37. *
  38. * @return null
  39. */
  40. public function _handleSingleItemAfterSync($item, $apiContact)
  41. {
  42. $curlError = $this->_checkCurlError($item);
  43. //no api connection error
  44. if (! $curlError) {
  45. //api response error
  46. if (isset($apiContact->message) or ! $apiContact) {
  47. $message = (isset($apiContact->message)) ? $apiContact->message : 'Error unknown';
  48. $item->setImportStatus(\Dotdigitalgroup\Email\Model\Importer::FAILED)
  49. ->setMessage($message);
  50. } else {
  51. $item->setImportStatus(\Dotdigitalgroup\Email\Model\Importer::IMPORTED)
  52. ->setImportFinished(time())
  53. ->setImportStarted(time())
  54. ->setMessage('');
  55. }
  56. $this->importerResource->save($item);
  57. }
  58. }
  59. }