Passed
Push — v1 ( 47c11d...06f464 )
by Andrew
18:55 queued 09:51
created

OptimizeController::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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