6
6
7
7
namespace Magento \Catalog \Model \ProductLink ;
8
8
9
- use Magento \Catalog \Api \Data ;
10
9
use Magento \Framework \Exception \CouldNotSaveException ;
11
10
use Magento \Framework \Exception \NoSuchEntityException ;
12
11
use Magento \Framework \Exception \InputException ;
12
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
13
+ use Magento \Catalog \Model \Product \LinkTypeProvider ;
14
+ use Magento \Catalog \Api \ProductLinkManagementInterface ;
13
15
14
- class Management implements \Magento \Catalog \Api \ProductLinkManagementInterface
16
+ /**
17
+ * Manage product links from api
18
+ */
19
+ class Management implements ProductLinkManagementInterface
15
20
{
16
21
/**
17
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
22
+ * @var ProductRepositoryInterface
18
23
*/
19
24
protected $ productRepository ;
20
25
21
26
/**
22
- * @var \Magento\Catalog\Model\Product\ LinkTypeProvider
27
+ * @var LinkTypeProvider
23
28
*/
24
29
protected $ linkTypeProvider ;
25
30
26
31
/**
27
- * @param \Magento\Catalog\Api\ ProductRepositoryInterface $productRepository
28
- * @param \Magento\Catalog\Model\Product\ LinkTypeProvider $linkTypeProvider
32
+ * @param ProductRepositoryInterface $productRepository
33
+ * @param LinkTypeProvider $linkTypeProvider
29
34
*/
30
35
public function __construct (
31
- \ Magento \ Catalog \ Api \ ProductRepositoryInterface $ productRepository ,
32
- \ Magento \ Catalog \ Model \ Product \ LinkTypeProvider $ linkTypeProvider
36
+ ProductRepositoryInterface $ productRepository ,
37
+ LinkTypeProvider $ linkTypeProvider
33
38
) {
34
39
$ this ->productRepository = $ productRepository ;
35
40
$ this ->linkTypeProvider = $ linkTypeProvider ;
36
41
}
37
42
38
43
/**
39
- * { @inheritdoc}
44
+ * @inheritdoc
40
45
*/
41
46
public function getLinkedItemsByType ($ sku , $ type )
42
47
{
@@ -63,47 +68,42 @@ public function getLinkedItemsByType($sku, $type)
63
68
}
64
69
65
70
/**
66
- * { @inheritdoc}
71
+ * @inheritdoc
67
72
*/
68
73
public function setProductLinks ($ sku , array $ items )
69
74
{
75
+
76
+ if (empty ($ items )) {
77
+ throw InputException::invalidFieldValue ('items ' , 'empty array ' );
78
+ }
79
+
70
80
$ linkTypes = $ this ->linkTypeProvider ->getLinkTypes ();
71
81
72
82
// Check if product link type is set and correct
73
- if (!empty ($ items )) {
74
- foreach ($ items as $ newLink ) {
75
- $ type = $ newLink ->getLinkType ();
76
- if ($ type == null ) {
77
- throw InputException::requiredField ("linkType " );
78
- }
79
- if (!isset ($ linkTypes [$ type ])) {
80
- throw new NoSuchEntityException (
81
- __ ('The "%1" link type wasn \'t found. Verify the type and try again. ' , $ type )
82
- );
83
- }
83
+ foreach ($ items as $ newLink ) {
84
+ $ type = $ newLink ->getLinkType ();
85
+ if ($ type == null ) {
86
+ throw InputException::requiredField ("linkType " );
87
+ }
88
+ if (!isset ($ linkTypes [$ type ])) {
89
+ throw new NoSuchEntityException (
90
+ __ ('The "%1" link type wasn \'t found. Verify the type and try again. ' , $ type )
91
+ );
84
92
}
85
93
}
86
94
87
95
$ product = $ this ->productRepository ->get ($ sku );
88
96
89
- // Replace only links of the specified type
90
97
$ existingLinks = $ product ->getProductLinks ();
91
- $ newLinks = [];
92
- if (!empty ($ existingLinks )) {
93
- foreach ($ existingLinks as $ link ) {
94
- if ($ link ->getLinkType () != $ type ) {
95
- $ newLinks [] = $ link ;
96
- }
97
- }
98
- $ newLinks = array_merge ($ newLinks , $ items );
99
- } else {
100
- $ newLinks = $ items ;
101
- }
98
+ $ newLinks = array_merge ($ existingLinks , $ items );
99
+
102
100
$ product ->setProductLinks ($ newLinks );
103
101
try {
104
102
$ this ->productRepository ->save ($ product );
105
103
} catch (\Exception $ exception ) {
106
- throw new CouldNotSaveException (__ ('The linked products data is invalid. Verify the data and try again. ' ));
104
+ throw new CouldNotSaveException (
105
+ __ ('The linked products data is invalid. Verify the data and try again. ' )
106
+ );
107
107
}
108
108
109
109
return true ;
0 commit comments