CustomerDataProviderPlugin.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Plugin;
  7. use Magento\Ui\DataProvider\AbstractDataProvider;
  8. use Vertex\Tax\Model\Config;
  9. use Vertex\Tax\Model\Repository\CustomerCodeRepository;
  10. /**
  11. * Ensures the Vertex Customer Code is available in the Customer Admin Form
  12. *
  13. * @see DataProvider
  14. */
  15. class CustomerDataProviderPlugin
  16. {
  17. /** @var Config */
  18. private $config;
  19. /** @var CustomerCodeRepository */
  20. private $repository;
  21. /**
  22. * @param CustomerCodeRepository $repository
  23. * @param Config $config
  24. */
  25. public function __construct(CustomerCodeRepository $repository, Config $config)
  26. {
  27. $this->repository = $repository;
  28. $this->config = $config;
  29. }
  30. /**
  31. * Load the Vertex Customer Code into the Customer Data Provider for use in the Admin form
  32. *
  33. * @see DataProvider::getData() Intercepted method
  34. * @param DataProvider $subject
  35. * @param array $data
  36. * @return array
  37. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  38. */
  39. public function afterGetData(AbstractDataProvider $subject, $data)
  40. {
  41. if (empty($data) || !$this->config->isVertexActive()) {
  42. return $data;
  43. }
  44. $customerIds = [];
  45. foreach ($data as $fieldData) {
  46. if (!isset($fieldData['customer']['entity_id'])) {
  47. continue;
  48. }
  49. $customerIds[] = $fieldData['customer']['entity_id'];
  50. }
  51. $customerCodes = $this->repository->getListByCustomerIds($customerIds);
  52. foreach ($data as $dataKey => $fieldData) {
  53. if (!isset($fieldData['customer']['entity_id'], $customerCodes[$fieldData['customer']['entity_id']])) {
  54. continue;
  55. }
  56. $entityId = $fieldData['customer']['entity_id'];
  57. $customerCode = $customerCodes[$entityId]->getCustomerCode();
  58. $data[$dataKey]['customer']['extension_attributes']['vertex_customer_code'] = $customerCode;
  59. }
  60. return $data;
  61. }
  62. }