Passed
Push — master ( 11bbe6...6fd8f2 )
by M. Mikkel
03:01
created

CacheFlag::addEventListeners()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 32
nc 1
nop 0
dl 0
loc 57
rs 8.1635
c 0
b 0
f 0

How to fix   Long Method   

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
 * Cache Flag plugin for Craft CMS 3.x
4
 *
5
 * Flag and clear template caches.
6
 *
7
 * @link      https://vaersaagod.no
8
 * @copyright Copyright (c) 2018 Mats Mikkel Rummelhoff
9
 */
10
11
namespace mmikkel\cacheflag;
12
13
use mmikkel\cacheflag\assetbundles\CpBundle;
14
use mmikkel\cacheflag\services\CacheFlagService;
15
use mmikkel\cacheflag\services\TemplateCachesService;
16
use mmikkel\cacheflag\twigextensions\Extension;
17
use mmikkel\cacheflag\variables\CpVariable;
18
19
use Craft;
20
use craft\base\Plugin;
21
use craft\elements\actions\SetStatus;
22
use craft\events\ElementEvent;
23
use craft\events\ElementActionEvent;
24
use craft\events\MergeElementsEvent;
25
use craft\events\MoveElementEvent;
26
use craft\events\PluginEvent;
27
use craft\services\Elements;
28
use craft\services\Plugins;
29
use craft\services\Structures;
30
use craft\web\UrlManager;
31
use craft\web\twig\variables\CraftVariable;
32
33
use yii\base\Event;
34
35
/**
36
 * Class CacheFlag
37
 *
38
 * @author    Mats Mikkel Rummelhoff
39
 * @package   CacheFlag
40
 * @since     1.0.0
41
 *
42
 * @property  CacheFlagService $cacheFlag
43
 * @property  TemplateCachesService $templateCaches
44
 */
45
class CacheFlag extends Plugin
46
{
47
    // Static Properties
48
    // =========================================================================
49
50
    /**
51
     * @var CacheFlag
52
     */
53
    public static $plugin;
54
55
    // Public Properties
56
    // =========================================================================
57
58
    /**
59
     * @var string
60
     */
61
    public $schemaVersion = '1.0.0';
62
63
    // Public Methods
64
    // =========================================================================
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function init()
70
    {
71
        parent::init();
72
        self::$plugin = $this;
73
74
        // Register services
75
        $this->setComponents([
76
            'cacheFlag' => CacheFlagService::class,
77
            'templateCaches' => TemplateCachesService::class,
78
        ]);
79
80
        Craft::$app->getView()->registerTwigExtension(new Extension());
81
82
        $this->addEventListeners();
83
        $this->registerResources();
84
85
        Craft::info(
86
            Craft::t(
87
                'cache-flag',
88
                '{name} plugin loaded',
89
                ['name' => $this->name]
90
            ),
91
            __METHOD__
92
        );
93
    }
94
95
    // Protected Methods
96
    // =========================================================================
97
98
    /**
99
     * Add event listeners for cache breaking
100
     */
101
    protected function addEventListeners()
102
    {
103
        Event::on(
104
            Elements::class,
105
            Elements::EVENT_AFTER_SAVE_ELEMENT,
106
            function (ElementEvent $event) {
107
                if ($event->element) {
108
                    CacheFlag::$plugin->cacheFlag->deleteFlaggedCachesByElement($event->element);
109
                }
110
            }
111
        );
112
113
        Event::on(
114
            Elements::class,
115
            Elements::EVENT_BEFORE_DELETE_ELEMENT,
116
            function (ElementEvent $event) {
117
                if ($event->element) {
118
                    CacheFlag::$plugin->cacheFlag->deleteFlaggedCachesByElement($event->element);
119
                }
120
            }
121
        );
122
123
        Event::on(
124
            Structures::class,
125
            Structures::EVENT_AFTER_MOVE_ELEMENT,
126
            function (MoveElementEvent $event) {
127
                if ($event->element) {
128
                    CacheFlag::$plugin->cacheFlag->deleteFlaggedCachesByElement($event->element);
129
                }
130
            }
131
        );
132
133
        Event::on(
134
            Elements::class,
135
            Elements::EVENT_AFTER_PERFORM_ACTION,
136
            function (ElementActionEvent $event) {
137
138
                /* @var ElementQueryInterface|null $criteria */
139
                $criteria = $event->criteria;
140
141
                if (!$criteria) {
142
                    return;
143
                }
144
145
                /* @var ElementActionInterface|null $action */
146
                $action = $event->action;
147
148
                if (!$action || !\in_array(\get_class($action), [
149
                        SetStatus::class,
150
                    ])) {
151
                    return;
152
                }
153
154
                $elements = $criteria->all();
155
156
                foreach ($elements as $element) {
157
                    CacheFlag::$plugin->cacheFlag->deleteFlaggedCachesByElement($element);
158
                }
159
            }
160
        );
161
    }
162
163
    /**
164
     *  Maybe register CP assets bundle and variable
165
     */
166
    protected function registerResources()
167
    {
168
169
        $request = Craft::$app->getRequest();
170
171
        if (!Craft::$app->getUser() || !$request->getIsCpRequest() || $request->getIsConsoleRequest() || ($request->getSegments()[0] ?? null) !== 'cache-flag') {
172
            return;
173
        }
174
175
        Event::on(
176
            Plugins::class,
177
            Plugins::EVENT_AFTER_LOAD_PLUGINS,
178
            function () {
179
                Craft::$app->getView()->registerAssetBundle(CpBundle::class);
180
            }
181
        );
182
183
        // Register CP variable
184
        Event::on(
185
            CraftVariable::class,
186
            CraftVariable::EVENT_INIT,
187
            function (Event $event) {
188
                /** @var CraftVariable $variable */
189
                $variable = $event->sender;
190
                $variable->set('cacheFlag', CpVariable::class);
191
            }
192
        );
193
    }
194
195
}
196