Passed
Push — develop ( f3999c...434aaf )
by Andrew
03:43
created

Cache::shouldBustCache()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
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\services;
12
13
use Craft;
14
use craft\base\Component;
15
use craft\base\Element;
16
use craft\elements\Entry;
17
use craft\helpers\FileHelper;
18
use nystudio107\fastcgicachebust\FastcgiCacheBust;
19
use nystudio107\fastcgicachebust\models\Settings;
20
use yii\base\ErrorException;
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @author    nystudio107
0 ignored issues
show
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...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
24
 * @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...
25
 * @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...
26
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
27
class Cache extends Component
28
{
29
    // Public Methods
30
    // =========================================================================
31
32
    /**
33
     * Clears the entirety of the FastCGI Cache
34
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
35
    public function clearAll()
36
    {
37
        /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
         * @var Settings $settings
39
         */
40
        $settings = FastcgiCacheBust::$plugin->getSettings();
41
        if ($settings !== null && !empty($settings->cachePath)) {
42
            $cacheDirs = explode(',', $settings->cachePath);
43
            foreach ($cacheDirs as $cacheDir) {
44
                $cacheDir = Craft::parseEnv($cacheDir);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

44
                $cacheDir = /** @scrutinizer ignore-deprecated */ Craft::parseEnv($cacheDir);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
45
                $cacheDir = trim($cacheDir);
0 ignored issues
show
Bug introduced by
It seems like $cacheDir can also be of type boolean; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
                $cacheDir = trim(/** @scrutinizer ignore-type */ $cacheDir);
Loading history...
46
                try {
47
                    FileHelper::clearDirectory($cacheDir);
48
                } catch (ErrorException $e) {
49
                    Craft::error($e->getMessage(), __METHOD__);
50
                }
51
                Craft::info(
52
                    Craft::t(
53
                        'fastcgi-cache-bust',
54
                        'FastCGI Cache busted: `' . $cacheDir
55
                    ),
56
                    __METHOD__
57
                );
58
            }
59
        }
60
    }
61
62
    /**
63
     * Determine whether the cache should be busted or not based on the $element
64
     *
65
     * @param Element $element
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
66
     *
67
     * @return bool
68
     */
69
    public function shouldBustCache(Element $element): bool
70
    {
71
        $bustCache = true;
72
        // Only bust the cache if the element is ENABLED or LIVE
73
        if (($element->getStatus() !== Element::STATUS_ENABLED)
74
            && ($element->getStatus() !== Entry::STATUS_LIVE)
75
        ) {
76
            $bustCache = false;
77
        }
78
79
        return $bustCache;
80
    }
81
}
82