Passed
Push — develop ( 7a1d69...8c455e )
by Andrew
11:14 queued 05:19
created

OptimizeController::actionCreate()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 30
rs 8.8333
cc 7
nc 36
nop 1
1
<?php
2
/**
3
 * Image Optimize plugin for Craft CMS
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
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 @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\imageoptimize\console\controllers;
12
13
use Craft;
14
use craft\base\Volume;
15
use craft\helpers\App;
16
use craft\queue\QueueInterface;
17
use nystudio107\imageoptimize\ImageOptimize;
18
use yii\console\Controller;
19
use yii\queue\redis\Queue as RedisQueue;
20
21
/**
22
 * Optimize Command
23
 *
24
 * @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...
25
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
26
 * @since     1.2.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...
27
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
28
class OptimizeController extends Controller
29
{
30
    // Public Properties
31
    // =========================================================================
32
33
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
34
     * @var bool Whether image variants should be forced to recreated, even if they already exist on disk
0 ignored issues
show
Coding Style introduced by
Tag value for @var tag indented incorrectly; expected 3 spaces but found 1
Loading history...
35
     * @since 1.6.18
36
     */
37
    public $force = false;
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @var string|null Only resave image variants associated with this field handle
0 ignored issues
show
Coding Style introduced by
Tag value for @var tag indented incorrectly; expected 3 spaces but found 1
Loading history...
41
     * @since 1.6.18
42
     */
43
    public $field = null;
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @var bool Should the image generation simply be queued, rather than run immediately?
47
     */
48
    public $queue = false;
49
50
    // Public Methods
51
    // =========================================================================
52
53
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $actionID should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
54
     * @inheritDoc
55
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
56
    public function options($actionID): array
57
    {
58
        $options = parent::options($actionID);
59
        return array_merge($options, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
60
            'force',
61
            'field',
62
            'queue',
63
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
64
    }
65
66
    /**
67
     * Create all of the OptimizedImages Field variants by creating all of the responsive image variant transforms
68
     *
69
     * @param string|null $volumeHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
70
     *
71
     * @throws \yii\base\InvalidConfigException
72
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
73
    public function actionCreate($volumeHandle = null)
74
    {
75
        echo 'Creating optimized image variants' . PHP_EOL;
76
        if ($this->force) {
77
            echo 'Forcing optimized image variants creation via --force' . PHP_EOL;
78
        }
79
80
        $fieldId = null;
81
        if ($this->field !== null) {
82
            $field = Craft::$app->getFields()->getFieldByHandle($this->field);
83
            if ($field !== null) {
84
                $fieldId = $field->id;
85
            }
86
        }
87
        if ($volumeHandle === null) {
88
            // Re-save all of the optimized image variants in all volumes
89
            ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets($fieldId, $this->force);
90
        } else {
91
            // Re-save all of the optimized image variants in a specific volume
92
            $volumes = Craft::$app->getVolumes();
93
            $volume = $volumes->getVolumeByHandle($volumeHandle);
94
            if ($volume) {
95
                /** @var Volume $volume */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
96
                ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume, $fieldId, $this->force);
97
            } else {
98
                echo 'Unknown Asset Volume handle: ' . $volumeHandle . PHP_EOL;
99
            }
100
        }
101
        if (!$this->queue) {
102
            $this->runCraftQueue();
103
        }
104
    }
105
106
    /**
107
     * Create a single OptimizedImage for the passed in Asset ID
108
     *
109
     * @param int|null $id
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
110
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
111
    public function actionCreateAsset($id = null)
112
    {
113
        echo 'Creating optimized image variants' . PHP_EOL;
114
115
        if ($id === null) {
116
            echo 'No Asset ID specified' . PHP_EOL;
117
        } else {
118
            // Re-save a single Asset ID
119
            ImageOptimize::$plugin->optimizedImages->resaveAsset($id, $this->force);
120
        }
121
        if (!$this->queue) {
122
            $this->runCraftQueue();
123
        }
124
    }
125
126
    /**
0 ignored issues
show
Coding Style introduced by
Doc comment is empty
Loading history...
127
     *
128
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
129
    private function runCraftQueue()
0 ignored issues
show
Coding Style introduced by
Private method name "OptimizeController::runCraftQueue" must be prefixed with an underscore
Loading history...
130
    {
131
        // This might take a while
132
        App::maxPowerCaptain();
133
        $queue = Craft::$app->getQueue();
134
        if ($queue instanceof QueueInterface) {
135
            $queue->run();
136
        } elseif ($queue instanceof RedisQueue) {
137
            $queue->run(false);
138
        }
139
    }
140
}
141