This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Repository.php
168 lines (149 loc) · 5.39 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ExternalLinks\Model\Plugin\Product;
use Magento\Catalog\Api\Data\ProductExtensionFactory;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\EntityManager\EntityManager;
use Magento\ExternalLinks\Api\Data\ExternalLinkInterface;
use Magento\ExternalLinks\Api\ExternalLinksProviderInterface;
/**
* Class Repository
* @package Magento\ExternalLinks\Model\Plugin\Product
*/
class Repository
{
/** @var ProductExtensionFactory */
private $productExtensionFactory;
/** @var ProductInterface */
private $currentProduct;
/** @var EntityManager */
private $entityManager;
/** @var ExternalLinksProviderInterface */
private $externalLinksProvider;
/**
* Repository constructor.
* @param ProductExtensionFactory $productExtensionFactory
* @param EntityManager $entityManager
*/
public function __construct(
ProductExtensionFactory $productExtensionFactory,
EntityManager $entityManager,
ExternalLinksProviderInterface $externalLinksProvider
)
{
$this->productExtensionFactory = $productExtensionFactory;
$this->entityManager = $entityManager;
$this->externalLinksProvider = $externalLinksProvider;
}
/**
* Add Social Links to product extension attributes
*
* @param \Magento\Catalog\Api\ProductRepositoryInterface $subject
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return \Magento\Framework\Api\SearchResults
*/
public function afterGetList
(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Framework\Api\SearchResults $searchResult
) {
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
foreach ($searchResult->getItems() as $product) {
$this->addExternalLinksToProduct($product);
}
return $searchResult;
}
/**
* @param \Magento\Catalog\Api\ProductRepositoryInterface $subject
* @param ProductInterface $product
* @return void
*/
public function beforeSave
(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $product
) {
$this->currentProduct = $product;
}
/**
* @param \Magento\Catalog\Api\ProductRepositoryInterface $subject
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @return \Magento\Catalog\Api\Data\ProductInterface
*/
public function afterGet
(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $product
) {
$this->addExternalLinksToProduct($product);
return $product;
}
/**
* Compare old and new links. And if old links has the same as new one -> delete them
*
* @param array $newLinks
* @param array $oldLinks
* @throws \Exception
*/
private function cleanOldLinks(array $newLinks, array $oldLinks)
{
/** @var ExternalLinkInterface $link */
foreach($newLinks as $link) {
/** @var ExternalLinkInterface $oldLink */
foreach($oldLinks as $oldLink) {
if ($oldLink->getLinkType() === $link->getLinkType()) {
$this->entityManager->delete($oldLink);
}
}
}
}
/**
* @param \Magento\Catalog\Api\ProductRepositoryInterface $subject
* @param ProductInterface $product
* @throws \Exception
* @return self
*/
public function afterSave
(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $product
) {
if ($this->currentProduct !== null) {
/** @var ProductInterface $previosProduct */
$extensionAttributes = $this->currentProduct->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getExternalLinks()) {
/** @var ExternalLinkInterface $externalLink */
$externalLinks = $extensionAttributes->getExternalLinks();
$oldExternalLinks = $product->getExtensionAttributes()->getExternalLinks();
if (is_array($externalLinks)) {
$this->cleanOldLinks($externalLinks, $oldExternalLinks);
/** @var ExternalLinkInterface $link */
foreach($externalLinks as $link) {
$link->setProductId($product->getId());
$this->entityManager->save($link);
}
}
}
$this->currentProduct = null;
}
return $product;
}
/**
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @return self
*/
private function addExternalLinksToProduct(\Magento\Catalog\Api\Data\ProductInterface $product)
{
$extensionAttributes = $product->getExtensionAttributes();
if (empty($extensionAttributes)) {
$extensionAttributes = $this->productExtensionFactory->create();
}
$externalLinks = $this->externalLinksProvider->getLinks($product->getId());
$extensionAttributes->setExternalLinks($externalLinks);
$product->setExtensionAttributes($extensionAttributes);
return $this;
}
}