Passed
Push — develop ( 5d0193...74fc79 )
by Andrew
05:52 queued 03:14
created

Cache   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B clearAll() 0 21 5
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
8
 * @copyright Copyright (c) 2017 nystudio107
9
 */
10
11
namespace nystudio107\fastcgicachebust\services;
12
13
use nystudio107\fastcgicachebust\FastcgiCacheBust;
14
use nystudio107\fastcgicachebust\models\Settings;
15
16
use Craft;
17
use craft\base\Component;
18
use craft\helpers\FileHelper;
19
use yii\base\ErrorException;
20
21
/**
22
 * @author    nystudio107
23
 * @package   FastcgiCacheBust
24
 * @since     1.0.0
25
 */
26
class Cache extends Component
27
{
28
    // Public Methods
29
    // =========================================================================
30
31
    /**
32
     * Clears the entirety of the FastCGI Cache
33
     */
34
    public function clearAll()
35
    {
36
        /**
37
         * @var Settings settings
38
         */
39
        $settings = FastcgiCacheBust::$plugin->getSettings();
40
        if (!empty($settings) && !empty($settings->cachePath)) {
41
            $cacheDirs = explode(',', $settings->cachePath);
42
            foreach ($cacheDirs as $cacheDir) {
43
                $cacheDir = trim($cacheDir);
44
                try {
45
                    FileHelper::clearDirectory($cacheDir);
46
                } catch (ErrorException $e) {
47
                    Craft::error($e->getMessage(), __METHOD__);
48
                }
49
                Craft::info(
50
                    Craft::t(
51
                        'fastcgi-cache-bust',
52
                        'FastCGI Cache busted: `'.$cacheDir
53
                    ),
54
                    __METHOD__
55
                );
56
            }
57
        }
58
    }
59
}
60