@@ -25,14 +25,19 @@ class Content extends DataSource
25
25
*/
26
26
protected $ fixtureFactory ;
27
27
28
+ /**
29
+ * Repository factory.
30
+ *
31
+ * @var RepositoryFactory
32
+ */
33
+ protected $ repositoryFactory ;
34
+
28
35
/**
29
36
* @constructor
30
37
* @param RepositoryFactory $repositoryFactory
31
38
* @param FixtureFactory $fixtureFactory
32
39
* @param array $params
33
40
* @param array $data
34
- *
35
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
36
41
*/
37
42
public function __construct (
38
43
RepositoryFactory $ repositoryFactory ,
@@ -41,37 +46,62 @@ public function __construct(
41
46
array $ data = []
42
47
) {
43
48
$ this ->fixtureFactory = $ fixtureFactory ;
49
+ $ this ->repositoryFactory = $ repositoryFactory ;
44
50
$ this ->params = $ params ;
45
51
$ this ->data = $ data ;
46
- if (isset ($ data ['widget ' ]['dataset ' ]) && isset ($ this ->params ['repository ' ])) {
47
- $ this ->data ['widget ' ]['dataset ' ] = $ repositoryFactory ->get ($ this ->params ['repository ' ])->get (
48
- $ data ['widget ' ]['dataset ' ]
52
+ $ this ->prepareSourceData ();
53
+ }
54
+
55
+ /**
56
+ * Prepare source data.
57
+ *
58
+ * @return void
59
+ */
60
+ protected function prepareSourceData ()
61
+ {
62
+ if (isset ($ this ->data ['widget ' ]['dataset ' ]) && isset ($ this ->params ['repository ' ])) {
63
+ $ this ->data ['widget ' ]['dataset ' ] = $ this ->repositoryFactory ->get ($ this ->params ['repository ' ])->get (
64
+ $ this ->data ['widget ' ]['dataset ' ]
49
65
);
50
- foreach ($ this ->data ['widget ' ]['dataset ' ] as $ key => $ widget ) {
51
- if (isset ($ widget ['chosen_option ' ]['category_path ' ])
52
- && !isset ($ widget ['chosen_option ' ]['filter_sku ' ])
53
- ) {
54
- $ category = $ this ->createCategory ($ widget );
55
- $ categoryName = $ category ->getData ('name ' );
56
- $ this ->data ['widget ' ]['dataset ' ][$ key ]['chosen_option ' ]['category_path ' ] = $ categoryName ;
57
- }
58
- if (isset ($ widget ['chosen_option ' ]['category_path ' ]) && isset ($ widget ['chosen_option ' ]['filter_sku ' ])) {
59
- $ product = $ this ->createProduct ($ widget );
60
- $ categoryName = $ product ->getCategoryIds ()[0 ]['name ' ];
61
- $ productSku = $ product ->getData ('sku ' );
62
- $ this ->data ['widget ' ]['dataset ' ][$ key ]['chosen_option ' ]['category_path ' ] = $ categoryName ;
63
- $ this ->data ['widget ' ]['dataset ' ][$ key ]['chosen_option ' ]['filter_sku ' ] = $ productSku ;
64
- }
65
- if ($ widget ['widget_type ' ] == 'Catalog New Products List ' ) {
66
- $ this ->createProduct ();
67
- }
68
- if ($ widget ['widget_type ' ] == 'CMS Static Block ' ) {
69
- $ block = $ this ->createBlock ($ widget );
70
- $ blockIdentifier = $ block ->getIdentifier ();
71
- $ this ->data ['widget ' ]['dataset ' ][$ key ]['chosen_option ' ]['filter_identifier ' ] = $ blockIdentifier ;
72
- }
66
+ $ this ->data = array_merge ($ this ->data , $ this ->prepareWidgetData ($ this ->data ['widget ' ]));
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Prepare widget data for the source.
72
+ *
73
+ * @param array $widgets
74
+ * @return array
75
+ */
76
+ protected function prepareWidgetData (array $ widgets )
77
+ {
78
+ $ data = [];
79
+ foreach ($ widgets ['dataset ' ] as $ key => $ widget ) {
80
+ if (isset ($ widget ['chosen_option ' ]['category_path ' ])
81
+ && !isset ($ widget ['chosen_option ' ]['filter_sku ' ])
82
+ ) {
83
+ $ category = $ this ->createCategory ($ widget );
84
+ $ categoryName = $ category ->getData ('name ' );
85
+ $ data ['widget ' ]['dataset ' ][$ key ]['chosen_option ' ]['category_path ' ] = $ categoryName ;
86
+ }
87
+ if (isset ($ widget ['chosen_option ' ]['category_path ' ]) && isset ($ widget ['chosen_option ' ]['filter_sku ' ])) {
88
+ $ product = $ this ->createProduct ($ widget );
89
+ $ categoryName = $ product ->getCategoryIds ()[0 ]['name ' ];
90
+ $ productSku = $ product ->getData ('sku ' );
91
+ $ data ['widget ' ]['dataset ' ][$ key ]['chosen_option ' ]['category_path ' ] = $ categoryName ;
92
+ $ data ['widget ' ]['dataset ' ][$ key ]['chosen_option ' ]['filter_sku ' ] = $ productSku ;
93
+ }
94
+ if ($ widget ['widget_type ' ] == 'Catalog New Products List ' ) {
95
+ $ this ->createProduct ();
96
+ }
97
+ if ($ widget ['widget_type ' ] == 'CMS Static Block ' ) {
98
+ $ block = $ this ->createBlock ($ widget );
99
+ $ blockIdentifier = $ block ->getIdentifier ();
100
+ $ data ['widget ' ]['dataset ' ][$ key ]['chosen_option ' ]['filter_identifier ' ] = $ blockIdentifier ;
73
101
}
74
102
}
103
+
104
+ return $ data ;
75
105
}
76
106
77
107
/**
0 commit comments