PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/GroupedProduct/Model/Product/Initialization/Helper/ProductLinks/Plugin/Grouped.php

https://gitlab.com/svillegas/magento2
PHP | 152 lines | 94 code | 11 blank | 47 comment | 14 complexity | 6005f73b32c8d99b81ac5dbb4e57143e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GroupedProduct\Model\Product\Initialization\Helper\ProductLinks\Plugin;
  7. use Magento\Catalog\Api\Data\ProductLinkExtensionFactory;
  8. use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\GroupedProduct\Model\Product\Type\Grouped as TypeGrouped;
  11. /**
  12. * Class Grouped
  13. */
  14. class Grouped
  15. {
  16. /**
  17. * String name for link type
  18. */
  19. const TYPE_NAME = 'associated';
  20. /**
  21. * @var ProductLinkInterfaceFactory
  22. */
  23. protected $productLinkFactory;
  24. /**
  25. * @var ProductRepositoryInterface
  26. */
  27. protected $productRepository;
  28. /**
  29. * @var ProductLinkExtensionFactory
  30. */
  31. protected $productLinkExtensionFactory;
  32. /**
  33. * Init
  34. *
  35. * @param ProductLinkInterfaceFactory $productLinkFactory
  36. * @param ProductRepositoryInterface $productRepository
  37. * @param ProductLinkExtensionFactory $productLinkExtensionFactory
  38. */
  39. public function __construct(
  40. ProductLinkInterfaceFactory $productLinkFactory,
  41. ProductRepositoryInterface $productRepository,
  42. ProductLinkExtensionFactory $productLinkExtensionFactory
  43. ) {
  44. $this->productLinkFactory = $productLinkFactory;
  45. $this->productRepository = $productRepository;
  46. $this->productLinkExtensionFactory = $productLinkExtensionFactory;
  47. }
  48. /**
  49. * Initialize grouped product links
  50. *
  51. * @param \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $subject
  52. * @param \Magento\Catalog\Model\Product $product
  53. * @param array $links
  54. *
  55. * @return \Magento\Catalog\Model\Product
  56. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  57. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  58. * @SuppressWarnings(PHPMD.NPathComplexity)
  59. */
  60. public function beforeInitializeLinks(
  61. \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $subject,
  62. \Magento\Catalog\Model\Product $product,
  63. array $links
  64. ) {
  65. if ($product->getTypeId() === TypeGrouped::TYPE_CODE && !$product->getGroupedReadonly()) {
  66. $links = (isset($links[self::TYPE_NAME])) ? $links[self::TYPE_NAME] : $product->getGroupedLinkData();
  67. if (!is_array($links)) {
  68. $links = [];
  69. }
  70. if ($product->getGroupedLinkData()) {
  71. $links = array_merge($links, $product->getGroupedLinkData());
  72. }
  73. $newLinks = [];
  74. $existingLinks = $product->getProductLinks();
  75. foreach ($links as $linkRaw) {
  76. /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
  77. $productLink = $this->productLinkFactory->create();
  78. if (!isset($linkRaw['id'])) {
  79. continue;
  80. }
  81. $productId = $linkRaw['id'];
  82. if (!isset($linkRaw['qty'])) {
  83. $linkRaw['qty'] = 0;
  84. }
  85. $linkedProduct = $this->productRepository->getById($productId);
  86. $productLink->setSku($product->getSku())
  87. ->setLinkType(self::TYPE_NAME)
  88. ->setLinkedProductSku($linkedProduct->getSku())
  89. ->setLinkedProductType($linkedProduct->getTypeId())
  90. ->setPosition($linkRaw['position'])
  91. ->getExtensionAttributes()
  92. ->setQty($linkRaw['qty']);
  93. if (isset($linkRaw['custom_attributes'])) {
  94. $productLinkExtension = $productLink->getExtensionAttributes();
  95. if ($productLinkExtension === null) {
  96. $productLinkExtension = $this->productLinkExtensionFactory->create();
  97. }
  98. foreach ($linkRaw['custom_attributes'] as $option) {
  99. $name = $option['attribute_code'];
  100. $value = $option['value'];
  101. $setterName = 'set' . ucfirst($name);
  102. // Check if setter exists
  103. if (method_exists($productLinkExtension, $setterName)) {
  104. call_user_func([$productLinkExtension, $setterName], $value);
  105. }
  106. }
  107. $productLink->setExtensionAttributes($productLinkExtension);
  108. }
  109. $newLinks[] = $productLink;
  110. }
  111. $existingLinks = $this->removeUnExistingLinks($existingLinks, $newLinks);
  112. $product->setProductLinks(array_merge($existingLinks, $newLinks));
  113. }
  114. }
  115. /**
  116. * Removes unexisting links
  117. *
  118. * @param array $existingLinks
  119. * @param array $newLinks
  120. * @return array
  121. */
  122. private function removeUnExistingLinks($existingLinks, $newLinks)
  123. {
  124. $result = [];
  125. foreach ($existingLinks as $key => $link) {
  126. $result[$key] = $link;
  127. if ($link->getLinkType() == self::TYPE_NAME) {
  128. $exists = false;
  129. foreach ($newLinks as $newLink) {
  130. if ($link->getLinkedProductSku() == $newLink->getLinkedProductSku()) {
  131. $exists = true;
  132. }
  133. }
  134. if (!$exists) {
  135. unset($result[$key]);
  136. }
  137. }
  138. }
  139. return $result;
  140. }
  141. }