Passed
Push — develop ( f3999c...434aaf )
by Andrew
03:43
created

FastcgiCacheBust::shouldBustCache()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * FastCGI Cache Bust plugin for Craft CMS 3.x
4
 *
5
 * Bust the Nginx FastCGI Cache when entries are saved or created.
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\fastcgicachebust;
12
13
use Craft;
14
use craft\base\Element;
15
use craft\base\Plugin;
16
use craft\events\DeleteTemplateCachesEvent;
17
use craft\events\ElementEvent;
18
use craft\events\RegisterCacheOptionsEvent;
19
use craft\services\Elements;
20
use craft\services\TemplateCaches;
21
use craft\utilities\ClearCaches;
22
use nystudio107\fastcgicachebust\models\Settings;
23
use nystudio107\fastcgicachebust\services\ServicesTrait;
24
use yii\base\Event;
25
use yii\base\Exception;
26
use function get_class;
27
28
/**
29
 * Class FastcgiCacheBust
30
 *
31
 * @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...
32
 * @package   FastcgiCacheBust
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
33
 * @since     1.0.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...
34
 */
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...
35
class FastcgiCacheBust extends Plugin
36
{
37
    // Traits
38
    // =========================================================================
39
40
    use ServicesTrait;
41
42
    // Static Properties
43
    // =========================================================================
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @var FastcgiCacheBust
47
     */
48
    public static $plugin;
49
50
    // Public Properties
51
    // =========================================================================
52
53
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
54
     * @var string
55
     */
56
    public $schemaVersion = '1.0.0';
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @var bool
60
     */
61
    public $hasCpSection = false;
62
63
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
64
     * @var bool
65
     */
66
    public $hasCpSettings = true;
67
68
    // Public Methods
69
    // =========================================================================
70
71
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
72
     * @inheritdoc
73
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
74
    public function init()
75
    {
76
        parent::init();
77
        self::$plugin = $this;
78
79
        // Handler: Elements::EVENT_AFTER_SAVE_ELEMENT
80
        Event::on(
81
            Elements::class,
82
            Elements::EVENT_AFTER_SAVE_ELEMENT,
83
            function (ElementEvent $event) {
84
                Craft::debug(
85
                    'Elements::EVENT_AFTER_SAVE_ELEMENT',
86
                    __METHOD__
87
                );
88
                /** @var Element $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...
89
                $element = $event->element;
90
                // Only bust the cache if it's not certain excluded element types
91
                if (self::$plugin->cache->shouldBustCache($element)) {
92
                    Craft::debug(
93
                        'Cache busted due to saving: ' . get_class($element) . ' - ' . $element->title,
94
                        __METHOD__
95
                    );
96
                    FastcgiCacheBust::$plugin->cache->clearAll();
97
                }
98
            }
99
        );
100
        // Handler: TemplateCaches::EVENT_AFTER_DELETE_CACHES
101
        Event::on(
102
            TemplateCaches::class,
103
            TemplateCaches::EVENT_AFTER_DELETE_CACHES,
0 ignored issues
show
Deprecated Code introduced by
The constant craft\services\TemplateC...ENT_AFTER_DELETE_CACHES has been deprecated: in 3.5.0 ( Ignorable by Annotation )

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

103
            /** @scrutinizer ignore-deprecated */ TemplateCaches::EVENT_AFTER_DELETE_CACHES,

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

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

Loading history...
104
            static function (DeleteTemplateCachesEvent $event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

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

104
            static function (/** @scrutinizer ignore-unused */ DeleteTemplateCachesEvent $event) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
                FastcgiCacheBust::$plugin->cache->clearAll();
106
            }
107
        );
108
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
109
        Event::on(
110
            ClearCaches::class,
111
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
112
            static function (RegisterCacheOptionsEvent $event) {
113
                $event->options[] = [
114
                    'key' => 'fastcgi-cache-bust',
115
                    'label' => Craft::t('fastcgi-cache-bust', 'FastCGI Cache'),
116
                    'action' => function () {
117
                        FastcgiCacheBust::$plugin->cache->clearAll();
118
                    },
119
                ];
120
            }
121
        );
122
123
        Craft::info(
124
            Craft::t(
125
                'fastcgi-cache-bust',
126
                '{name} plugin loaded',
127
                ['name' => $this->name]
128
            ),
129
            __METHOD__
130
        );
131
    }
132
133
    // Protected Methods
134
    // =========================================================================
135
136
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
137
     * @inheritdoc
138
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
139
    protected function createSettingsModel()
140
    {
141
        return new Settings();
142
    }
143
144
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
145
     * @inheritdoc
146
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
147
    protected function settingsHtml(): string
148
    {
149
        try {
150
            return Craft::$app->view->renderTemplate(
151
                'fastcgi-cache-bust/settings',
152
                [
153
                    'settings' => $this->getSettings(),
154
                ]
155
            );
156
        } catch (Exception $e) {
157
            Craft::error($e->getMessage(), __METHOD__);
158
            return '';
159
        }
160
    }
161
}
162