| Conditions | 7 |
| Paths | 21 |
| Total Lines | 55 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 19 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 33 | public function getNutritionalInfo(array $ingredients, int $serves = null): array |
||
| 34 | { |
||
| 35 | if (Recipe::$plugin->settings->hasApiCredentials() === false) { |
||
| 36 | return []; |
||
| 37 | } |
||
| 38 | |||
| 39 | $url = 'https://api.edamam.com/api/nutrition-details' |
||
| 40 | .'?app_id='.Craft::parseEnv(Recipe::$plugin->settings->apiApplicationId) |
||
| 41 | .'&app_key='.Craft::parseEnv(Recipe::$plugin->settings->apiApplicationKey); |
||
| 42 | |||
| 43 | $data = [ |
||
| 44 | 'ingr' => $ingredients, |
||
| 45 | ]; |
||
| 46 | |||
| 47 | if ($serves) { |
||
| 48 | $data['yield'] = $serves; |
||
| 49 | } |
||
| 50 | |||
| 51 | try { |
||
| 52 | $response = Craft::createGuzzleClient()->post($url, ['json' => $data]); |
||
| 53 | |||
| 54 | $result = json_decode($response->getBody()); |
||
| 55 | |||
| 56 | $yield = $result->yield ?: 1; |
||
| 57 | |||
| 58 | return [ |
||
| 59 | 'servingSize' => round($result->totalWeight / $yield, 0).' grams', |
||
| 60 | 'calories' => round($result->totalNutrients->ENERC_KCAL->quantity / $yield, 0), |
||
| 61 | 'carbohydrateContent' => round($result->totalNutrients->CHOCDF->quantity / $yield, 1), |
||
| 62 | 'cholesterolContent' => round($result->totalNutrients->CHOLE->quantity / $yield, 1), |
||
| 63 | 'fatContent' => round($result->totalNutrients->FAT->quantity / $yield, 1), |
||
| 64 | 'fiberContent' => round($result->totalNutrients->FIBTG->quantity / $yield, 1), |
||
| 65 | 'proteinContent' => round($result->totalNutrients->PROCNT->quantity / $yield, 1), |
||
| 66 | 'saturatedFatContent' => round($result->totalNutrients->FASAT->quantity / $yield, 1), |
||
| 67 | 'sodiumContent' => round($result->totalNutrients->NA->quantity / $yield, 1), |
||
| 68 | 'sugarContent' => round($result->totalNutrients->SUGAR->quantity / $yield, 1), |
||
| 69 | 'transFatContent' => round($result->totalNutrients->FATRN->quantity / $yield, 1), |
||
| 70 | 'unsaturatedFatContent' => round(($result->totalNutrients->FAMS->quantity + $result->totalNutrients->FAPU->quantity) / $yield, 1), |
||
| 71 | ]; |
||
| 72 | } |
||
| 73 | catch (Exception $exception) { |
||
| 74 | $message = 'Error fetching nutritional information from API. '; |
||
| 75 | |||
| 76 | switch ($exception->getCode()) { |
||
| 77 | case 401: |
||
| 78 | $message .= 'Please verify your API credentials.'; |
||
| 79 | break; |
||
| 80 | case 555: |
||
| 81 | $message .= 'One or more ingredients could not be recognized.'; |
||
| 82 | break; |
||
| 83 | } |
||
| 84 | |||
| 85 | Craft::error($message.$exception->getMessage(), __METHOD__); |
||
| 86 | |||
| 87 | return ['error' => $message]; |
||
| 88 | } |
||
| 91 |