Conditions | 7 |
Paths | 21 |
Total Lines | 53 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 23 | ||
Bugs | 1 | 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 |
||
32 | public function getNutritionalInfo(array $ingredients, int $serves = null): array |
||
33 | { |
||
34 | /** @var Settings $settings */ |
||
35 | $settings = Recipe::$plugin->getSettings(); |
||
36 | if (!$settings->hasApiCredentials()) { |
||
37 | return []; |
||
38 | } |
||
39 | |||
40 | $url = 'https://api.edamam.com/api/nutrition-details' |
||
41 | . '?app_id=' . Craft::parseEnv($settings->apiApplicationId) |
||
42 | . '&app_key=' . Craft::parseEnv($settings->apiApplicationKey); |
||
43 | |||
44 | $data = [ |
||
45 | 'ingr' => $ingredients, |
||
46 | ]; |
||
47 | |||
48 | if ($serves) { |
||
49 | $data['yield'] = $serves; |
||
50 | } |
||
51 | |||
52 | try { |
||
53 | $response = Craft::createGuzzleClient()->post($url, ['json' => $data]); |
||
54 | |||
55 | $result = json_decode($response->getBody(), null, 512, JSON_THROW_ON_ERROR); |
||
56 | |||
57 | $yield = $result->yield ?: 1; |
||
58 | |||
59 | return [ |
||
60 | 'servingSize' => round($result->totalWeight ?? 0 / $yield, 0) . ' grams', |
||
61 | 'calories' => round($result->totalNutrients->ENERC_KCAL->quantity ?? 0 / $yield, 0), |
||
62 | 'carbohydrateContent' => round($result->totalNutrients->CHOCDF->quantity ?? 0 / $yield, 1), |
||
63 | 'cholesterolContent' => round($result->totalNutrients->CHOLE->quantity ?? 0 / $yield, 1), |
||
64 | 'fatContent' => round($result->totalNutrients->FAT->quantity ?? 0 / $yield, 1), |
||
65 | 'fiberContent' => round($result->totalNutrients->FIBTG->quantity ?? 0 / $yield, 1), |
||
66 | 'proteinContent' => round($result->totalNutrients->PROCNT->quantity ?? 0 / $yield, 1), |
||
67 | 'saturatedFatContent' => round($result->totalNutrients->FASAT->quantity ?? 0 / $yield, 1), |
||
68 | 'sodiumContent' => round($result->totalNutrients->NA->quantity ?? 0 / $yield, 1), |
||
69 | 'sugarContent' => round($result->totalNutrients->SUGAR->quantity ?? 0 / $yield, 1), |
||
70 | 'transFatContent' => round($result->totalNutrients->FATRN->quantity ?? 0 / $yield, 1), |
||
71 | 'unsaturatedFatContent' => round((($result->totalNutrients->FAMS->quantity ?? 0) + ($result->totalNutrients->FAPU->quantity ?? 0)) / $yield, 1), |
||
72 | ]; |
||
73 | } catch (Exception $exception) { |
||
74 | $message = 'Error fetching nutritional information from API. '; |
||
75 | |||
76 | if ($exception->getCode() == 401) { |
||
77 | $message .= 'Please verify your API credentials.'; |
||
78 | } elseif ($exception->getCode() == 555) { |
||
79 | $message .= 'One or more ingredients could not be recognized.'; |
||
80 | } |
||
81 | |||
82 | Craft::error($message . $exception->getMessage(), __METHOD__); |
||
83 | |||
84 | return ['error' => $message]; |
||
85 | } |
||
88 |