Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
54 / 54 |
Writer | |
100.00% |
1 / 1 |
|
100.00% |
8 / 8 |
35 | |
100.00% |
54 / 54 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
comment | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
spacer | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
spacers | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
line | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
lines | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
reset | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
execute | |
100.00% |
1 / 1 |
25 | |
100.00% |
38 / 38 |
<?php | |
/** | |
* src/Writer.php | |
* | |
* @package php-security-txt | |
* @author Austin Heap <me@austinheap.com> | |
* @version v0.4.0 | |
*/ | |
declare(strict_types = 1); | |
namespace AustinHeap\Security\Txt; | |
use Exception; | |
/** | |
* Writer | |
* | |
* @link https://github.com/austinheap/php-security-txt | |
* @link https://packagist.org/packages/austinheap/php-security-txt | |
* @link https://austinheap.github.io/php-security-txt/classes/AustinHeap.Security.Txt.Writer.html | |
* @link https://securitytext.org/ | |
*/ | |
class Writer extends SecurityTxt implements SecurityTxtInterface | |
{ | |
/** | |
* Internal lines cache. | |
* | |
* @var array | |
*/ | |
private $lines = []; | |
/** | |
* Create a new Writer instance. | |
* | |
* @return Writer|SecurityTxt | |
*/ | |
public function __construct() | |
{ | |
return parent::__construct($this); | |
} | |
/** | |
* Add a comment to the output buffer. | |
* | |
* @param string $comment | |
* | |
* @return Writer | |
*/ | |
public function comment(string $comment = ''): Writer | |
{ | |
$comment = trim($comment); | |
if (!empty($comment)) { | |
$comment = ' ' . $comment; | |
} | |
return $this->line(trim('#' . $comment)); | |
} | |
/** | |
* Add a spacer to the output buffer. | |
* | |
* @return Writer | |
*/ | |
public function spacer(): Writer | |
{ | |
return $this->line(''); | |
} | |
/** | |
* Add multiple spacers to the output buffer. | |
* | |
* @param int $count | |
* | |
* @return Writer | |
*/ | |
public function spacers(int $count = 1): Writer | |
{ | |
for ($x = 0; $x < $count; $x++) { | |
$this->spacer(); | |
} | |
return $this; | |
} | |
/** | |
* Add a line. | |
* | |
* @param string $line | |
* | |
* @return Writer | |
*/ | |
public function line(string $line): Writer | |
{ | |
$this->lines[] = $line; | |
return $this; | |
} | |
/** | |
* Add multiple lines. | |
* | |
* @param array $lines | |
* | |
* @return Writer | |
*/ | |
public function lines(array $lines): Writer | |
{ | |
foreach ($lines as $line) { | |
$this->line($line); | |
} | |
return $this; | |
} | |
/** | |
* Reset the output buffer. | |
* | |
* @param bool $test_case | |
* | |
* @return Writer | |
*/ | |
public function reset(bool $test_case = false): Writer | |
{ | |
$this->lines = []; | |
return $this; | |
} | |
/** | |
* Execute the SecurityTxtInterface. | |
* | |
* @param bool $test_case | |
* | |
* @return Writer | |
* @throws Exception | |
*/ | |
public function execute(bool $test_case = false): Writer | |
{ | |
$time = microtime(true); | |
if ($this->comments) { | |
$this->comment('Our security address'); | |
} | |
if (empty($this->contacts)) { | |
throw new Exception('One (or more) contacts must be defined.'); | |
} | |
foreach (array_keys($this->contacts) as $contact) { | |
$this->line('Contact: ' . trim($contact)); | |
} | |
if (!empty($this->encryption)) { | |
if ($this->comments) { | |
$this->spacer() | |
->comment('Our PGP key'); | |
} | |
$this->line('Encryption: ' . trim($this->encryption)); | |
} | |
if (!empty($this->disclosure)) { | |
if ($this->comments) { | |
$this->spacer() | |
->comment('Our disclosure policy'); | |
} | |
$this->line('Disclosure: ' . trim(ucfirst($this->disclosure))); | |
} | |
if (!empty($this->acknowledgement)) { | |
if ($this->comments) { | |
$this->spacer() | |
->comment('Our public acknowledgement'); | |
} | |
$this->line('Acknowledgement: ' . trim($this->acknowledgement)); | |
} | |
if ($this->debug) { | |
$this->spacer() | |
->comment() | |
->comment( | |
'Generated by "' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? 'laravel' : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? 'wordpress' : 'php')) . '-security-txt"' . | |
(defined('LARAVEL_SECURITY_TXT_VERSION') ? ' v' . LARAVEL_SECURITY_TXT_VERSION : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? ' v' . WORDPRESS_SECURITY_TXT_VERSION : (defined('PHP_SECURITY_TXT_VERSION') ? ' v' . PHP_SECURITY_TXT_VERSION : ''))) . | |
' (https://github.com/austinheap/' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? 'laravel' : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? 'wordpress' : 'php')) . '-security-txt' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? '/releases/tag/v' . LARAVEL_SECURITY_TXT_VERSION : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? '/releases/tag/v' . WORDPRESS_SECURITY_TXT_VERSION : (defined('PHP_SECURITY_TXT_VERSION') ? '/releases/tag/v' . PHP_SECURITY_TXT_VERSION : ''))) . ')'); | |
if (defined('LARAVEL_SECURITY_TXT_VERSION') || defined('WORDPRESS_SECURITY_TXT_VERSION')) { | |
$this->comment( | |
'using "php-security-txt"' . (defined('PHP_SECURITY_TXT_VERSION') ? ' v' . PHP_SECURITY_TXT_VERSION : '') . | |
' (https://github.com/austinheap/php-security-txt' . (defined('PHP_SECURITY_TXT_VERSION') ? '/releases/tag/v' . PHP_SECURITY_TXT_VERSION : '') . ')'); | |
} | |
$this->comment('in ' . round((microtime(true) - $time) * 1000, 6) . ' seconds on ' . date('c') . '.') | |
->comment() | |
->spacer(); | |
} | |
$output = implode(PHP_EOL, $this->lines); | |
return $this->setText($output); | |
} | |
} |