GroupPlugin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Config\Model\Config\Structure\Element\Group;
  8. use Vertex\Tax\Model\Config;
  9. use Vertex\Tax\Model\ModuleManager;
  10. /**
  11. * Hides likely unused tax classes from the store configuration
  12. *
  13. * @see Group
  14. */
  15. class GroupPlugin
  16. {
  17. /** @var Config */
  18. private $config;
  19. /** @var ModuleManager */
  20. private $moduleManager;
  21. /**
  22. * @param ModuleManager $moduleManager
  23. * @param Config $config
  24. */
  25. public function __construct(ModuleManager $moduleManager, Config $config)
  26. {
  27. $this->moduleManager = $moduleManager;
  28. $this->config = $config;
  29. }
  30. /**
  31. * Hides likely unused tax classes
  32. * MEQP2 Warning: Unused Parameter $subject necessary for plugins
  33. *
  34. * @see Group::setData()
  35. * @param Group $subject
  36. * @param \Closure $proceed
  37. * @param array $data
  38. * @param string $scope
  39. * @return mixed
  40. * @SuppressWarnings(PHPMD.UnusedFormalParameter) $subject is a necessary part of a plugin
  41. */
  42. public function aroundSetData(Group $subject, \Closure $proceed, $data, $scope)
  43. {
  44. if (!$this->config->isVertexActive() || !$this->config->isTaxCalculationEnabled()) {
  45. return $proceed($data, $scope);
  46. }
  47. $taxClasses = isset($data['path'], $data['id']) && $data['path'] === 'tax' && $data['id'] === 'classes';
  48. if ($taxClasses && !$this->moduleManager->isEnabled('Magento_GiftWrapping')) {
  49. $this->hide(
  50. $data,
  51. [
  52. 'giftwrap_order_class',
  53. 'giftwrap_order_code',
  54. 'giftwrap_item_class',
  55. 'giftwrap_item_code',
  56. 'printed_giftcard_class',
  57. 'printed_giftcard_code',
  58. ]
  59. );
  60. }
  61. if ($taxClasses && !$this->moduleManager->isEnabled('Magento_Reward')) {
  62. $this->hide(
  63. $data,
  64. [
  65. 'reward_points_class',
  66. 'reward_points_code',
  67. ]
  68. );
  69. }
  70. return $proceed($data, $scope);
  71. }
  72. /**
  73. * Updates the data array to hide a path
  74. *
  75. * @param array &$data
  76. * @param array $toHide
  77. */
  78. private function hide(array &$data, array $toHide)
  79. {
  80. if (isset($data['path'], $data['id']) && $data['path'] === 'tax' && $data['id'] === 'classes') {
  81. foreach ($toHide as $code) {
  82. if (is_array($data['children'][$code])) {
  83. $data['children'][$code]['showInDefault'] = 0;
  84. $data['children'][$code]['showInWebsite'] = 0;
  85. $data['children'][$code]['showInStore'] = 0;
  86. }
  87. }
  88. }
  89. }
  90. }