Passed
Push — 5.x ( 64edde...71aac6 )
by Jeroen
12:33 queued 14s
created

SecurityTxt   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
c 1
b 0
f 1
dl 0
loc 46
ccs 0
cts 28
cp 0
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 36 5
1
<?php
2
3
namespace Elgg\Controllers;
4
5
use Elgg\Exceptions\Http\EntityNotFoundException;
6
use Elgg\Http\ResponseBuilder;
7
use Elgg\Values;
8
9
/**
10
 * Controller for the /security.txt resource
11
 *
12
 * @since 5.1
13
 */
14
class SecurityTxt {
15
	
16
	/**
17
	 * Handle the request
18
	 *
19
	 * @param \Elgg\Request $request current request
20
	 *
21
	 * @return ResponseBuilder
22
	 * @throws EntityNotFoundException
23
	 */
24
	public function __invoke(\Elgg\Request $request): ResponseBuilder {
25
		$contact = elgg_get_config('security_txt_contact');
26
		$expires = elgg_get_config('security_txt_expires');
27
		if (empty($contact) || empty($expires)) {
28
			throw new EntityNotFoundException();
29
		}
30
		
31
		$lines = [
32
			"Contact: {$contact}",
33
			'Expires: ' . Values::normalizeTime($expires)->format(DATE_ATOM),
34
		];
35
		
36
		$fields = [
37
			'encryption' => 'Encryption',
38
			'acknowledgments' => 'Acknowledgments',
39
			'language' => 'Preferred-Languages',
40
			'canonical' => 'Canonical',
41
			'policy' => 'Policy',
42
			'hiring' => 'Hiring',
43
			'csaf' => 'CSAF',
44
		];
45
		foreach ($fields as $name => $output) {
46
			$value = elgg_get_config("security_txt_{$name}");
47
			if (empty($value)) {
48
				continue;
49
			}
50
			
51
			$lines[] = "{$output}: {$value}";
52
		}
53
		
54
		$response = elgg_ok_response(implode(PHP_EOL, $lines));
55
		$response->setHeaders([
56
			'Content-Type' => 'text/plain; charset=utf-8',
57
		]);
58
		
59
		return $response;
60
	}
61
}
62