| Total Complexity | 80 |
| Total Lines | 682 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| 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 |
||
| 32 | class Recipe extends Model |
||
| 33 | { |
||
| 34 | // Constants |
||
| 35 | // ========================================================================= |
||
| 36 | |||
| 37 | const SEOMATIC_PLUGIN_HANDLE = 'seomatic'; |
||
| 38 | const MAIN_ENTITY_KEY = 'mainEntityOfPage'; |
||
| 39 | |||
| 40 | const US_RDA = [ |
||
| 41 | 'calories' => 2000, |
||
| 42 | 'carbohydrateContent' => 275, |
||
| 43 | 'cholesterolContent' => 300, |
||
| 44 | 'fatContent' => 78, |
||
| 45 | 'fiberContent' => 28, |
||
| 46 | 'proteinContent' => 50, |
||
| 47 | 'saturatedFatContent' => 20, |
||
| 48 | 'sodiumContent' => 2300, |
||
| 49 | 'sugarContent' => 50, |
||
| 50 | ]; |
||
| 51 | |||
| 52 | // Mapping to convert any of the incorrect plural values |
||
| 53 | const NORMALIZE_PLURALS = [ |
||
| 54 | 'tsps' => 'tsp', |
||
| 55 | 'tbsps' => 'tbsp', |
||
| 56 | 'flozs' => 'floz', |
||
| 57 | 'cups' => 'cups', |
||
| 58 | 'ozs' => 'oz', |
||
| 59 | 'lbs' => 'lb', |
||
| 60 | 'mls' => 'ml', |
||
| 61 | 'ls' => 'l', |
||
| 62 | 'mgs' => 'mg', |
||
| 63 | 'gs' => 'g', |
||
| 64 | 'kg' => 'kg', |
||
| 65 | ]; |
||
| 66 | |||
| 67 | // Public Properties |
||
| 68 | // ========================================================================= |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public $name; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | public $description; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | public $skill = 'intermediate'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | public $serves = 1; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | public $ingredients = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | public $directions = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | public $equipment = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var int |
||
| 107 | */ |
||
| 108 | public $imageId = 0; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var int |
||
| 112 | */ |
||
| 113 | public $prepTime; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var int |
||
| 117 | */ |
||
| 118 | public $cookTime; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var int |
||
| 122 | */ |
||
| 123 | public $totalTime; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | public $ratings = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | public $servingSize; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var int |
||
| 137 | */ |
||
| 138 | public $calories; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var int |
||
| 142 | */ |
||
| 143 | public $carbohydrateContent; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var int |
||
| 147 | */ |
||
| 148 | public $cholesterolContent; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var int |
||
| 152 | */ |
||
| 153 | public $fatContent; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var int |
||
| 157 | */ |
||
| 158 | public $fiberContent; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var int |
||
| 162 | */ |
||
| 163 | public $proteinContent; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var int |
||
| 167 | */ |
||
| 168 | public $saturatedFatContent; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var int |
||
| 172 | */ |
||
| 173 | public $sodiumContent; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var int |
||
| 177 | */ |
||
| 178 | public $sugarContent; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var int |
||
| 182 | */ |
||
| 183 | public $transFatContent; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var int |
||
| 187 | */ |
||
| 188 | public $unsaturatedFatContent; |
||
| 189 | |||
| 190 | // Public Methods |
||
| 191 | // ========================================================================= |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @inheritdoc |
||
| 195 | */ |
||
| 196 | public function init() |
||
| 197 | { |
||
| 198 | parent::init(); |
||
| 199 | // Fix any of the incorrect plural values |
||
| 200 | if (!empty($this->ingredients)) { |
||
| 201 | foreach ($this->ingredients as &$row) { |
||
| 202 | if (!empty($row['units']) && !empty(self::NORMALIZE_PLURALS[$row['units']])) { |
||
| 203 | $row['units'] = self::NORMALIZE_PLURALS[$row['units']]; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | unset($row); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @inheritdoc |
||
| 212 | */ |
||
| 213 | public function rules() |
||
| 214 | { |
||
| 215 | return [ |
||
| 216 | ['name', 'string'], |
||
| 217 | ['name', 'default', 'value' => ''], |
||
| 218 | ['description', 'string'], |
||
| 219 | ['skill', 'string'], |
||
| 220 | ['serves', 'integer'], |
||
| 221 | ['imageId', 'integer'], |
||
| 222 | ['prepTime', 'integer'], |
||
| 223 | ['cookTime', 'integer'], |
||
| 224 | ['totalTime', 'integer'], |
||
| 225 | ['servingSize', 'string'], |
||
| 226 | ['calories', 'integer'], |
||
| 227 | ['carbohydrateContent', 'integer'], |
||
| 228 | ['cholesterolContent', 'integer'], |
||
| 229 | ['fatContent', 'integer'], |
||
| 230 | ['fiberContent', 'integer'], |
||
| 231 | ['proteinContent', 'integer'], |
||
| 232 | ['saturatedFatContent', 'integer'], |
||
| 233 | ['sodiumContent', 'integer'], |
||
| 234 | ['sugarContent', 'integer'], |
||
| 235 | ['transFatContent', 'integer'], |
||
| 236 | ['unsaturatedFatContent', 'integer'], |
||
| 237 | [ |
||
| 238 | [ |
||
| 239 | 'ingredients', |
||
| 240 | 'directions', |
||
| 241 | 'equipment', |
||
| 242 | ], |
||
| 243 | ArrayValidator::class, |
||
| 244 | ], |
||
| 245 | |||
| 246 | ]; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Return the JSON-LD Structured Data for this recipe |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function getRecipeJSONLD(): array |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Create the SEOmatic MetaJsonLd object for this recipe |
||
| 336 | * |
||
| 337 | * @param bool $add |
||
| 338 | * @param null $key |
||
| 339 | * @return null|MetaJsonLd |
||
| 340 | */ |
||
| 341 | public function createRecipeMetaJsonLd($key = null, bool $add = true) |
||
| 342 | { |
||
| 343 | $result = null; |
||
| 344 | if (Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE)) { |
||
| 345 | $seomatic = Seomatic::getInstance(); |
||
| 346 | if ($seomatic !== null) { |
||
| 347 | $recipeJson = $this->getRecipeJSONLD(); |
||
| 348 | // If we're adding the MetaJsonLd to the container, and no key is provided, give it a random key |
||
| 349 | if ($add && $key === null) { |
||
| 350 | try { |
||
| 351 | $key = StringHelper::UUID(); |
||
| 352 | } catch (\Exception $e) { |
||
| 353 | // That's okay |
||
| 354 | } |
||
| 355 | } |
||
| 356 | if ($key !== null) { |
||
| 357 | $recipeJson['key'] = $key; |
||
| 358 | } |
||
| 359 | // If the key is `mainEntityOfPage` add in the URL |
||
| 360 | if ($key === self::MAIN_ENTITY_KEY) { |
||
| 361 | $mainEntity = Seomatic::$plugin->jsonLd->get(self::MAIN_ENTITY_KEY); |
||
| 362 | if ($mainEntity) { |
||
| 363 | $recipeJson[self::MAIN_ENTITY_KEY] = $mainEntity[self::MAIN_ENTITY_KEY]; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | $result = Seomatic::$plugin->jsonLd->create( |
||
| 368 | $recipeJson, |
||
| 369 | $add |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | return $result; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Render the JSON-LD Structured Data for this recipe |
||
| 379 | * |
||
| 380 | * @param bool $raw |
||
| 381 | * |
||
| 382 | * @return string|\Twig_Markup |
||
| 383 | */ |
||
| 384 | public function renderRecipeJSONLD($raw = true) |
||
| 385 | { |
||
| 386 | return $this->renderJsonLd($this->getRecipeJSONLD(), $raw); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get the URL to the recipe's image |
||
| 391 | * |
||
| 392 | * @param null $transform |
||
| 393 | * |
||
| 394 | * @return null|string |
||
| 395 | */ |
||
| 396 | public function getImageUrl($transform = null) |
||
| 397 | { |
||
| 398 | $result = ''; |
||
| 399 | if (isset($this->imageId) && $this->imageId) { |
||
| 400 | $image = Craft::$app->getAssets()->getAssetById($this->imageId[0]); |
||
| 401 | if ($image) { |
||
| 402 | $result = $image->getUrl($transform); |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | return $result; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Render the Nutrition Facts template |
||
| 411 | * |
||
| 412 | * @param array $rda |
||
| 413 | * @return Markup |
||
| 414 | */ |
||
| 415 | public function renderNutritionFacts(array $rda = self::US_RDA): Markup { |
||
| 416 | return PluginTemplate::renderPluginTemplate( |
||
| 417 | 'recipe-nutrition-facts', |
||
| 418 | [ |
||
| 419 | 'value' => $this, |
||
| 420 | 'rda' => $rda, |
||
| 421 | ] |
||
| 422 | ); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get all of the ingredients for this recipe |
||
| 427 | * |
||
| 428 | * @param string $outputUnits |
||
| 429 | * @param int $serving |
||
| 430 | * @param bool $raw |
||
| 431 | * |
||
| 432 | * @return array |
||
| 433 | */ |
||
| 434 | public function getIngredients($outputUnits = 'imperial', $serving = 0, $raw = true): array |
||
| 435 | { |
||
| 436 | $result = []; |
||
| 437 | |||
| 438 | if (!empty($this->ingredients)) { |
||
| 439 | foreach ($this->ingredients as $row) { |
||
| 440 | $convertedUnits = ''; |
||
| 441 | $ingredient = ''; |
||
| 442 | if ($row['quantity']) { |
||
| 443 | // Multiply the quantity by how many servings we want |
||
| 444 | $multiplier = 1; |
||
| 445 | if ($serving > 0) { |
||
| 446 | $multiplier = $serving / $this->serves; |
||
| 447 | } |
||
| 448 | $quantity = $row['quantity'] * $multiplier; |
||
| 449 | $originalQuantity = $quantity; |
||
| 450 | |||
| 451 | // Do the imperial->metric units conversion |
||
| 452 | if ($outputUnits === 'imperial') { |
||
| 453 | switch ($row['units']) { |
||
| 454 | case 'ml': |
||
| 455 | $convertedUnits = 'tsp'; |
||
| 456 | $quantity *= 0.2; |
||
| 457 | break; |
||
| 458 | case 'l': |
||
| 459 | $convertedUnits = 'cups'; |
||
| 460 | $quantity *= 4.2; |
||
| 461 | break; |
||
| 462 | case 'mg': |
||
| 463 | $convertedUnits = 'oz'; |
||
| 464 | $quantity *= 0.000035274; |
||
| 465 | break; |
||
| 466 | case 'g': |
||
| 467 | $convertedUnits = 'oz'; |
||
| 468 | $quantity *= 0.035274; |
||
| 469 | break; |
||
| 470 | case 'kg': |
||
| 471 | $convertedUnits = 'lb'; |
||
| 472 | $quantity *= 2.2046226218; |
||
| 473 | break; |
||
| 474 | } |
||
| 475 | } |
||
| 476 | // Do the metric->imperial units conversion |
||
| 477 | if ($outputUnits === 'metric') { |
||
| 478 | switch ($row['units']) { |
||
| 479 | case 'tsp': |
||
| 480 | $convertedUnits = 'ml'; |
||
| 481 | $quantity *= 4.929; |
||
| 482 | break; |
||
| 483 | case 'tbsp': |
||
| 484 | $convertedUnits = 'ml'; |
||
| 485 | $quantity *= 14.787; |
||
| 486 | break; |
||
| 487 | case 'floz': |
||
| 488 | $convertedUnits = 'ml'; |
||
| 489 | $quantity *= 29.574; |
||
| 490 | break; |
||
| 491 | case 'cups': |
||
| 492 | $convertedUnits = 'l'; |
||
| 493 | $quantity *= 0.236588; |
||
| 494 | break; |
||
| 495 | case 'oz': |
||
| 496 | $convertedUnits = 'g'; |
||
| 497 | $quantity *= 28.3495; |
||
| 498 | break; |
||
| 499 | case 'lb': |
||
| 500 | $convertedUnits = 'kg'; |
||
| 501 | $quantity *= 0.45359237; |
||
| 502 | break; |
||
| 503 | } |
||
| 504 | |||
| 505 | $quantity = round($quantity, 1); |
||
| 506 | } |
||
| 507 | |||
| 508 | // Convert units to nice fractions |
||
| 509 | $quantity = $this->convertToFractions($quantity); |
||
| 510 | |||
| 511 | $ingredient .= $quantity; |
||
| 512 | |||
| 513 | if ($row['units']) { |
||
| 514 | $units = $row['units']; |
||
| 515 | if ($convertedUnits) { |
||
| 516 | $units = $convertedUnits; |
||
| 517 | } |
||
| 518 | if ($originalQuantity <= 1) { |
||
| 519 | $units = rtrim($units); |
||
| 520 | $units = rtrim($units, 's'); |
||
| 521 | } |
||
| 522 | $ingredient .= ' ' . $units; |
||
| 523 | } |
||
| 524 | } |
||
| 525 | if ($row['ingredient']) { |
||
| 526 | $ingredient .= ' ' . $row['ingredient']; |
||
| 527 | } |
||
| 528 | if ($raw) { |
||
| 529 | $ingredient = Template::raw($ingredient); |
||
| 530 | } |
||
| 531 | $result[] = $ingredient; |
||
| 532 | } |
||
| 533 | } |
||
| 534 | |||
| 535 | return $result; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Convert decimal numbers into fractions |
||
| 540 | * |
||
| 541 | * @param $quantity |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | private function convertToFractions($quantity) |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Get all of the directions for this recipe |
||
| 606 | * |
||
| 607 | * @param bool $raw |
||
| 608 | * |
||
| 609 | * @return array |
||
| 610 | */ |
||
| 611 | public function getDirections($raw = true) |
||
| 612 | { |
||
| 613 | $result = []; |
||
| 614 | if (!empty($this->directions)) { |
||
| 615 | foreach ($this->directions as $row) { |
||
| 616 | $direction = $row['direction']; |
||
| 617 | if ($raw) { |
||
| 618 | $direction = Template::raw($direction); |
||
| 619 | } |
||
| 620 | $result[] = $direction; |
||
| 621 | } |
||
| 622 | } |
||
| 623 | |||
| 624 | return $result; |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Get all of the equipment for this recipe |
||
| 629 | * |
||
| 630 | * @param bool $raw |
||
| 631 | * |
||
| 632 | * @return array |
||
| 633 | */ |
||
| 634 | public function getEquipment($raw = true) |
||
| 635 | { |
||
| 636 | $result = []; |
||
| 637 | if (!empty($this->equipment)) { |
||
| 638 | foreach ($this->equipment as $row) { |
||
| 639 | $equipment = $row['equipment']; |
||
| 640 | if ($raw) { |
||
| 641 | $equipment = Template::raw(equipment); |
||
| 642 | } |
||
| 643 | $result[] = $equipment; |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | return $result; |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Get the aggregate rating from all of the ratings |
||
| 652 | * |
||
| 653 | * @return float|int|string |
||
| 654 | */ |
||
| 655 | public function getAggregateRating() |
||
| 656 | { |
||
| 657 | $result = 0; |
||
| 658 | $total = 0; |
||
| 659 | if (isset($this->ratings) && !empty($this->ratings)) { |
||
| 660 | foreach ($this->ratings as $row) { |
||
| 661 | $result += $row['rating']; |
||
| 662 | $total++; |
||
| 663 | } |
||
| 664 | $result /= $total; |
||
| 665 | } else { |
||
| 666 | $result = ''; |
||
| 667 | } |
||
| 668 | |||
| 669 | return $result; |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Get the total number of ratings |
||
| 674 | * |
||
| 675 | * @return int |
||
| 676 | */ |
||
| 677 | public function getRatingsCount(): int |
||
| 678 | { |
||
| 679 | return count($this->ratings); |
||
| 680 | } |
||
| 681 | |||
| 682 | // Private Methods |
||
| 683 | // ========================================================================= |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Renders a JSON-LD representation of the schema |
||
| 687 | * |
||
| 688 | * @param $json |
||
| 689 | * @param bool $raw |
||
| 690 | * |
||
| 691 | * @return string|\Twig_Markup |
||
| 692 | */ |
||
| 693 | private function renderJsonLd($json, $raw = true) |
||
| 714 | } |
||
| 715 | } |
||
| 716 |