Completed
Pull Request — master (#5)
by Chauncey
07:26 queued 05:26
created

CacheConfig::prefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\App\Config;
4
5
use InvalidArgumentException;
6
7
// From 'charcoal-config'
8
use Charcoal\Config\AbstractConfig;
9
10
/**
11
 * Cache Configuration
12
 */
13
class CacheConfig extends AbstractConfig
14
{
15
    /**
16
     * Human-readable intervals in seconds.
17
     */
18
    const HOUR_IN_SECONDS = 3600;
19
    const DAY_IN_SECONDS  = 86400;
20
    const WEEK_IN_SECONDS = 604800;
21
22
    /**
23
     * Cache driver(s).
24
     *
25
     * @var array
26
     */
27
    private $types = [ 'memory' ];
28
29
    /**
30
     * Time-to-live in seconds.
31
     *
32
     * @var integer
33
     */
34
    private $defaultTtl = self::WEEK_IN_SECONDS;
35
36
    /**
37
     * Cache namespace.
38
     *
39
     * @var string
40
     */
41
    private $prefix = 'charcoal';
42
43
    /**
44
     * @return array
45
     */
46
    public function defaults()
47
    {
48
        return [
49
            'types'       => [ 'memory' ],
50
            'default_ttl' => self::WEEK_IN_SECONDS,
51
            'prefix'      => 'charcoal'
52
        ];
53
    }
54
55
    /**
56
     * Set the types (drivers) of cache.
57
     *
58
     * The first cache actually available on the system will be the one used for caching.
59
     *
60
     * @param string[] $types The types of cache to try using, in order of priority.
61
     * @return CacheConfig Chainable
62
     */
63
    public function setTypes(array $types)
64
    {
65
        $this->types = [];
66
        foreach ($types as $type) {
67
            $this->addType($type);
68
        }
69
        return $this;
70
    }
71
72
    /**
73
     * Get the valid types (drivers).
74
     *
75
     * @return array
76
     */
77
    public function validTypes()
78
    {
79
        return [
80
            'apc',
81
            'file',
82
            'db',
83
            'memcache',
84
            'memory',
85
            'noop',
86
            'redis'
87
        ];
88
    }
89
90
    /**
91
     * @param string $type The cache type.
92
     * @throws InvalidArgumentException If the type is not a string.
93
     * @return CacheConfig Chainable
94
     */
95
    public function addType($type)
96
    {
97
        if (!in_array($type, $this->validTypes())) {
98
            throw new InvalidArgumentException(
99
                sprintf('Invalid cache type: "%s"', $type)
100
            );
101
        }
102
        $this->types[] = $type;
103
        return $this;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function types()
110
    {
111
        return $this->types;
112
    }
113
114
    /**
115
     * @param integer $ttl The time-to-live, in seconds.
116
     * @throws InvalidArgumentException If the TTL argument is not numeric.
117
     * @return CacheConfig Chainable
118
     */
119
    public function setDefaultTtl($ttl)
120
    {
121
        if (!is_numeric($ttl)) {
122
            throw new InvalidArgumentException(
123
                'TTL must be an integer (seconds).'
124
            );
125
        }
126
        $this->defaultTtl = (int)$ttl;
127
        return $this;
128
    }
129
130
    /**
131
     * @return integer
132
     */
133
    public function defaultTtl()
134
    {
135
        return $this->defaultTtl;
136
    }
137
138
    /**
139
     * @param string $prefix The cache prefix (or namespace).
140
     * @throws InvalidArgumentException If the prefix is not a string.
141
     * @return CacheConfig Chainable
142
     */
143
    public function setPrefix($prefix)
144
    {
145
        if (!is_string($prefix)) {
146
            throw new InvalidArgumentException(
147
                'Prefix must be a string.'
148
            );
149
        }
150
        $this->prefix = $prefix;
151
        return $this;
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public function prefix()
158
    {
159
        return $this->prefix;
160
    }
161
}
162