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