CachingPreferences   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 86
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dontCacheInDevelopment() 0 7 2
A dontCacheIf() 0 7 2
A dontCacheInProduction() 0 7 2
A dontCacheInEnv() 0 7 2
A onlyCacheInProduction() 0 7 2
A dontCacheInLocal() 0 3 1
1
<?php
2
3
namespace Sfneal\ViewModels\Traits;
4
5
use Sfneal\Helpers\Laravel\AppInfo;
6
7
trait CachingPreferences
8
{
9
    /**
10
     * @var bool Determine if caching has been disabled.
11
     */
12
    protected bool $cachingDisabled = false;
13
14
    /**
15
     * Disable ViewModel render caching if the conditional evaluates as true.
16
     *
17
     * @param  bool  $conditional
18
     * @return $this
19
     */
20
    public function dontCacheIf(bool $conditional): self
21
    {
22
        if ($conditional) {
23
            $this->cachingDisabled = true;
24
        }
25
26
        return $this;
27
    }
28
29
    /**
30
     * Disable ViewModel render caching if the app environment is 'development'.
31
     *
32
     * @return $this
33
     */
34
    public function dontCacheInDevelopment(): self
35
    {
36
        if (AppInfo::isEnvDevelopment()) {
37
            $this->cachingDisabled = true;
38
        }
39
40
        return $this;
41
    }
42
43
    /**
44
     * Disable ViewModel render caching if the app environment is 'production'.
45
     *
46
     * @return $this
47
     */
48
    public function dontCacheInProduction(): self
49
    {
50
        if (AppInfo::isEnvProduction()) {
51
            $this->cachingDisabled = true;
52
        }
53
54
        return $this;
55
    }
56
57
    /**
58
     * Disable ViewModel render caching if the app environment is 'local'.
59
     *
60
     * @return $this
61
     */
62
    public function dontCacheInLocal(): self
63
    {
64
        return $this->dontCacheInEnv('local');
65
    }
66
67
    /**
68
     * Disable ViewModel render caching if the app environment is 'local'.
69
     *
70
     * @return $this
71
     */
72
    public function dontCacheInEnv(string $env): self
73
    {
74
        if (AppInfo::isEnv($env)) {
75
            $this->cachingDisabled = true;
76
        }
77
78
        return $this;
79
    }
80
81
    /**
82
     * Disable caching if the environment is not 'production'.
83
     *
84
     * @return $this
85
     */
86
    public function onlyCacheInProduction(): self
87
    {
88
        if (! AppInfo::isEnvProduction()) {
89
            $this->cachingDisabled = true;
90
        }
91
92
        return $this;
93
    }
94
}
95