11
11
use Magento \Catalog \Api \CategoryRepositoryInterface ;
12
12
use Magento \Catalog \Api \ProductRepositoryInterface ;
13
13
use Magento \Framework \Webapi \Rest \Request as RestRequest ;
14
+ use Magento \Webapi \Controller \Rest \Router \Route ;
14
15
15
16
/**
16
17
* Plugin for InputParamsResolver
@@ -44,64 +45,37 @@ public function __construct(RestRequest $request)
44
45
*/
45
46
public function afterResolve (\Magento \Webapi \Controller \Rest \InputParamsResolver $ subject , array $ result ): array
46
47
{
47
- $ route = $ subject ->getRoute ();
48
- $ serviceMethodName = $ route ->getServiceMethod ();
49
- $ serviceClassName = $ route ->getServiceClass ();
50
- $ requestBodyParams = $ this ->request ->getBodyParams ();
48
+ $ this ->processProductCall ($ subject , $ result );
49
+ $ this ->processCategoryCall ($ subject , $ result );
51
50
52
- if ($ this ->isProductSaveCalled ($ serviceClassName , $ serviceMethodName )
53
- && $ this ->isCustomAttributesExists ($ requestBodyParams , 'product ' )) {
54
- foreach ($ requestBodyParams ['product ' ]['custom_attributes ' ] as $ attribute ) {
55
- if ($ attribute ['attribute_code ' ] === self ::SAVE_REWRITES_HISTORY ) {
56
- foreach ($ result as $ resultItem ) {
57
- if ($ resultItem instanceof \Magento \Catalog \Model \Product) {
58
- $ resultItem ->setData (self ::SAVE_REWRITES_HISTORY , (bool )$ attribute ['value ' ]);
59
- break 2 ;
60
- }
61
- }
62
- break ;
63
- }
64
- }
65
- }
66
-
67
- if ($ this ->isCategorySaveCalled ($ serviceClassName , $ serviceMethodName )
68
- && $ this ->isCustomAttributesExists ($ requestBodyParams , 'category ' )) {
69
- foreach ($ requestBodyParams ['category ' ]['custom_attributes ' ] as $ attribute ) {
70
- if ($ attribute ['attribute_code ' ] === self ::SAVE_REWRITES_HISTORY ) {
71
- foreach ($ result as $ resultItem ) {
72
- if ($ resultItem instanceof \Magento \Catalog \Model \Category) {
73
- $ resultItem ->setData (self ::SAVE_REWRITES_HISTORY , (bool )$ attribute ['value ' ]);
74
- break 2 ;
75
- }
76
- }
77
- break ;
78
- }
79
- }
80
- }
81
51
return $ result ;
82
52
}
83
53
84
54
/**
85
55
* Check that product save method called
86
56
*
87
- * @param string $serviceClassName
88
- * @param string $serviceMethodName
57
+ * @param Route $route
89
58
* @return bool
90
59
*/
91
- private function isProductSaveCalled (string $ serviceClassName , string $ serviceMethodName ): bool
60
+ private function isProductSaveCalled (Route $ route ): bool
92
61
{
62
+ $ serviceMethodName = $ route ->getServiceMethod ();
63
+ $ serviceClassName = $ route ->getServiceClass ();
64
+
93
65
return $ serviceClassName === ProductRepositoryInterface::class && $ serviceMethodName === 'save ' ;
94
66
}
95
67
96
68
/**
97
69
* Check that category save method called
98
70
*
99
- * @param string $serviceClassName
100
- * @param string $serviceMethodName
71
+ * @param Route $route
101
72
* @return bool
102
73
*/
103
- private function isCategorySaveCalled (string $ serviceClassName , string $ serviceMethodName ): bool
74
+ private function isCategorySaveCalled (Route $ route ): bool
104
75
{
76
+ $ serviceMethodName = $ route ->getServiceMethod ();
77
+ $ serviceClassName = $ route ->getServiceClass ();
78
+
105
79
return $ serviceClassName === CategoryRepositoryInterface::class && $ serviceMethodName === 'save ' ;
106
80
}
107
81
@@ -116,4 +90,53 @@ private function isCustomAttributesExists(array $requestBodyParams, string $enti
116
90
{
117
91
return !empty ($ requestBodyParams [$ entityCode ]['custom_attributes ' ]);
118
92
}
93
+
94
+ /**
95
+ * @param \Magento\Webapi\Controller\Rest\InputParamsResolver $subject
96
+ * @param array $result
97
+ * @return array
98
+ */
99
+ private function processProductCall (\Magento \Webapi \Controller \Rest \InputParamsResolver $ subject , array $ result ): void
100
+ {
101
+ $ requestBodyParams = $ this ->request ->getBodyParams ();
102
+
103
+ if ($ this ->isProductSaveCalled ($ subject ->getRoute ())
104
+ && $ this ->isCustomAttributesExists ($ requestBodyParams , 'product ' )) {
105
+ foreach ($ requestBodyParams ['product ' ]['custom_attributes ' ] as $ attribute ) {
106
+ if ($ attribute ['attribute_code ' ] === self ::SAVE_REWRITES_HISTORY ) {
107
+ foreach ($ result as $ resultItem ) {
108
+ if ($ resultItem instanceof \Magento \Catalog \Model \Product) {
109
+ $ resultItem ->setData (self ::SAVE_REWRITES_HISTORY , (bool )$ attribute ['value ' ]);
110
+ break 2 ;
111
+ }
112
+ }
113
+ break ;
114
+ }
115
+ }
116
+ }
117
+ }
118
+
119
+ /**
120
+ * @param \Magento\Webapi\Controller\Rest\InputParamsResolver $subject
121
+ * @param array $result
122
+ */
123
+ private function processCategoryCall (\Magento \Webapi \Controller \Rest \InputParamsResolver $ subject , array $ result ): void
124
+ {
125
+ $ requestBodyParams = $ this ->request ->getBodyParams ();
126
+
127
+ if ($ this ->isCategorySaveCalled ($ subject ->getRoute ())
128
+ && $ this ->isCustomAttributesExists ($ requestBodyParams , 'category ' )) {
129
+ foreach ($ requestBodyParams ['category ' ]['custom_attributes ' ] as $ attribute ) {
130
+ if ($ attribute ['attribute_code ' ] === self ::SAVE_REWRITES_HISTORY ) {
131
+ foreach ($ result as $ resultItem ) {
132
+ if ($ resultItem instanceof \Magento \Catalog \Model \Category) {
133
+ $ resultItem ->setData (self ::SAVE_REWRITES_HISTORY , (bool )$ attribute ['value ' ]);
134
+ break 2 ;
135
+ }
136
+ }
137
+ break ;
138
+ }
139
+ }
140
+ }
141
+ }
119
142
}
0 commit comments