Passed
Pull Request — v1 (#150)
by
unknown
04:28
created

OptimizeController::actionQueueAssets()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
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\elements\Asset;
18
use craft\helpers\App;
19
use craft\helpers\FileHelper;
20
use craft\utilities\ClearCaches;
21
22
use yii\console\Controller;
23
24
/**
25
 * Optimize Command
26
 *
27
 * @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...
28
 * @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...
29
 * @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...
30
 */
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...
31
class OptimizeController extends Controller
32
{
33
    // Public Methods
34
    // =========================================================================
35
36
    /**
37
     * Create all of the OptimizedImages Field variants by creating all of the
38
     * responsive image variant transforms
39
     *
40
     * @param string|null $volumeHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
41
     *
42
     * @throws \yii\base\InvalidConfigException
43
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
44
    public function actionCreate($volumeHandle = null)
45
    {
46
        echo 'Creating optimized image variants'.PHP_EOL;
47
48
        if ($volumeHandle === null) {
49
            // Re-save all of the optimized image variants in all volumes
50
            ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets();
51
        } else {
52
            // Re-save all of the optimized image variants in a specific volume
53
            $volumes = Craft::$app->getVolumes();
54
            $volume = $volumes->getVolumeByHandle($volumeHandle);
55
            if ($volume) {
56
                /** @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...
57
                ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume);
58
            } else {
59
                echo 'Unknown Asset Volume handle: '.$volumeHandle.PHP_EOL;
60
            }
61
        }
62
        // This might take a while
63
        App::maxPowerCaptain();
64
        Craft::$app->getQueue()->run();
65
    }
66
67
    /**
68
     * Clear the Asset transform index cache tables, to force the re-creation of transformed images
69
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
70
    public function actionClear()
71
    {
72
        foreach (ClearCaches::cacheOptions() as $cacheOption) {
73
            if ($cacheOption['key'] !== 'transform-indexes') {
74
                continue;
75
            }
76
77
            $action = $cacheOption['action'];
78
79
            if (\is_string($action)) {
80
                try {
81
                    FileHelper::clearDirectory($action);
82
                } catch (\Throwable $e) {
83
                    Craft::warning("Could not clear the directory {$action}: ".$e->getMessage(), __METHOD__);
84
                }
85
            } elseif (isset($cacheOption['params'])) {
86
                \call_user_func_array($action, $cacheOption['params']);
87
            } else {
88
                $action();
89
            }
90
        }
91
    }
92
93
    /**
94
     * Individually queue all assets for optimizing. We run the query
95
     * in small batches so we never hit a memory limit.
96
     *
97
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
Coding Style introduced by
Missing @return tag in function comment
Loading history...
98
    public function actionQueueAssets()
99
    {
100
        $query = Asset::find();
101
        $batchSize = 100;
102
        $assetCount = $query->count();
103
        $numberOfBatches = ceil($assetCount / $batchSize);
104
        $query->limit($batchSize);
105
        $queue = Craft::$app->getQueue();
0 ignored issues
show
Unused Code introduced by
The assignment to $queue is dead and can be removed.
Loading history...
106
107
        for ($i = 0; $i < $numberOfBatches; ++$i) {
108
            $query->offset = $batchSize * $i;
109
110
            foreach ($query->all() as $asset) {
111
                ImageOptimize::$plugin->optimizedImages->resaveAsset($asset->id);
112
            }
113
        }
114
    }
115
}
116