Passed
Pull Request — master (#653)
by Georges
02:05
created

IOConfigurationOptionTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 111
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setCacheFileExtension() 0 19 3
A getCacheFileExtension() 0 3 1
A setHtaccess() 0 5 1
A getSecurityKey() 0 3 1
A setSecureFileManipulation() 0 4 1
A setSecurityKey() 0 5 1
A isSecureFileManipulation() 0 3 1
A getHtaccess() 0 3 1
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> https://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
declare(strict_types=1);
15
namespace Phpfastcache\Config;
16
17
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
18
19
const SAFE_FILE_EXTENSIONS = 'txt|cache|db|pfc';
20
21
trait IOConfigurationOptionTrait
22
{
23
    /**
24
     * @var boolean
25
     */
26
    protected $secureFileManipulation = false;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $htaccess = true;
32
33
    /**
34
     * @var string
35
     */
36
    protected $securityKey = '';
37
38
    /**
39
     * @var string
40
     */
41
    protected $cacheFileExtension = 'txt';
42
43
    /**
44
     * @return string
45
     */
46
    public function getSecurityKey(): string
47
    {
48
        return $this->securityKey;
49
    }
50
51
    /**
52
     * @param string $securityKey
53
     * @return Config
54
     */
55
    public function setSecurityKey(string $securityKey): self
56
    {
57
        $this->securityKey = $securityKey;
58
59
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Phpfastcache\Config\IOConfigurationOptionTrait which is incompatible with the documented return type Phpfastcache\Config\Config.
Loading history...
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function getHtaccess(): bool
66
    {
67
        return $this->htaccess;
68
    }
69
70
    /**
71
     * @param bool $htaccess
72
     * @return Config
73
     */
74
    public function setHtaccess(bool $htaccess): ConfigurationOptionInterface
75
    {
76
        $this->htaccess = $htaccess;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    public function isSecureFileManipulation(): bool
85
    {
86
        return $this->secureFileManipulation;
87
    }
88
89
    /**
90
     * @param bool $secureFileManipulation
91
     * @return self
92
     */
93
    public function setSecureFileManipulation(bool $secureFileManipulation): self
94
    {
95
        $this->secureFileManipulation = $secureFileManipulation;
96
        return $this;
97
    }
98
99
100
    /**
101
     * @return string
102
     */
103
    public function getCacheFileExtension(): string
104
    {
105
        return $this->cacheFileExtension;
106
    }
107
108
    /**
109
     * @param string $cacheFileExtension
110
     * @return self
111
     * @throws PhpfastcacheInvalidConfigurationException
112
     */
113
    public function setCacheFileExtension(string $cacheFileExtension): self
114
    {
115
        /**
116
         * Feel free to propose your own one
117
         * by opening a pull request :)
118
         */
119
        $safeFileExtensions = \explode('|', SAFE_FILE_EXTENSIONS);
120
121
        if (\strpos($cacheFileExtension, '.') !== false) {
122
            throw new PhpfastcacheInvalidConfigurationException('cacheFileExtension cannot contain a dot "."');
123
        }
124
        if (!\in_array($cacheFileExtension, $safeFileExtensions, true)) {
125
            throw new PhpfastcacheInvalidConfigurationException(
126
                "Extension \"{$cacheFileExtension}\" is not safe, currently allowed extension names: " . \implode(', ', $safeFileExtensions)
127
            );
128
        }
129
130
        $this->cacheFileExtension = $cacheFileExtension;
131
        return $this;
132
    }
133
}