Passed
Push — develop ( 766fa0...f06543 )
by Andrew
08:54
created

OptimizeController::actionCreate()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 16
c 4
b 0
f 0
dl 0
loc 26
rs 9.4222
cc 5
nc 9
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
62
        $fieldId = null;
63
        if ($this->field !== null) {
64
            $field = Craft::$app->getFields()->getFieldByHandle($this->field);
65
            if ($field !== null) {
66
                $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...
67
            }
68
        }
69
        if ($volumeHandle === null) {
70
            // Re-save all of the optimized image variants in all volumes
71
            ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets($fieldId, $this->force);
72
        } else {
73
            // Re-save all of the optimized image variants in a specific volume
74
            $volumes = Craft::$app->getVolumes();
75
            $volume = $volumes->getVolumeByHandle($volumeHandle);
76
            if ($volume) {
77
                /** @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...
78
                ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume, $fieldId, $this->force);
79
            } else {
80
                echo 'Unknown Asset Volume handle: '.$volumeHandle.PHP_EOL;
81
            }
82
        }
83
        $this->runCraftQueue();
84
    }
85
86
    /**
87
     * Create a single OptimizedImage for the passed in Asset ID
88
     *
89
     * @param int|null $id
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
90
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
91
    public function actionCreateAsset($id = null)
92
    {
93
        echo 'Creating optimized image variants'.PHP_EOL;
94
95
        if ($id === null) {
96
            echo 'No Asset ID specified'.PHP_EOL;
97
        } else {
98
            // Re-save a single Asset ID
99
            ImageOptimize::$plugin->optimizedImages->resaveAsset($id, $this->force);
100
        }
101
        $this->runCraftQueue();
102
    }
103
104
    /**
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...
105
     * @inheritdoc
106
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
107
    public function options($actionId): array
108
    {
109
        $options = parent::options($actionId);
110
        $options[] = 'force';
111
        $options[] = 'field';
112
113
        return $options;
114
    }
115
116
    /**
0 ignored issues
show
Coding Style introduced by
Doc comment is empty
Loading history...
117
     *
118
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
119
    private function runCraftQueue()
0 ignored issues
show
Coding Style introduced by
Private method name "OptimizeController::runCraftQueue" must be prefixed with an underscore
Loading history...
120
    {
121
        // This might take a while
122
        App::maxPowerCaptain();
123
        $queue = Craft::$app->getQueue();
124
        if ($queue instanceof QueueInterface) {
0 ignored issues
show
introduced by
$queue is always a sub-type of craft\queue\QueueInterface.
Loading history...
125
            $queue->run();
126
        } elseif ($queue instanceof RedisQueue) {
127
            $queue->run(false);
128
        }
129
    }
130
}
131