Passed
Push — master ( 3b17b1...bb2309 )
by M. Mikkel
05:38
created

CachesController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 41
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A actionInvalidate() 0 27 5
A init() 0 4 1
1
<?php
2
3
4
namespace mmikkel\cacheflag\controllers;
5
6
use Craft;
7
use craft\web\Controller;
8
9
use mmikkel\cacheflag\CacheFlag;
10
11
/**
12
 * Class CachesController
13
 * @package mmikkel\cacheflag\controllers
14
 * @since 1.2.0
15
 */
16
class CachesController extends Controller
17
{
18
19
    public function init(): void
20
    {
21
        $this->allowAnonymous = true;
22
        parent::init();
23
    }
24
25
    /**
26
     * Invalidate all flagged caches, or caches flagged with one or several particular flags
27
     *
28
     * @return bool
29
     */
30
    public function actionInvalidate(): bool
31
    {
32
        $flags = Craft::$app->getRequest()->getParam('flags', []);
33
34
        if (\is_string($flags)) {
35
            $flags = \preg_replace('/\s+/', '', $flags);
36
            $flags = \array_filter(\explode(',', $flags));
37
        } else if (\is_array($flags)) {
38
            $flags = \array_reduce($flags, function (array $carry, string $flag) {
39
                $flag = \preg_replace('/\s+/', '', $flag);
40
                if (\strlen($flag)) {
41
                    $carry[] = $flag;
42
                }
43
                return $carry;
44
            }, []);
45
        }
46
47
        /** @var array $flags */
48
        if (empty($flags)) {
49
            CacheFlag::getInstance()->cacheFlag->invalidateAllFlaggedCaches();
50
            return true;
51
        }
52
53
        $flags = \array_unique($flags);
54
55
        CacheFlag::getInstance()->cacheFlag->invalidateFlaggedCachesByFlags($flags);
56
        return true;
57
    }
58
}
59