Passed
Push — v1 ( 7859d5...83eaa9 )
by Andrew
22:02 queued 12:22
created

OptimizeController::runCraftQueue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
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 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 Methods
33
    // =========================================================================
34
35
    /**
36
     * Create all of the OptimizedImages Field variants by creating all of the
37
     * responsive image variant transforms
38
     *
39
     * @param string|null $volumeHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
40
     *
41
     * @throws \yii\base\InvalidConfigException
42
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
43
    public function actionCreate($volumeHandle = null)
44
    {
45
        echo 'Creating optimized image variants'.PHP_EOL;
46
47
        if ($volumeHandle === null) {
48
            // Re-save all of the optimized image variants in all volumes
49
            ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets();
50
        } else {
51
            // Re-save all of the optimized image variants in a specific volume
52
            $volumes = Craft::$app->getVolumes();
53
            $volume = $volumes->getVolumeByHandle($volumeHandle);
54
            if ($volume) {
55
                /** @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...
56
                ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume);
57
            } else {
58
                echo 'Unknown Asset Volume handle: '.$volumeHandle.PHP_EOL;
59
            }
60
        }
61
        $this->runCraftQueue();
62
    }
63
64
    /**
65
     * Create a single OptimizedImage for the passed in Asset ID
66
     *
67
     * @param int|null $id
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    public function actionCreateAsset($id = null)
70
    {
71
        echo 'Creating optimized image variants'.PHP_EOL;
72
73
        if ($id === null) {
74
            echo 'No Asset ID specified'.PHP_EOL;
75
        } else {
76
            // Re-save a single Asset ID
77
            ImageOptimize::$plugin->optimizedImages->resaveAsset($id);
78
        }
79
        $this->runCraftQueue();
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Doc comment is empty
Loading history...
83
     *
84
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
85
    private function runCraftQueue()
0 ignored issues
show
Coding Style introduced by
Private method name "OptimizeController::runCraftQueue" must be prefixed with an underscore
Loading history...
86
    {
87
        // This might take a while
88
        App::maxPowerCaptain();
89
        $queue = Craft::$app->getQueue();
90
        if ($queue instanceof QueueInterface) {
0 ignored issues
show
introduced by
$queue is always a sub-type of craft\queue\QueueInterface.
Loading history...
91
            $queue->run();
92
        } elseif ($queue instanceof RedisQueue) {
93
            $queue->run(false);
94
        }
95
    }
96
}
97