DefaultController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
eloc 64
c 2
b 0
f 0
dl 0
loc 133
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B actionSaveFlags() 0 53 9
A actionInvalidateAllFlaggedCaches() 0 19 3
A actionInvalidateFlaggedCachesByFlags() 0 33 4
1
<?php
2
3
namespace mmikkel\cacheflag\controllers;
4
5
use mmikkel\cacheflag\CacheFlag;
6
7
use Craft;
8
use craft\web\Controller;
9
10
use yii\web\BadRequestHttpException;
11
use yii\web\ForbiddenHttpException;
12
13
/**
14
 * @author    Mats Mikkel Rummelhoff
15
 * @package   CacheFlag
16
 * @since     1.0.0
17
 */
18
class DefaultController extends Controller
19
{
20
21
    /** @var array|bool|int */
22
    public array|bool|int $allowAnonymous = self::ALLOW_ANONYMOUS_NEVER;
23
24
    /**
25
     * @return \yii\web\Response
26
     * @throws BadRequestHttpException
27
     * @throws \yii\base\InvalidConfigException
28
     */
29
    public function actionSaveFlags(): \yii\web\Response
30
    {
31
        $this->requirePostRequest();
32
        $this->requireAcceptsJson();
33
34
        if (!Craft::$app->getConfig()->getGeneral()->allowAdminChanges) {
35
            throw new ForbiddenHttpException('Administrative changes are disallowed in this environment.');
36
        }
37
38
        $params = Craft::$app->getRequest()->getBodyParams();
39
        $cacheFlags = $params['cacheflags'] ?? null;
40
41
        $error = null;
42
43
        foreach ($cacheFlags as $source => $flags) {
44
45
            $sourceArray = explode(':', $source);
46
            $sourceColumn = $sourceArray[0] ?? null;
47
            $sourceId = $sourceArray[1] ?? null;
48
49
            if (!$sourceColumn || !$sourceId) {
50
                continue;
51
            }
52
53
            $flags = preg_replace('/\s+/', '', $flags);
54
55
            try {
56
                if (!$flags) {
57
                    CacheFlag::getInstance()->cacheFlag->deleteFlagsBySource($sourceColumn, $sourceId);
58
                    continue;
59
                }
60
                CacheFlag::getInstance()->cacheFlag->saveFlags($flags, $sourceColumn, $sourceId);
61
            } catch (\Throwable $e) {
62
                $error = $e->getMessage();
63
            }
64
65
            if ($error) {
66
                break;
67
            }
68
69
        }
70
71
        if ($error) {
72
            return $this->asJson([
73
                'success' => false,
74
                'message' => $error,
75
            ]);
76
        }
77
78
        return $this->asJson([
79
            'success' => true,
80
            'message' => Craft::t('cache-flag', 'Cache flags saved'),
81
            'flags' => CacheFlag::getInstance()->cacheFlag->getAllFlags(),
82
        ]);
83
84
    }
85
86
    /**
87
     * @return \yii\web\Response
88
     * @throws BadRequestHttpException
89
     * @throws \yii\base\InvalidConfigException
90
     */
91
    public function actionInvalidateFlaggedCachesByFlags()
92
    {
93
        $this->requirePostRequest();
94
        $this->requireAcceptsJson();
95
96
        $params = Craft::$app->getRequest()->getBodyParams();
97
        $flags = $params['flags'] ?? null;
98
99
        if (!$flags) {
100
            return $this->asJson([
101
                'success' => false,
102
                'message' => Craft::t('cache-flag', 'No flags to invalidate caches for'),
103
            ]);
104
        }
105
106
        $error = null;
107
108
        try {
109
            CacheFlag::getInstance()->cacheFlag->invalidateFlaggedCachesByFlags($flags);
110
        } catch (\Throwable $e) {
111
            $error = $e->getMessage();
112
        }
113
114
        if ($error) {
115
            return $this->asJson([
116
                'success' => false,
117
                'message' => $error,
118
            ]);
119
        }
120
121
        return $this->asJson([
122
            'success' => true,
123
            'message' => Craft::t('cache-flag', 'Flagged caches invalidated'),
124
        ]);
125
    }
126
127
    /**
128
     * @return \yii\web\Response
129
     * @throws BadRequestHttpException
130
     * @throws \craft\errors\MissingComponentException
131
     */
132
    public function actionInvalidateAllFlaggedCaches()
133
    {
134
        $this->requirePostRequest();
135
136
        $error = null;
137
138
        try {
139
            CacheFlag::getInstance()->cacheFlag->invalidateAllFlaggedCaches();
140
        } catch (\Throwable $e) {
141
            $error = $e;
142
        }
143
144
        if ($error) {
145
            Craft::$app->getSession()->setError($error);
146
        } else {
147
            Craft::$app->getSession()->setNotice(Craft::t('cache-flag', 'All flagged caches invalidated'));
148
        }
149
150
        return $this->redirectToPostedUrl();
151
    }
152
153
}
154