Passed
Pull Request — master (#1)
by Austin
02:31 queued 42s
created

SecurityTxtHelper::disable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/**
3
 * src/SecurityTxtHelper.php.
4
 *
5
 * @author      Austin Heap <[email protected]>
6
 *
7
 * @version     v0.3.0
8
 */
9
declare(strict_types=1);
10
11
namespace AustinHeap\Security\Txt;
12
13
/**
14
 * SecurityTxtHelper.
15
 *
16
 * @link        https://github.com/austinheap/laravel-security-txt
17
 * @link        https://packagist.org/packages/austinheap/laravel-security-txt
18
 * @link        https://austinheap.github.io/laravel-security-txt/classes/AustinHeap.Security.Txt.SecurityTxtHelper.html
19
 */
20
class SecurityTxtHelper
21
{
22
    /**
23
     * Internal version number.
24
     *
25
     * @var string
26
     */
27
    const VERSION = '0.3.0';
28
29
    /**
30
     * Internal SecurityTxt object.
31
     *
32
     * @var \AustinHeap\Security\Txt\Writer
33
     */
34
    protected $writer = null;
35
36
    /**
37
     * Internal array of log entries.
38
     *
39
     * @var array
40
     */
41
    protected $logEntries = [];
42
43
    /**
44
     * Enable built-in cache.
45
     *
46
     * @var bool
47
     */
48
    protected $cache = false;
49
50
    /**
51
     * Minutes to cache output.
52
     *
53
     * @var int
54
     */
55
    protected $cacheTime = null;
56
57
    /**
58
     * Cache key to use.
59
     *
60
     * @var string
61
     */
62
    protected $cacheKey = 'cache:AustinHeap\Security\Txt\SecurityTxt';
63
64
    /**
65
     * Create a new SecurityTxtHelper instance.
66
     *
67
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
68
     */
69
    public function __construct()
70
    {
71
        $this->writer = new Writer();
72
73
        $keys = [
74
            'security-txt.enabled'          => ['validator' => 'is_bool',       'setter' => 'setEnabled',           'self' => true],
75
            'security-txt.debug'            => ['validator' => 'is_bool',       'setter' => 'setDebug'],
76
            'security-txt.cache'            => ['validator' => 'is_bool',       'setter' => 'setCache',             'self' => true],
77
            'security-txt.cache-time'       => ['validator' => 'is_numeric',    'setter' => 'setCacheTime',         'self' => true],
78
            'security-txt.cache-key'        => ['validator' => 'is_string',     'setter' => 'setCacheKey',          'self' => true],
79
            'security-txt.comments'         => ['validator' => 'is_bool',       'setter' => 'setComments'],
80
            'security-txt.contacts'         => ['validator' => 'is_array',      'setter' => 'setContacts'],
81
            'security-txt.encryption'       => ['validator' => 'is_string',     'setter' => 'setEncryption'],
82
            'security-txt.disclosure'       => ['validator' => 'is_string',     'setter' => 'setDisclosure'],
83
            'security-txt.acknowledgement'  => ['validator' => 'is_string',     'setter' => 'setAcknowledgement'],
84
        ];
85
86
        foreach ($keys as $key => $mapping) {
87
            if (empty(config($key, null))) {
88
                $this->addLogEntry('"'.__CLASS__.'" cannot process empty value for key "'.$key.'".', 'notice');
89
                continue;
90
            }
91
92
            if (!$mapping['validator'](config($key))) {
93
                $this->addLogEntry('"'.__CLASS__.'" cannot find mapping "validator" method named "'.$mapping['setter'].'".', 'warning');
94
                continue;
95
            }
96
97
            if (array_key_exists('self', $mapping) &&
98
                is_bool($mapping['self']) &&
99
                $mapping['self'] === true) {
100 View Code Duplication
                if (!method_exists($this, $mapping['setter'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
                    $this->addLogEntry('"'.__CLASS__.'" cannot find mapping "setter" method on object "'.get_class($this).'" named "'.$mapping['setter'].'".', 'error');
102
                    continue;
103
                }
104
105
                $this->{$mapping['setter']}(config($key));
106
            } else {
107 View Code Duplication
                if (!method_exists($this->writer, $mapping['setter'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
                    $this->addLogEntry('"'.__CLASS__.'" cannot find mapping "setter" method on object "'.get_class($this->writer).'" named "'.$mapping['setter'].'".', 'error');
109
                    continue;
110
                }
111
112
                $this->writer->{$mapping['setter']}(config($key));
113
            }
114
        }
115
116
        return $this;
117
    }
118
119
    /**
120
     * Add log entry.
121
     *
122
     * @param string $text
123
     * @param string $level
124
     *
125
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
126
     */
127
    public function addLogEntry(string $text, string $level = 'info'): SecurityTxtHelper
128
    {
129
        \Log::$level($text);
130
131
        $this->logEntries[] = ['text' => $text, 'level' => $level];
132
133
        return $this;
134
    }
135
136
    /**
137
     * Fetches the raw text of the document.
138
     *
139
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
140
     */
141
    public function fetch(): string
142
    {
143
        if ($this->cache) {
144
            $text = cache($this->cacheKey, null);
145
146
            if (!is_null($text)) {
147
                return $text;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $text could return the type Illuminate\Cache\CacheMa...tracts\Cache\Repository which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
148
            }
149
        }
150
151
        $text = $this->writer
152
                     ->generate()
153
                     ->getText();
154
155
        if ($this->writer->getComments()) {
156
            $text .= '# Cache is '.($this->cache ? 'enabled with key "'.$this->cacheKey.'"' : 'disabled').'.'.PHP_EOL.
157
                     '#'.PHP_EOL;
158
        }
159
160
        if ($this->cache) {
161
            cache([$this->cacheKey => $text], now()->addMinutes($this->cacheTime));
162
        }
163
164
        return empty($text) ? '' : $text;
0 ignored issues
show
Bug Best Practice introduced by
The expression return empty($text) ? '' : $text returns the type string which is incompatible with the documented return type AustinHeap\Security\Txt\SecurityTxtHelper.
Loading history...
165
    }
166
167
    /**
168
     * Enable the enabled flag.
169
     *
170
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
171
     */
172
    public function enable(): SecurityTxtHelper
173
    {
174
        return $this->setEnabled(true);
175
    }
176
177
    /**
178
     * Disable the enabled flag.
179
     *
180
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
181
     */
182
    public function disable(): SecurityTxtHelper
183
    {
184
        return $this->setEnabled(false);
185
    }
186
187
    /**
188
     * Set the enabled flag.
189
     *
190
     * @param bool $enabled
191
     *
192
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
193
     */
194
    public function setEnabled(bool $enabled): SecurityTxtHelper
195
    {
196
        $this->enabled = $enabled;
0 ignored issues
show
Bug Best Practice introduced by
The property enabled does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
197
198
        return $this;
199
    }
200
201
    /**
202
     * Get the enabled flag.
203
     *
204
     * @return bool
205
     */
206
    public function getEnabled(): bool
207
    {
208
        return $this->enabled;
209
    }
210
211
    /**
212
     * Enable the cache flag.
213
     *
214
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
215
     */
216
    public function enableCache(): SecurityTxtHelper
217
    {
218
        return $this->setCache(true);
219
    }
220
221
    /**
222
     * Disable the cache flag.
223
     *
224
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
225
     */
226
    public function disableCache(): SecurityTxtHelper
227
    {
228
        return $this->setCache(false);
229
    }
230
231
    /**
232
     * Set the cache flag.
233
     *
234
     * @param bool $cache
235
     *
236
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
237
     */
238
    public function setCache(bool $cache): SecurityTxtHelper
239
    {
240
        $this->cache = $cache;
241
242
        return $this;
243
    }
244
245
    /**
246
     * Get the cache flag.
247
     *
248
     * @return bool
249
     */
250
    public function getCache(): bool
251
    {
252
        return $this->cache;
253
    }
254
255
    /**
256
     * Set the cache key.
257
     *
258
     * @param string $cacheKey
259
     *
260
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
261
     */
262
    public function setCacheKey(string $cacheKey): SecurityTxtHelper
263
    {
264
        $this->cacheKey = $cacheKey;
265
266
        return $this;
267
    }
268
269
    /**
270
     * Get the cache key.
271
     *
272
     * @return string
273
     */
274
    public function getCacheKey(): string
275
    {
276
        return $this->cacheKey;
277
    }
278
279
    /**
280
     * Set the cache time.
281
     *
282
     * @param int $cacheTime
283
     *
284
     * @return \AustinHeap\Security\Txt\SecurityTxtHelper
285
     */
286
    public function setCacheTime(int $cacheTime): SecurityTxtHelper
287
    {
288
        $this->cacheTime = $cacheTime;
289
290
        return $this;
291
    }
292
293
    /**
294
     * Get the cache time.
295
     *
296
     * @return int
297
     */
298
    public function getCacheTime(): int
299
    {
300
        return $this->cacheTime;
301
    }
302
}
303