Passed
Push — develop ( 3d637f...91097a )
by Andrew
06:40 queued 01:03
created

FastcgiCacheBust::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
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\Cache as CacheService;
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
 *
36
 * @property  CacheService cache
0 ignored issues
show
Coding Style introduced by
Tag value for @property tag indented incorrectly; expected 1 spaces but found 2
Loading history...
37
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
38
class FastcgiCacheBust extends Plugin
39
{
40
    // Static Properties
41
    // =========================================================================
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
     * @var FastcgiCacheBust
45
     */
46
    public static $plugin;
47
48
    // Public Properties
49
    // =========================================================================
50
51
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
52
     * @var string
53
     */
54
    public $schemaVersion = '1.0.0';
55
56
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
57
     * @var bool
58
     */
59
    public $hasCpSection = false;
60
61
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
62
     * @var bool
63
     */
64
    public $hasCpSettings = true;
65
66
    // Public Methods
67
    // =========================================================================
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $id should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
70
     * @inheritdoc
71
     */
72
    public function __construct($id, $parent = null, array $config = [])
73
    {
74
        $config['components'] = [
75
            'cache' => CacheService::class,
76
        ];
77
78
        parent::__construct($id, $parent, $config);
79
    }
80
81
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
82
     * @inheritdoc
83
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
84
    public function init()
85
    {
86
        parent::init();
87
        self::$plugin = $this;
88
89
        // Handler: Elements::EVENT_AFTER_SAVE_ELEMENT
90
        Event::on(
91
            Elements::class,
92
            Elements::EVENT_AFTER_SAVE_ELEMENT,
93
            function (ElementEvent $event) {
94
                Craft::debug(
95
                    'Elements::EVENT_AFTER_SAVE_ELEMENT',
96
                    __METHOD__
97
                );
98
                /** @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
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
99
                $element = $event->element;
100
                // Only bust the cache if it's not certain excluded element types
101
                if ($this->shouldBustCache($element)) {
102
                    Craft::debug(
103
                        'Cache busted due to saving: ' . get_class($element) . ' - ' . $element->title,
104
                        __METHOD__
105
                    );
106
                    FastcgiCacheBust::$plugin->cache->clearAll();
107
                }
108
            }
109
        );
110
        // Handler: TemplateCaches::EVENT_AFTER_DELETE_CACHES
111
        Event::on(
112
            TemplateCaches::class,
113
            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

113
            /** @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...
114
            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

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