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