Conditions | 11 |
Paths | 18 |
Total Lines | 80 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
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 |
||
55 | public function actionGenerate(): int |
||
56 | { |
||
57 | /** @var Settings $settings */ |
||
58 | $settings = Recipe::$plugin->getSettings(); |
||
59 | if (!$settings->hasApiCredentials()) { |
||
60 | $this->stderr(Craft::t('recipe', 'API credentials do not exist in plugin settings.') . PHP_EOL, BaseConsole::FG_RED); |
||
61 | |||
62 | return ExitCode::OK; |
||
63 | } |
||
64 | |||
65 | if ($this->section === null) { |
||
66 | $this->stderr(Craft::t('recipe', 'A section handle must be provided using --section.') . PHP_EOL, BaseConsole::FG_RED); |
||
67 | |||
68 | return ExitCode::OK; |
||
69 | } |
||
70 | |||
71 | if ($this->field === null) { |
||
72 | $this->stderr(Craft::t('recipe', 'A field handle must be provided using --field.') . PHP_EOL, BaseConsole::FG_RED); |
||
73 | |||
74 | return ExitCode::OK; |
||
75 | } |
||
76 | |||
77 | $entries = Entry::find()->section($this->section)->all(); |
||
78 | |||
79 | if (empty($entries)) { |
||
80 | $this->stderr(Craft::t('recipe', 'No entries found in the section with handle `{handle}`.', ['handle' => $this->section]) . PHP_EOL, BaseConsole::FG_RED); |
||
81 | |||
82 | return ExitCode::OK; |
||
83 | } |
||
84 | |||
85 | $total = count($entries); |
||
86 | $count = 0; |
||
87 | $failed = 0; |
||
88 | |||
89 | $this->stdout(Craft::t('recipe', 'Generating nutritional information for {count} entries...', ['count' => $total]) . PHP_EOL, BaseConsole::FG_YELLOW); |
||
90 | |||
91 | Console::startProgress($count, $total, '', 0.8); |
||
92 | |||
93 | foreach ($entries as $entry) { |
||
94 | $field = $entry->{$this->field}; |
||
95 | $ingredients = $field->ingredients; |
||
96 | |||
97 | foreach ($ingredients as $key => $value) { |
||
98 | $ingredients[$key] = implode(' ', $value); |
||
99 | } |
||
100 | |||
101 | $nutritionalInfo = Recipe::$plugin->nutritionApi->getNutritionalInfo($ingredients, $field->serves); |
||
102 | |||
103 | if (empty($nutritionalInfo['error'])) { |
||
104 | $recipe = $entry->{$this->field}; |
||
105 | |||
106 | foreach ($nutritionalInfo as $fieldHandle => $value) { |
||
107 | $recipe[$fieldHandle] = $value; |
||
108 | } |
||
109 | |||
110 | $entry->setFieldValue($this->field, $recipe); |
||
111 | |||
112 | if (!Craft::$app->getElements()->saveElement($entry)) { |
||
113 | ++$failed; |
||
114 | } |
||
115 | } else { |
||
116 | ++$failed; |
||
117 | } |
||
118 | |||
119 | ++$count; |
||
120 | |||
121 | Console::updateProgress($count, $total); |
||
122 | } |
||
123 | |||
124 | Console::endProgress(); |
||
125 | |||
126 | $succeeded = $count - $failed; |
||
127 | |||
128 | $this->stdout(Craft::t('recipe', 'Successfully generated nutritional information for {count} entries.', ['count' => $succeeded]) . PHP_EOL, BaseConsole::FG_GREEN); |
||
129 | |||
130 | if ($failed > 0) { |
||
131 | $this->stderr(Craft::t('recipe', 'Failed to generate nutritional information for {count} entries.', ['count' => $failed]) . PHP_EOL, BaseConsole::FG_RED); |
||
132 | } |
||
133 | |||
134 | return ExitCode::OK; |
||
135 | } |
||
137 |