Passed
Push — develop ( 447e9d...67bf5f )
by Andrew
08:59
created

ResaveOptimizedImages::execute()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 35
c 4
b 0
f 0
dl 0
loc 56
rs 6.6166
cc 13
nc 12
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * ImageOptimize 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) 2017 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\jobs;
12
13
use nystudio107\imageoptimize\fields\OptimizedImages as OptimizedImagesField;
14
15
use Craft;
16
use craft\base\ElementInterface;
17
use craft\base\Field;
18
use craft\console\Application as ConsoleApplication;
19
use craft\elements\Asset;
20
use craft\elements\db\ElementQuery;
21
use craft\helpers\App;
22
use craft\queue\BaseJob;
23
24
use nystudio107\imageoptimize\ImageOptimize;
25
use yii\base\Exception;
26
27
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
28
 * @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...
29
 * @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...
30
 * @since     1.4.8
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...
31
 */
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...
32
class ResaveOptimizedImages extends BaseJob
33
{
34
    // Properties
35
    // =========================================================================
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @var array|null The element criteria that determines which elements should be resaved
39
     */
40
    public $criteria;
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @var int|null The id of the field to resave images for, or null for all images
44
     */
45
    public $fieldId;
46
47
    // Public Methods
48
    // =========================================================================
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $queue should have a doc-comment as per coding-style.
Loading history...
51
     * @inheritdoc
52
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
53
    public function execute($queue)
54
    {
55
        // Let's save ourselves some trouble and just clear all the caches for this element class
56
        Craft::$app->getTemplateCaches()->deleteCachesByElementType(Asset::class);
0 ignored issues
show
Deprecated Code introduced by
The function craft\services\TemplateC...teCachesByElementType() has been deprecated: in 3.5.0. Use [[\craft\services\Elements::invalidateCachesForElementType()]] instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

56
        /** @scrutinizer ignore-deprecated */ Craft::$app->getTemplateCaches()->deleteCachesByElementType(Asset::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
57
58
        // Now find the affected element IDs
59
        /** @var ElementQuery $query */
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...
60
        $query = Asset::find();
61
        if (!empty($this->criteria)) {
62
            Craft::configure($query, $this->criteria);
63
        }
64
        $query
65
            ->offset(null)
66
            ->limit(null)
67
            ->orderBy(null);
68
69
        if (Craft::$app instanceof ConsoleApplication) {
70
            echo $this->description.PHP_EOL;
71
        }
72
73
        $totalElements = $query->count();
74
        $currentElement = 0;
75
76
        /** @var ElementInterface $element */
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...
77
        foreach ($query->each() as $element) {
78
            // Find each OptimizedImages field and process it
79
            $layout = $element->getFieldLayout();
80
            if ($layout !== null) {
81
                $fields = $layout->getFields();
82
                /** @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
Tag value for @var tag indented incorrectly; expected 1 spaces but found 2
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
83
                foreach ($fields as $field) {
84
                    if ($field instanceof OptimizedImagesField && $element instanceof Asset) {
85
                        if ($this->fieldId === null || $field->id == $this->fieldId) {
86
                            if (Craft::$app instanceof ConsoleApplication) {
87
                                echo $currentElement . '/' . $totalElements
88
                                    . ' - processing asset: ' . $element->title
89
                                    . ' from field: ' . $field->name . PHP_EOL;
90
                            }
91
                            try {
92
                                ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($field, $element);
93
                            } catch (Exception $e) {
94
                                Craft::error($e->getMessage(), __METHOD__);
95
                                if (Craft::$app instanceof ConsoleApplication) {
96
                                    echo '[error]: '
97
                                        . $e->getMessage()
98
                                        . ' while processing '
99
                                        . $currentElement . '/' . $totalElements
100
                                        . ' - processing asset: ' . $element->title
101
                                        . ' from field: ' . $field->name . PHP_EOL;
102
                                }
103
                            }
104
                        }
105
                    }
106
                }
107
            }
108
            $this->setProgress($queue, $currentElement++ / $totalElements);
109
        }
110
    }
111
112
    // Protected Methods
113
    // =========================================================================
114
115
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
116
     * @inheritdoc
117
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
118
    protected function defaultDescription(): string
119
    {
120
        return Craft::t('app', 'Resaving {class} elements', [
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...
121
            'class' => App::humanizeClass(Asset::class),
122
        ]);
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...
123
    }
124
}
125