OptimizeController::runCraftQueue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 0
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\Field;
15
use craft\helpers\App;
16
use craft\queue\QueueInterface;
17
use nystudio107\imageoptimize\ImageOptimize;
18
use yii\base\InvalidConfigException;
19
use yii\console\Controller;
20
use yii\queue\redis\Queue as RedisQueue;
21
22
/**
23
 * Optimize Command
24
 *
25
 * @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...
26
 * @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...
27
 * @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...
28
 */
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...
29
class OptimizeController extends Controller
30
{
31
    // Public Properties
32
    // =========================================================================
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @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...
36
     * @since 1.6.18
37
     */
38
    public bool $force = false;
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @var string|null Only re-save 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...
42
     * @since 1.6.18
43
     */
44
    public ?string $field = null;
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
47
     * @var bool Should the image generation simply be queued, rather than run immediately?
48
     */
49
    public bool $queue = false;
50
51
    // Public Methods
52
    // =========================================================================
53
54
    /**
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...
55
     * @inheritDoc
56
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
57
    public function options($actionID): array
58
    {
59
        $options = parent::options($actionID);
60
        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...
61
            'force',
62
            'field',
63
            'queue',
64
        ]);
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...
65
    }
66
67
    /**
68
     * Create all the OptimizedImages Field variants by creating all the responsive image variant transforms
69
     *
70
     * @param ?string $volumeHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
71
     *
72
     * @throws InvalidConfigException
73
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
74
    public function actionCreate(?string $volumeHandle = null): void
75
    {
76
        echo 'Creating optimized image variants' . PHP_EOL;
77
        if ($this->force) {
78
            echo 'Forcing optimized image variants creation via --force' . PHP_EOL;
79
        }
80
81
        $fieldId = null;
82
        if ($this->field !== null) {
83
            /** @var ?Field $field */
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
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
84
            $field = Craft::$app->getFields()->getFieldByHandle($this->field);
85
            $fieldId = $field?->id;
86
        }
87
        if ($volumeHandle === null) {
88
            // Re-save all the optimized image variants in all volumes
89
            ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets($fieldId, $this->force);
90
        } else {
91
            // Re-save all the optimized image variants in a specific volume
92
            $volumes = Craft::$app->getVolumes();
93
            $volume = $volumes->getVolumeByHandle($volumeHandle);
94
            if ($volume) {
95
                ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume, $fieldId, $this->force);
96
            } else {
97
                echo 'Unknown Asset Volume handle: ' . $volumeHandle . PHP_EOL;
98
            }
99
        }
100
        if (!$this->queue) {
101
            $this->runCraftQueue();
102
        }
103
    }
104
105
    /**
106
     * Create a single OptimizedImage for the passed in Asset ID
107
     *
108
     * @param ?int $id
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
109
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
110
    public function actionCreateAsset(?int $id = null): void
111
    {
112
        echo 'Creating optimized image variants' . PHP_EOL;
113
114
        if ($id === null) {
115
            echo 'No Asset ID specified' . PHP_EOL;
116
        } else {
117
            // Re-save a single Asset ID
118
            ImageOptimize::$plugin->optimizedImages->resaveAsset($id, $this->force);
119
        }
120
        if (!$this->queue) {
121
            $this->runCraftQueue();
122
        }
123
    }
124
125
    /**
0 ignored issues
show
Coding Style introduced by
Doc comment is empty
Loading history...
126
     *
127
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
128
    private function runCraftQueue(): void
0 ignored issues
show
Coding Style introduced by
Private method name "OptimizeController::runCraftQueue" must be prefixed with an underscore
Loading history...
129
    {
130
        // This might take a while
131
        App::maxPowerCaptain();
132
        $queue = Craft::$app->getQueue();
133
        if ($queue instanceof QueueInterface) {
134
            $queue->run();
135
        } elseif ($queue instanceof RedisQueue) {
136
            $queue->run(false);
137
        }
138
    }
139
}
140