1 | <?php |
||||||
2 | /** |
||||||
3 | * Recipe plugin for Craft CMS 3.x |
||||||
4 | * |
||||||
5 | * A comprehensive recipe FieldType for Craft CMS that includes metric/imperial |
||||||
6 | * conversion, portion calculation, and JSON-LD microdata support |
||||||
7 | * |
||||||
8 | * @link https://nystudio107.com |
||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||||
9 | * @copyright Copyright (c) 2017 nystudio107 |
||||||
0 ignored issues
–
show
|
|||||||
10 | */ |
||||||
0 ignored issues
–
show
|
|||||||
11 | |||||||
12 | namespace nystudio107\recipe\services; |
||||||
13 | |||||||
14 | use Craft; |
||||||
15 | use craft\base\Component; |
||||||
16 | use Exception; |
||||||
17 | use nystudio107\recipe\models\Settings; |
||||||
18 | use nystudio107\recipe\Recipe; |
||||||
19 | |||||||
20 | /** |
||||||
0 ignored issues
–
show
|
|||||||
21 | * @author nystudio107 |
||||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||||
22 | * @package Recipe |
||||||
0 ignored issues
–
show
|
|||||||
23 | * @since 1.3.0 |
||||||
0 ignored issues
–
show
|
|||||||
24 | */ |
||||||
0 ignored issues
–
show
|
|||||||
25 | class NutritionApi extends Component |
||||||
26 | { |
||||||
27 | /** |
||||||
0 ignored issues
–
show
|
|||||||
28 | * Returns nutritional information about a recipe. |
||||||
29 | * |
||||||
30 | * @param int|null $serves |
||||||
0 ignored issues
–
show
|
|||||||
31 | */ |
||||||
0 ignored issues
–
show
|
|||||||
32 | public function getNutritionalInfo(array $ingredients, int $serves = null): array |
||||||
33 | { |
||||||
34 | /** @var Settings $settings */ |
||||||
0 ignored issues
–
show
|
|||||||
35 | $settings = Recipe::$plugin->getSettings(); |
||||||
0 ignored issues
–
show
The method
getSettings() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
36 | if (!$settings->hasApiCredentials()) { |
||||||
37 | return []; |
||||||
38 | } |
||||||
39 | |||||||
40 | $url = 'https://api.edamam.com/api/nutrition-details' |
||||||
41 | . '?app_id=' . Craft::parseEnv($settings->apiApplicationId) |
||||||
0 ignored issues
–
show
The function
Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||||
42 | . '&app_key=' . Craft::parseEnv($settings->apiApplicationKey); |
||||||
0 ignored issues
–
show
The function
Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||||
43 | |||||||
44 | $data = [ |
||||||
45 | 'ingr' => $ingredients, |
||||||
46 | ]; |
||||||
47 | |||||||
48 | if ($serves) { |
||||||
0 ignored issues
–
show
The expression
$serves of type integer|null is loosely compared to true ; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||||||
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 | } |
||||||
86 | } |
||||||
87 | } |
||||||
88 |