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