Passed
Push — develop ( a52af0...f3999c )
by Andrew
03:45
created

FastcgiCacheBust::createSettingsModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

104
            /** @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...
105
            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

105
            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...
106
                FastcgiCacheBust::$plugin->cache->clearAll();
107
            }
108
        );
109
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
110
        Event::on(
111
            ClearCaches::class,
112
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
113
            static function (RegisterCacheOptionsEvent $event) {
114
                $event->options[] = [
115
                    'key' => 'fastcgi-cache-bust',
116
                    'label' => Craft::t('fastcgi-cache-bust', 'FastCGI Cache'),
117
                    'action' => function () {
118
                        FastcgiCacheBust::$plugin->cache->clearAll();
119
                    },
120
                ];
121
            }
122
        );
123
124
        Craft::info(
125
            Craft::t(
126
                'fastcgi-cache-bust',
127
                '{name} plugin loaded',
128
                ['name' => $this->name]
129
            ),
130
            __METHOD__
131
        );
132
    }
133
134
    // Protected Methods
135
    // =========================================================================
136
137
    /**
138
     * Determine whether the cache should be busted or not based on the $element
139
     *
140
     * @param Element $element
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
141
     *
142
     * @return bool
143
     */
144
    protected function shouldBustCache(Element $element): bool
145
    {
146
        $bustCache = true;
147
        // Only bust the cache if the element is ENABLED or LIVE
148
        if (($element->getStatus() !== Element::STATUS_ENABLED)
149
            && ($element->getStatus() !== Entry::STATUS_LIVE)
150
        ) {
151
            $bustCache = false;
152
        }
153
154
        return $bustCache;
155
    }
156
157
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
158
     * @inheritdoc
159
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
160
    protected function createSettingsModel()
161
    {
162
        return new Settings();
163
    }
164
165
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
166
     * @inheritdoc
167
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
168
    protected function settingsHtml(): string
169
    {
170
        try {
171
            return Craft::$app->view->renderTemplate(
172
                'fastcgi-cache-bust/settings',
173
                [
174
                    'settings' => $this->getSettings(),
175
                ]
176
            );
177
        } catch (Exception $e) {
178
            Craft::error($e->getMessage(), __METHOD__);
179
            return '';
180
        }
181
    }
182
}
183