Passed
Push — develop ( 19391e...30063e )
by Andrew
07:28
created

NutritionApiController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 51
c 8
b 0
f 0
dl 0
loc 112
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 7 1
C actionGenerate() 0 85 12
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
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
11
12
namespace nystudio107\recipe\console\controllers;
13
14
use Craft;
15
use craft\console\Controller;
16
use craft\elements\Entry;
17
use craft\helpers\Console;
18
use nystudio107\recipe\Recipe;
19
use yii\console\ExitCode;
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
23
 * @package   Recipe
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @since     1.3.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
26
class NutritionApiController extends Controller
27
{
28
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
29
     * @var string The handle of the section.
30
     */
31
    public $section;
32
33
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
34
     * @var string The handle of the recipe field.
35
     */
36
    public $field;
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $actionID should have a doc-comment as per coding-style.
Loading history...
39
     * @inheritdoc
40
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
41
    public function options($actionID)
42
    {
43
        $options = parent::options($actionID);
44
        $options[] = 'section';
45
        $options[] = 'field';
46
47
        return $options;
48
    }
49
50
    /**
51
     * Generates nutritional information for all entries in a section provided using --section.
52
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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);
0 ignored issues
show
Bug introduced by
0.8 of type double is incompatible with the type boolean|integer expected by parameter $width of yii\helpers\BaseConsole::startProgress(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
        Console::startProgress($count, $total, '', /** @scrutinizer ignore-type */ 0.8);
Loading history...
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 {
0 ignored issues
show
Coding Style introduced by
Expected "} else \n"; found "\n else {\n"
Loading history...
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
    }
139
}
140