Total Complexity | 61 |
Total Lines | 543 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like Recipe 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 Recipe, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Recipe extends Model |
||
26 | { |
||
27 | // Public Properties |
||
28 | // ========================================================================= |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | public $name; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | public $description; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | public $skill = 'intermediate'; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | public $serves = 1; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | public $ingredients = []; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | public $directions = []; |
||
59 | |||
60 | /** |
||
61 | * @var int |
||
62 | */ |
||
63 | public $imageId = 0; |
||
64 | |||
65 | /** |
||
66 | * @var int |
||
67 | */ |
||
68 | public $prepTime; |
||
69 | |||
70 | /** |
||
71 | * @var int |
||
72 | */ |
||
73 | public $cookTime; |
||
74 | |||
75 | /** |
||
76 | * @var int |
||
77 | */ |
||
78 | public $totalTime; |
||
79 | |||
80 | /** |
||
81 | * @var array |
||
82 | */ |
||
83 | public $ratings = []; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | public $servingSize; |
||
89 | |||
90 | /** |
||
91 | * @var int |
||
92 | */ |
||
93 | public $calories; |
||
94 | |||
95 | /** |
||
96 | * @var int |
||
97 | */ |
||
98 | public $carbohydrateContent; |
||
99 | |||
100 | /** |
||
101 | * @var int |
||
102 | */ |
||
103 | public $cholesterolContent; |
||
104 | |||
105 | /** |
||
106 | * @var int |
||
107 | */ |
||
108 | public $fatContent; |
||
109 | |||
110 | /** |
||
111 | * @var int |
||
112 | */ |
||
113 | public $fiberContent; |
||
114 | |||
115 | /** |
||
116 | * @var int |
||
117 | */ |
||
118 | public $proteinContent; |
||
119 | |||
120 | /** |
||
121 | * @var int |
||
122 | */ |
||
123 | public $saturatedFatContent; |
||
124 | |||
125 | /** |
||
126 | * @var int |
||
127 | */ |
||
128 | public $sodiumContent; |
||
129 | |||
130 | /** |
||
131 | * @var int |
||
132 | */ |
||
133 | public $sugarContent; |
||
134 | |||
135 | /** |
||
136 | * @var int |
||
137 | */ |
||
138 | public $transFatContent; |
||
139 | |||
140 | /** |
||
141 | * @var int |
||
142 | */ |
||
143 | public $unsaturatedFatContent; |
||
144 | |||
145 | // Public Methods |
||
146 | // ========================================================================= |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | public function rules() |
||
152 | { |
||
153 | return [ |
||
154 | ['name', 'string'], |
||
155 | ['name', 'default', 'value' => ''], |
||
156 | ['description', 'string'], |
||
157 | ['skill', 'string'], |
||
158 | ['serves', 'integer'], |
||
159 | ['imageId', 'integer'], |
||
160 | ['prepTime', 'integer'], |
||
161 | ['cookTime', 'integer'], |
||
162 | ['totalTime', 'integer'], |
||
163 | ['servingSize', 'string'], |
||
164 | ['calories', 'integer'], |
||
165 | ['carbohydrateContent', 'integer'], |
||
166 | ['cholesterolContent', 'integer'], |
||
167 | ['fatContent', 'integer'], |
||
168 | ['fiberContent', 'integer'], |
||
169 | ['proteinContent', 'integer'], |
||
170 | ['saturatedFatContent', 'integer'], |
||
171 | ['sodiumContent', 'integer'], |
||
172 | ['sugarContent', 'integer'], |
||
173 | ['transFatContent', 'integer'], |
||
174 | ['unsaturatedFatContent', 'integer'], |
||
175 | ]; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Render the JSON-LD Structured Data for this recipe |
||
180 | * |
||
181 | * @param bool $raw |
||
182 | * |
||
183 | * @return string|\Twig_Markup |
||
184 | */ |
||
185 | public function renderRecipeJSONLD($raw = true) |
||
186 | { |
||
187 | $recipeJSONLD = [ |
||
188 | "context" => "http://schema.org", |
||
189 | "type" => "Recipe", |
||
190 | "name" => $this->name, |
||
191 | "image" => $this->getImageUrl(), |
||
192 | "description" => $this->description, |
||
193 | "recipeYield" => $this->serves, |
||
194 | "recipeIngredient" => $this->getIngredients("imperial", 0, false), |
||
195 | "recipeInstructions" => $this->getDirections(false), |
||
196 | ]; |
||
197 | $recipeJSONLD = array_filter($recipeJSONLD); |
||
198 | |||
199 | $nutrition = [ |
||
200 | "type" => "NutritionInformation", |
||
201 | 'servingSize' => $this->servingSize, |
||
202 | 'calories' => $this->calories, |
||
203 | 'carbohydrateContent' => $this->carbohydrateContent, |
||
204 | 'cholesterolContent' => $this->cholesterolContent, |
||
205 | 'fatContent' => $this->fatContent, |
||
206 | 'fiberContent' => $this->fiberContent, |
||
207 | 'proteinContent' => $this->proteinContent, |
||
208 | 'saturatedFatContent' => $this->saturatedFatContent, |
||
209 | 'sodiumContent' => $this->sodiumContent, |
||
210 | 'sugarContent' => $this->sugarContent, |
||
211 | 'transFatContent' => $this->transFatContent, |
||
212 | 'unsaturatedFatContent' => $this->unsaturatedFatContent, |
||
213 | ]; |
||
214 | $nutrition = array_filter($nutrition); |
||
215 | $recipeJSONLD['nutrition'] = $nutrition; |
||
216 | if (count($recipeJSONLD['nutrition']) == 1) { |
||
217 | unset($recipeJSONLD['nutrition']); |
||
218 | } |
||
219 | $aggregateRating = $this->getAggregateRating(); |
||
220 | if ($aggregateRating) { |
||
221 | $aggregateRatings = [ |
||
222 | "type" => "AggregateRating", |
||
223 | 'ratingCount' => $this->getRatingsCount(), |
||
224 | 'bestRating' => '5', |
||
225 | 'worstRating' => '1', |
||
226 | 'ratingValue' => $aggregateRating, |
||
227 | ]; |
||
228 | $aggregateRatings = array_filter($aggregateRatings); |
||
229 | $recipeJSONLD['aggregateRating'] = $aggregateRatings; |
||
230 | |||
231 | $reviews = []; |
||
232 | foreach ($this->ratings as $rating) { |
||
233 | $review = [ |
||
234 | "type" => "Review", |
||
235 | 'author' => $rating['author'], |
||
236 | 'name' => $this->name." ".Craft::t("recipe", "Review"), |
||
237 | 'description' => $rating['review'], |
||
238 | 'reviewRating' => [ |
||
239 | "type" => "Rating", |
||
240 | 'bestRating' => '5', |
||
241 | 'worstRating' => '1', |
||
242 | 'ratingValue' => $rating['rating'], |
||
243 | ], |
||
244 | ]; |
||
245 | array_push($reviews, $review); |
||
246 | } |
||
247 | $reviews = array_filter($reviews); |
||
248 | $recipeJSONLD['review'] = $reviews; |
||
249 | } |
||
250 | |||
251 | if ($this->prepTime) { |
||
252 | $recipeJSONLD['prepTime'] = "PT".$this->prepTime."M"; |
||
253 | } |
||
254 | if ($this->cookTime) { |
||
255 | $recipeJSONLD['cookTime'] = "PT".$this->cookTime."M"; |
||
256 | } |
||
257 | if ($this->totalTime) { |
||
258 | $recipeJSONLD['totalTime'] = "PT".$this->totalTime."M"; |
||
259 | } |
||
260 | |||
261 | return $this->renderJsonLd($recipeJSONLD, $raw); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Get the URL to the recipe's image |
||
266 | * |
||
267 | * @return null|string |
||
268 | */ |
||
269 | public function getImageUrl() |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Get all of the ingredients for this recipe |
||
284 | * |
||
285 | * @param string $outputUnits |
||
286 | * @param int $serving |
||
287 | * @param bool $raw |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getIngredients($outputUnits = "imperial", $serving = 0, $raw = true) |
||
292 | { |
||
293 | $result = []; |
||
294 | |||
295 | if ($this->ingredients != '') { |
||
296 | foreach ($this->ingredients as $row) { |
||
297 | $convertedUnits = ""; |
||
298 | $ingredient = ""; |
||
299 | if ($row['quantity']) { |
||
300 | // Multiply the quantity by how many servings we want |
||
301 | $multiplier = 1; |
||
302 | if ($serving > 0) { |
||
303 | $multiplier = $serving / $this->serves; |
||
304 | } |
||
305 | $quantity = $row['quantity'] * $multiplier; |
||
306 | $originalQuantity = $quantity; |
||
307 | |||
308 | // Do the units conversion |
||
309 | |||
310 | if ($outputUnits == 'imperial') { |
||
311 | if ($row['units'] == "mls") { |
||
312 | $convertedUnits = "tsps"; |
||
313 | $quantity = $quantity * 0.2; |
||
314 | } |
||
315 | |||
316 | if ($row['units'] == "ls") { |
||
317 | $convertedUnits = "cups"; |
||
318 | $quantity = $quantity * 4.2; |
||
319 | } |
||
320 | |||
321 | if ($row['units'] == "mgs") { |
||
322 | $convertedUnits = "ozs"; |
||
323 | $quantity = $quantity * 0.000035274; |
||
324 | } |
||
325 | |||
326 | if ($row['units'] == "gs") { |
||
327 | $convertedUnits = "ozs"; |
||
328 | $quantity = $quantity * 0.035274; |
||
329 | } |
||
330 | |||
331 | if ($row['units'] == "kg") { |
||
332 | $convertedUnits = "lbs"; |
||
333 | $quantity = $quantity * 2.2046226218; |
||
334 | } |
||
335 | } |
||
336 | |||
337 | if ($outputUnits == 'metric') { |
||
338 | if ($row['units'] == "tsps") { |
||
339 | $convertedUnits = "mls"; |
||
340 | $quantity = $quantity * 4.929; |
||
341 | } |
||
342 | |||
343 | if ($row['units'] == "tbsps") { |
||
344 | $convertedUnits = "mls"; |
||
345 | $quantity = $quantity * 14.787; |
||
346 | } |
||
347 | |||
348 | if ($row['units'] == "flozs") { |
||
349 | $convertedUnits = "mls"; |
||
350 | $quantity = $quantity * 29.574; |
||
351 | } |
||
352 | |||
353 | if ($row['units'] == "cups") { |
||
354 | $convertedUnits = "ls"; |
||
355 | $quantity = $quantity * 0.236588; |
||
356 | } |
||
357 | |||
358 | if ($row['units'] == "ozs") { |
||
359 | $convertedUnits = "gs"; |
||
360 | $quantity = $quantity * 28.3495; |
||
361 | } |
||
362 | |||
363 | if ($row['units'] == "lbs") { |
||
364 | $convertedUnits = "kg"; |
||
365 | $quantity = $quantity * 0.45359237; |
||
366 | } |
||
367 | |||
368 | $quantity = round($quantity, 1); |
||
369 | } |
||
370 | |||
371 | // Convert imperial units to nice fractions |
||
372 | |||
373 | if ($outputUnits == 'imperial') { |
||
374 | $quantity = $this->convertToFractions($quantity); |
||
375 | } |
||
376 | $ingredient .= $quantity; |
||
377 | |||
378 | if ($row['units']) { |
||
379 | $units = $row['units']; |
||
380 | if ($convertedUnits) { |
||
381 | $units = $convertedUnits; |
||
382 | } |
||
383 | if ($originalQuantity <= 1) { |
||
384 | $units = rtrim($units); |
||
385 | $units = rtrim($units, 's'); |
||
386 | } |
||
387 | $ingredient .= " ".$units; |
||
388 | } |
||
389 | } |
||
390 | if ($row['ingredient']) { |
||
391 | $ingredient .= " ".$row['ingredient']; |
||
392 | } |
||
393 | if ($raw) { |
||
394 | $ingredient = Template::raw($ingredient); |
||
395 | } |
||
396 | array_push($result, $ingredient); |
||
397 | } |
||
398 | } |
||
399 | |||
400 | return $result; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Convert decimal numbers into fractions |
||
405 | * |
||
406 | * @param $quantity |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | private function convertToFractions($quantity) |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Get all of the directions for this recipe |
||
483 | * |
||
484 | * @param bool $raw |
||
485 | * |
||
486 | * @return array |
||
487 | */ |
||
488 | public function getDirections($raw = true) |
||
489 | { |
||
490 | $result = []; |
||
491 | if ($this->directions != '') { |
||
492 | foreach ($this->directions as $row) { |
||
493 | $direction = $row['direction']; |
||
494 | if ($raw) { |
||
495 | $direction = Template::raw($direction); |
||
496 | } |
||
497 | array_push($result, $direction); |
||
498 | } |
||
499 | } |
||
500 | |||
501 | return $result; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Get the aggregate rating from all of the ratings |
||
506 | * |
||
507 | * @return float|int|string |
||
508 | */ |
||
509 | public function getAggregateRating() |
||
510 | { |
||
511 | $result = 0; |
||
512 | $total = 0; |
||
513 | if (isset($this->ratings) && !empty($this->ratings)) { |
||
514 | foreach ($this->ratings as $row) { |
||
515 | $result += $row['rating']; |
||
516 | $total++; |
||
517 | } |
||
518 | $result = $result / $total; |
||
519 | } else { |
||
520 | $result = ""; |
||
521 | } |
||
522 | |||
523 | return $result; |
||
524 | } |
||
525 | |||
526 | // Private Methods |
||
527 | // ========================================================================= |
||
528 | |||
529 | /** |
||
530 | * Get the total number of ratings |
||
531 | * |
||
532 | * @return int |
||
533 | */ |
||
534 | public function getRatingsCount() |
||
535 | { |
||
536 | return count($this->ratings); |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Renders a JSON-LD representation of the schema |
||
541 | * |
||
542 | * @param $json |
||
543 | * @param bool $raw |
||
544 | * |
||
545 | * @return string|\Twig_Markup |
||
546 | */ |
||
547 | private function renderJsonLd($json, $raw = true) |
||
568 | } |
||
569 | } |
||
570 |