Passed
Push — master ( e45b53...cf614d )
by Austin
01:43
created

Writer::lines()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * src/Writer.php
4
 *
5
 * @package     php-security-txt
6
 * @author      Austin Heap <[email protected]>
7
 * @version     v0.3.0
8
 */
9
10
declare(strict_types=1);
11
12
namespace AustinHeap\Security\Txt;
13
14
/**
15
 * Writer
16
 *
17
 * @link        https://github.com/austinheap/php-security-txt
18
 * @link        https://packagist.org/packages/austinheap/php-security-txt
19
 * @link        https://austinheap.github.io/php-security-txt/classes/AustinHeap.Security.Txt.Writer.html
20
 */
21
class Writer extends SecurityTxt
22
{
23
24
    /**
25
     * Internal lines cache.
26
     *
27
     * @var array
28
     */
29
    private $lines              = [];
30
31
    /**
32
     * Create a new Writer instance.
33
     *
34
     * @return \AustinHeap\Security\Txt\Writer
35
     */
36
    public function __construct()
37
    {
38
        return parent::__construct($this);
39
    }
40
41
    /**
42
     * Add a comment to the output buffer.
43
     *
44
     * @param  string $comment
45
     * @return \AustinHeap\Security\Txt\Writer
46
     */
47
    public function comment(string $comment = ''): Writer
48
    {
49
        $comment = trim($comment);
50
51
        if (!empty($comment))
52
            $comment = ' ' . $comment;
53
54
        return $this->line(trim('#' . $comment));
55
    }
56
57
    /**
58
     * Add a spacer to the output buffer.
59
     *
60
     * @return \AustinHeap\Security\Txt\Writer
61
     */
62
    public function spacer(): Writer
63
    {
64
        return $this->line('');
65
    }
66
67
    /**
68
     * Add multiple spacers to the output buffer.
69
     *
70
     * @return \AustinHeap\Security\Txt\Writer
71
     */
72
    public function spacers($count = 1): Writer
73
    {
74
        for ($x = 0; $x < $count; $x++)
75
            $this->spacer();
76
77
        return $this;
78
    }
79
80
    /**
81
     * Add a line.
82
     *
83
     * @param  string $line
84
     * @return \AustinHeap\Security\Txt\Writer
85
     */
86
    public function line(string $line): Writer
87
    {
88
        $this->lines[] = $line;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Add multiple lines.
95
     *
96
     * @param  array $lines
97
     * @return \AustinHeap\Security\Txt\Writer
98
     */
99
    public function lines(array $lines): Writer
100
    {
101
        foreach ($lines as $line)
102
            $this->line($line);
103
104
        return $this;
105
    }
106
107
    /**
108
     * Reset the output buffer.
109
     *
110
     * @return \AustinHeap\Security\Txt\Writer
111
     */
112
    public function reset(): Writer
113
    {
114
        $this->lines = [];
115
116
        return $this;
117
    }
118
119
    /**
120
     * Generate the data.
121
     *
122
     * @return \AustinHeap\Security\Txt\Writer
123
     */
124
    public function generate(): Writer
125
    {
126
        if ($this->debug)
127
            $time = microtime(true);
128
129
        if ($this->comments)
130
            $this->comment('Our security address');
131
132
        if (empty($this->contacts))
133
            throw new \Exception('One (or more) contacts must be defined.');
134
135
        foreach ($this->contacts as $contact)
136
            $this->line('Contact: ' . trim($contact));
137
138
        if (!empty($this->encryption)) {
139
            if ($this->comments)
140
                $this->spacer()
141
                     ->comment('Our PGP key');
142
143
            $this->line('Encryption: ' . trim($this->encryption));
144
        }
145
146
        if (!empty($this->disclosure)) {
147
            if ($this->comments)
148
                $this->spacer()
149
                     ->comment('Our disclosure policy');
150
151
            $this->line('Disclosure: ' . trim(ucfirst($this->disclosure)));
152
        }
153
154
        if (!empty($this->acknowledgement)) {
155
            if ($this->comments)
156
                $this->spacer()
157
                     ->comment('Our public acknowledgement');
158
159
            $this->line('Acknowledgement: ' . trim($this->acknowledgement));
160
        }
161
162
        if ($this->debug)
163
            $this->spacer()
164
                 ->comment()
165
                 ->comment(
166
                    'Generated by "' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? 'laravel' : 'php') . '-security-txt"' .
167
                    (defined('LARAVEL_SECURITY_TXT_VERSION') ? ' v' . LARAVEL_SECURITY_TXT_VERSION : (defined('PHP_SECURITY_TXT_VERSION') ? ' v' . PHP_SECURITY_TXT_VERSION : '')) .
0 ignored issues
show
Bug introduced by
The constant AustinHeap\Security\Txt\...EL_SECURITY_TXT_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
168
                    ' (https://github.com/austinheap/' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? 'laravel' : 'php') . '-security-txt' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? '/releases/tag/v' . LARAVEL_SECURITY_TXT_VERSION : (defined('PHP_SECURITY_TXT_VERSION') ? '/releases/tag/v' . PHP_SECURITY_TXT_VERSION : '')) . ')')
169
                 ->comment(
170
                    'using "php-security-txt"' . (defined('PHP_SECURITY_TXT_VERSION') ? ' v' . PHP_SECURITY_TXT_VERSION : '') .
171
                    ' (https://github.com/austinheap/php-security-txt' . (defined('PHP_SECURITY_TXT_VERSION') ? '/releases/tag/v' . PHP_SECURITY_TXT_VERSION : '') . ')')
172
                 ->comment('in ' . round((microtime(true) - $time) * 1000, 6) . ' seconds on ' . now() . '.')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $time does not seem to be defined for all execution paths leading up to this point.
Loading history...
Bug introduced by
The function now was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
                 ->comment('in ' . round((microtime(true) - $time) * 1000, 6) . ' seconds on ' . /** @scrutinizer ignore-call */ now() . '.')
Loading history...
173
                 ->comment()
174
                 ->spacer();
175
176
        $output = implode(PHP_EOL, $this->lines);
177
178
        return $this->setText($output);
179
    }
180
181
}
182