Total Complexity | 57 |
Total Lines | 306 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ProductBulkLoader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductBulkLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class ProductBulkLoader extends CsvBulkLoader |
||
28 | { |
||
29 | /** |
||
30 | * You can force every product to be in a certain category, as long as you know its ID. |
||
31 | * |
||
32 | * @config |
||
33 | * @var null |
||
34 | */ |
||
35 | private static $parent_page_id = null; |
||
36 | |||
37 | /** |
||
38 | * Set this if you want categories to be created if they don't exist. |
||
39 | * |
||
40 | * @config |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected static $create_new_product_groups = false; |
||
44 | |||
45 | protected $foundParentId = null; |
||
46 | |||
47 | |||
48 | // NB do NOT use functional indirection on any fields where they |
||
49 | // will be used in $duplicateChecks as well - they simply don't work. |
||
50 | public $columnMap = [ |
||
51 | 'Price' => 'BasePrice', |
||
52 | |||
53 | 'Category' => '->setParent', |
||
54 | 'ProductGroup' => '->setParent', |
||
55 | 'ProductCategory' => '->setParent', |
||
56 | |||
57 | 'Product ID' => 'InternalItemID', |
||
58 | 'ProductID' => 'InternalItemID', |
||
59 | 'SKU' => 'InternalItemID', |
||
60 | |||
61 | 'Description' => '->setContent', |
||
62 | 'Long Description' => '->setContent', |
||
63 | 'Short Description' => 'MetaDescription', |
||
64 | |||
65 | 'Short Title' => 'MenuTitle', |
||
66 | |||
67 | 'Title' => 'Title', |
||
68 | 'Page name' => 'Title', |
||
69 | 'Page Name' => 'Title', |
||
70 | |||
71 | 'Variation' => '->processVariation', |
||
72 | 'Variation1' => '->processVariation1', |
||
73 | 'Variation2' => '->processVariation2', |
||
74 | 'Variation3' => '->processVariation3', |
||
75 | 'Variation4' => '->processVariation4', |
||
76 | 'Variation5' => '->processVariation5', |
||
77 | 'Variation6' => '->processVariation6', |
||
78 | |||
79 | 'VariationID' => '->variationRow', |
||
80 | 'Variation ID' => '->variationRow', |
||
81 | 'SubID' => '->variationRow', |
||
82 | 'Sub ID' => '->variationRow', |
||
83 | ]; |
||
84 | |||
85 | public $duplicateChecks = [ |
||
86 | 'InternalItemID' => 'InternalItemID', |
||
87 | 'SKU' => 'InternalItemID', |
||
88 | 'Product ID' => 'InternalItemID', |
||
89 | 'ProductID' => 'InternalItemID', |
||
90 | 'Title' => 'Title', |
||
91 | 'Page Title' => 'Title', |
||
92 | 'PageTitle' => 'Title', |
||
93 | ]; |
||
94 | |||
95 | public $relationCallbacks = [ |
||
96 | 'Image' => [ |
||
97 | 'relationname' => 'Image', // relation accessor name |
||
98 | 'callback' => 'imageByFilename', |
||
99 | ], |
||
100 | 'Photo' => [ |
||
101 | 'relationname' => 'Image', // relation accessor name |
||
102 | 'callback' => 'imageByFilename', |
||
103 | ], |
||
104 | ]; |
||
105 | |||
106 | protected function processAll($filepath, $preview = false) |
||
107 | { |
||
108 | $this->extend('updateColumnMap', $this->columnMap); |
||
109 | |||
110 | $results = parent::processAll($filepath, $preview); |
||
111 | //After results have been processed, publish all created & updated products |
||
112 | $objects = ArrayList::create(); |
||
113 | $objects->merge($results->Created()); |
||
114 | $objects->merge($results->Updated()); |
||
115 | $parentPageID = $this->config()->parent_page_id; |
||
116 | foreach ($objects as $object) { |
||
117 | if (!$object->ParentID) { |
||
118 | //set parent page |
||
119 | if (is_numeric($parentPageID) && ProductCategory::get()->byID($parentPageID)) { //cached option |
||
120 | $object->ParentID = $parentPageID; |
||
121 | } elseif ($parentPage = ProductCategory::get()->filter('Title', 'Products')->sort('Created', 'DESC')->first()) { //page called 'Products' |
||
122 | $object->ParentID = $parentPageID = $parentPage->ID; |
||
123 | } elseif ($parentpage = ProductCategory::get()->filter('ParentID', 0)->sort('Created', 'DESC')->first()) { //root page |
||
124 | $object->ParentID = $parentPageID = $parentpage->ID; |
||
125 | } elseif ($parentpage = ProductCategory::get()->sort('Created', 'DESC')->first()) { //any product page |
||
126 | $object->ParentID = $parentPageID = $parentpage->ID; |
||
127 | } else { |
||
128 | $object->ParentID = $parentPageID = 0; |
||
129 | } |
||
130 | } |
||
131 | $this->foundParentId = $parentPageID; |
||
132 | $object->extend('updateImport'); //could be used for setting other attributes, such as stock level |
||
133 | $object->writeToStage('Stage'); |
||
134 | $object->publishSingle(); |
||
135 | } |
||
136 | return $results; |
||
137 | } |
||
138 | |||
139 | public function processRecord($record, $columnMap, &$results, $preview = false) |
||
140 | { |
||
141 | if (!$record || !isset($record['Title']) || $record['Title'] == '') { //TODO: make required fields customisable |
||
142 | return null; |
||
143 | } |
||
144 | return parent::processRecord($record, $columnMap, $results, $preview); |
||
145 | } |
||
146 | |||
147 | // set image, based on filename |
||
148 | public function imageByFilename(&$obj, $val) |
||
149 | { |
||
150 | $filename = trim(strtolower(Convert::raw2sql($val))); |
||
|
|||
151 | $filenamedashes = str_replace(' ', '-', $filename); |
||
152 | if ($filename |
||
153 | && $image = Image::get()->whereAny( |
||
154 | [ |
||
155 | "LOWER(\"FileFilename\") LIKE '%$filename%'", |
||
156 | "LOWER(\"FileFilename\") LIKE '%$filenamedashes%'" |
||
157 | ] |
||
158 | )->first() |
||
159 | ) { //ignore case |
||
160 | if ($image instanceof Image && $image->exists()) { |
||
161 | return $image; |
||
162 | } |
||
163 | } |
||
164 | return null; |
||
165 | } |
||
166 | |||
167 | // find product group parent (ie Cateogry) |
||
168 | public function setParent(&$obj, $val) |
||
169 | { |
||
170 | $title = strtolower(Convert::raw2sql($val)); |
||
171 | if ($title) { |
||
172 | // find or create parent category, if provided |
||
173 | if ($parentPage = ProductCategory::get()->where(['LOWER("Title") = ?' => $title])->sort('Created', 'DESC')->first()) { |
||
174 | $obj->ParentID = $parentPage->ID; |
||
175 | $obj->write(); |
||
176 | $obj->writeToStage('Stage'); |
||
177 | $obj->publishSingle(); |
||
178 | //TODO: otherwise assign it to the first product group found |
||
179 | } elseif ($this->config()->create_new_product_groups) { |
||
180 | //create parent product group |
||
181 | $pg = ProductCategory::create(); |
||
182 | $pg->setTitle($title); |
||
183 | $pg->ParentID = ($this->foundParentId) ? $this->foundParentId : 0; |
||
184 | $pg->writeToStage('Stage'); |
||
185 | $pg->publishSingle(); |
||
186 | $obj->ParentID = $pg->ID; |
||
187 | $obj->write(); |
||
188 | $obj->writeToStage('Stage'); |
||
189 | $obj->publishSingle(); |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Adds paragraphs to content. |
||
196 | */ |
||
197 | public function setContent(&$obj, $val, $record) |
||
198 | { |
||
199 | $val = trim($val); |
||
200 | if ($val) { |
||
201 | $paragraphs = explode("\n", $val); |
||
202 | $obj->Content = '<p>' . implode('</p><p>', $paragraphs) . '</p>'; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | public function processVariation(&$obj, $val, $record) |
||
207 | { |
||
208 | if (isset($record['->variationRow'])) { |
||
209 | return; |
||
210 | } //don't use this technique for variation rows |
||
211 | $parts = explode(':', $val); |
||
212 | if (count($parts) == 2) { |
||
213 | $attributetype = trim($parts[0]); |
||
214 | $attributevalues = explode(',', $parts[1]); |
||
215 | //get rid of empty values |
||
216 | foreach ($attributevalues as $key => $value) { |
||
217 | if (!$value || trim($value) == '') { |
||
218 | unset($attributevalues[$key]); |
||
219 | } |
||
220 | } |
||
221 | if (count($attributevalues) >= 1) { |
||
222 | $attributetype = AttributeType::find_or_make($attributetype); |
||
223 | foreach ($attributevalues as $key => $value) { |
||
224 | $val = trim($value); |
||
225 | if ($val != '' && $val != null) { |
||
226 | $attributevalues[$key] = $val; //remove outside spaces from values |
||
227 | } |
||
228 | } |
||
229 | $attributetype->addValues($attributevalues); |
||
230 | $obj->VariationAttributeTypes()->add($attributetype); |
||
231 | //only generate variations if none exist yet |
||
232 | if (!$obj->Variations()->exists() || $obj->WeAreBuildingVariations) { |
||
233 | //either start new variations, or multiply existing ones by new variations |
||
234 | $obj->generateVariationsFromAttributes($attributetype, $attributevalues); |
||
235 | $obj->WeAreBuildingVariations = true; |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | |||
241 | //work around until I can figure out how to allow calling processVariation multiple times |
||
242 | public function processVariation1(&$obj, $val, $record) |
||
243 | { |
||
244 | $this->processVariation($obj, $val, $record); |
||
245 | } |
||
246 | |||
247 | public function processVariation2(&$obj, $val, $record) |
||
248 | { |
||
249 | $this->processVariation($obj, $val, $record); |
||
250 | } |
||
251 | |||
252 | public function processVariation3(&$obj, $val, $record) |
||
255 | } |
||
256 | |||
257 | public function processVariation4(&$obj, $val, $record) |
||
260 | } |
||
261 | |||
262 | public function processVariation5(&$obj, $val, $record) |
||
265 | } |
||
266 | |||
267 | public function processVariation6(&$obj, $val, $record) |
||
270 | } |
||
271 | |||
272 | public function variationRow(&$obj, $val, $record) |
||
333 | } |
||
334 | } |
||
335 |