| Total Complexity | 67 |
| Total Lines | 589 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| 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 |
||
| 28 | class Recipe extends Model |
||
| 29 | { |
||
| 30 | // Constants |
||
| 31 | // ========================================================================= |
||
| 32 | |||
| 33 | const US_RDA = [ |
||
| 34 | 'calories' => 2000, |
||
| 35 | 'carbohydrateContent' => 275, |
||
| 36 | 'cholesterolContent' => 300, |
||
| 37 | 'fatContent' => 78, |
||
| 38 | 'fiberContent' => 28, |
||
| 39 | 'proteinContent' => 50, |
||
| 40 | 'saturatedFatContent' => 20, |
||
| 41 | 'sodiumContent' => 2300, |
||
| 42 | 'sugarContent' => 50, |
||
| 43 | ]; |
||
| 44 | |||
| 45 | // Mapping to convert any of the incorrect plural values |
||
| 46 | const NORMALIZE_PLURALS = [ |
||
| 47 | 'tsps' => 'tsp', |
||
| 48 | 'tbsps' => 'tbsp', |
||
| 49 | 'flozs' => 'floz', |
||
| 50 | 'cups' => 'c', |
||
| 51 | 'ozs' => 'oz', |
||
| 52 | 'lbs' => 'lb', |
||
| 53 | 'mls' => 'ml', |
||
| 54 | 'ls' => 'l', |
||
| 55 | 'mgs' => 'mg', |
||
| 56 | 'gs' => 'g', |
||
| 57 | 'kg' => 'kg', |
||
| 58 | ]; |
||
| 59 | |||
| 60 | // Public Properties |
||
| 61 | // ========================================================================= |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | public $name; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $description; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | public $skill = 'intermediate'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var int |
||
| 80 | */ |
||
| 81 | public $serves = 1; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | public $ingredients = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | public $directions = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | public $imageId = 0; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | public $prepTime; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | public $cookTime; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var int |
||
| 110 | */ |
||
| 111 | public $totalTime; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | public $ratings = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | public $servingSize; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var int |
||
| 125 | */ |
||
| 126 | public $calories; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var int |
||
| 130 | */ |
||
| 131 | public $carbohydrateContent; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var int |
||
| 135 | */ |
||
| 136 | public $cholesterolContent; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var int |
||
| 140 | */ |
||
| 141 | public $fatContent; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var int |
||
| 145 | */ |
||
| 146 | public $fiberContent; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var int |
||
| 150 | */ |
||
| 151 | public $proteinContent; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var int |
||
| 155 | */ |
||
| 156 | public $saturatedFatContent; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var int |
||
| 160 | */ |
||
| 161 | public $sodiumContent; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var int |
||
| 165 | */ |
||
| 166 | public $sugarContent; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var int |
||
| 170 | */ |
||
| 171 | public $transFatContent; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var int |
||
| 175 | */ |
||
| 176 | public $unsaturatedFatContent; |
||
| 177 | |||
| 178 | // Public Methods |
||
| 179 | // ========================================================================= |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @inheritdoc |
||
| 183 | */ |
||
| 184 | public function init() |
||
| 185 | { |
||
| 186 | parent::init(); |
||
| 187 | // Fix any of the incorrect plural values |
||
| 188 | if (!empty($this->ingredients)) { |
||
| 189 | foreach ($this->ingredients as &$row) { |
||
| 190 | if (!empty($row['units']) && !empty(self::NORMALIZE_PLURALS[$row['units']])) { |
||
| 191 | $row['units'] = self::NORMALIZE_PLURALS[$row['units']]; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | unset($row); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @inheritdoc |
||
| 200 | */ |
||
| 201 | public function rules() |
||
| 225 | ]; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Render the JSON-LD Structured Data for this recipe |
||
| 230 | * |
||
| 231 | * @param bool $raw |
||
| 232 | * |
||
| 233 | * @return string|\Twig_Markup |
||
| 234 | */ |
||
| 235 | public function renderRecipeJSONLD($raw = true) |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the URL to the recipe's image |
||
| 316 | * |
||
| 317 | * @param null $transform |
||
| 318 | * |
||
| 319 | * @return null|string |
||
| 320 | */ |
||
| 321 | public function getImageUrl($transform = null) |
||
| 322 | { |
||
| 323 | $result = ''; |
||
| 324 | if (isset($this->imageId) && $this->imageId) { |
||
| 325 | $image = Craft::$app->getAssets()->getAssetById($this->imageId[0]); |
||
| 326 | if ($image) { |
||
| 327 | $result = $image->getUrl($transform); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | return $result; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Render the Nutrition Facts template |
||
| 336 | * |
||
| 337 | * @param array $rda |
||
| 338 | * @return Markup |
||
| 339 | */ |
||
| 340 | public function renderNutritionFacts(array $rda = self::US_RDA): Markup { |
||
| 341 | return PluginTemplate::renderPluginTemplate( |
||
| 342 | 'recipe-nutrition-facts', |
||
| 343 | [ |
||
| 344 | 'value' => $this, |
||
| 345 | 'rda' => $rda, |
||
| 346 | ] |
||
| 347 | ); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get all of the ingredients for this recipe |
||
| 352 | * |
||
| 353 | * @param string $outputUnits |
||
| 354 | * @param int $serving |
||
| 355 | * @param bool $raw |
||
| 356 | * |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | public function getIngredients($outputUnits = 'imperial', $serving = 0, $raw = true): array |
||
| 360 | { |
||
| 361 | $result = []; |
||
| 362 | |||
| 363 | if (!empty($this->ingredients)) { |
||
| 364 | foreach ($this->ingredients as $row) { |
||
| 365 | $convertedUnits = ''; |
||
| 366 | $ingredient = ''; |
||
| 367 | if ($row['quantity']) { |
||
| 368 | // Multiply the quantity by how many servings we want |
||
| 369 | $multiplier = 1; |
||
| 370 | if ($serving > 0) { |
||
| 371 | $multiplier = $serving / $this->serves; |
||
| 372 | } |
||
| 373 | $quantity = $row['quantity'] * $multiplier; |
||
| 374 | $originalQuantity = $quantity; |
||
| 375 | |||
| 376 | // Do the imperial->metric units conversion |
||
| 377 | if ($outputUnits === 'imperial') { |
||
| 378 | switch ($row['units']) { |
||
| 379 | case 'ml': |
||
| 380 | $convertedUnits = 'tsp'; |
||
| 381 | $quantity *= 0.2; |
||
| 382 | break; |
||
| 383 | case 'l': |
||
| 384 | $convertedUnits = 'c'; |
||
| 385 | $quantity *= 4.2; |
||
| 386 | break; |
||
| 387 | case 'mg': |
||
| 388 | $convertedUnits = 'oz'; |
||
| 389 | $quantity *= 0.000035274; |
||
| 390 | break; |
||
| 391 | case 'g': |
||
| 392 | $convertedUnits = 'oz'; |
||
| 393 | $quantity *= 0.035274; |
||
| 394 | break; |
||
| 395 | case 'kg': |
||
| 396 | $convertedUnits = 'lb'; |
||
| 397 | $quantity *= 2.2046226218; |
||
| 398 | break; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | // Do the metric->imperial units conversion |
||
| 402 | if ($outputUnits === 'metric') { |
||
| 403 | switch ($row['units']) { |
||
| 404 | case 'tsp': |
||
| 405 | $convertedUnits = 'ml'; |
||
| 406 | $quantity *= 4.929; |
||
| 407 | break; |
||
| 408 | case 'tbsp': |
||
| 409 | $convertedUnits = 'ml'; |
||
| 410 | $quantity *= 14.787; |
||
| 411 | break; |
||
| 412 | case 'floz': |
||
| 413 | $convertedUnits = 'ml'; |
||
| 414 | $quantity *= 29.574; |
||
| 415 | break; |
||
| 416 | case 'c': |
||
| 417 | $convertedUnits = 'l'; |
||
| 418 | $quantity *= 0.236588; |
||
| 419 | break; |
||
| 420 | case 'oz': |
||
| 421 | $convertedUnits = 'g'; |
||
| 422 | $quantity *= 28.3495; |
||
| 423 | break; |
||
| 424 | case 'lb': |
||
| 425 | $convertedUnits = 'kg'; |
||
| 426 | $quantity *= 0.45359237; |
||
| 427 | break; |
||
| 428 | } |
||
| 429 | |||
| 430 | $quantity = round($quantity, 1); |
||
| 431 | } |
||
| 432 | |||
| 433 | // Convert imperial units to nice fractions |
||
| 434 | if ($outputUnits === 'imperial') { |
||
| 435 | $quantity = $this->convertToFractions($quantity); |
||
| 436 | } |
||
| 437 | $ingredient .= $quantity; |
||
| 438 | |||
| 439 | if ($row['units']) { |
||
| 440 | $units = $row['units']; |
||
| 441 | if ($convertedUnits) { |
||
| 442 | $units = $convertedUnits; |
||
| 443 | } |
||
| 444 | if ($originalQuantity <= 1) { |
||
| 445 | $units = rtrim($units); |
||
| 446 | $units = rtrim($units, 's'); |
||
| 447 | } |
||
| 448 | $ingredient .= ' ' . $units; |
||
| 449 | } |
||
| 450 | } |
||
| 451 | if ($row['ingredient']) { |
||
| 452 | $ingredient .= ' ' . $row['ingredient']; |
||
| 453 | } |
||
| 454 | if ($raw) { |
||
| 455 | $ingredient = Template::raw($ingredient); |
||
| 456 | } |
||
| 457 | $result[] = $ingredient; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | |||
| 461 | return $result; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Convert decimal numbers into fractions |
||
| 466 | * |
||
| 467 | * @param $quantity |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | private function convertToFractions($quantity) |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Get all of the directions for this recipe |
||
| 532 | * |
||
| 533 | * @param bool $raw |
||
| 534 | * |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | public function getDirections($raw = true) |
||
| 538 | { |
||
| 539 | $result = []; |
||
| 540 | if (!empty($this->directions)) { |
||
| 541 | foreach ($this->directions as $row) { |
||
| 542 | $direction = $row['direction']; |
||
| 543 | if ($raw) { |
||
| 544 | $direction = Template::raw($direction); |
||
| 545 | } |
||
| 546 | $result[] = $direction; |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | return $result; |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Get the aggregate rating from all of the ratings |
||
| 555 | * |
||
| 556 | * @return float|int|string |
||
| 557 | */ |
||
| 558 | public function getAggregateRating() |
||
| 559 | { |
||
| 560 | $result = 0; |
||
| 561 | $total = 0; |
||
| 562 | if (isset($this->ratings) && !empty($this->ratings)) { |
||
| 563 | foreach ($this->ratings as $row) { |
||
| 564 | $result += $row['rating']; |
||
| 565 | $total++; |
||
| 566 | } |
||
| 567 | $result /= $total; |
||
| 568 | } else { |
||
| 569 | $result = ''; |
||
| 570 | } |
||
| 571 | |||
| 572 | return $result; |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Get the total number of ratings |
||
| 577 | * |
||
| 578 | * @return int |
||
| 579 | */ |
||
| 580 | public function getRatingsCount(): int |
||
| 581 | { |
||
| 582 | return count($this->ratings); |
||
| 583 | } |
||
| 584 | |||
| 585 | // Private Methods |
||
| 586 | // ========================================================================= |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Renders a JSON-LD representation of the schema |
||
| 590 | * |
||
| 591 | * @param $json |
||
| 592 | * @param bool $raw |
||
| 593 | * |
||
| 594 | * @return string|\Twig_Markup |
||
| 595 | */ |
||
| 596 | private function renderJsonLd($json, $raw = true) |
||
| 617 | } |
||
| 618 | } |
||
| 619 |