Issues (14)

src/Directives/Contact.php (5 issues)

1
<?php
2
/**
3
 * src/Directives/Contact.php
4
 *
5
 * @package     php-security-txt
6
 * @author      Austin Heap <[email protected]>
7
 * @version     v0.4.0
8
 */
9
10
declare(strict_types = 1);
11
12
namespace AustinHeap\Security\Txt\Directives;
13
14
use AustinHeap\Security\Txt\SecurityTxt;
15
use Exception;
16
17
/**
18
 * Contact
19
 *
20
 * @link        https://github.com/austinheap/php-security-txt
21
 * @link        https://packagist.org/packages/austinheap/php-security-txt
22
 * @link        https://austinheap.github.io/php-security-txt/classes/AustinHeap.Security.Txt.SecurityTxt.html
23
 * @link        https://securitytext.org/
24
 */
25
trait Contact
26
{
27
28
    /**
29
     * The security contact(s).
30
     *
31
     * @var array
32
     */
33
    protected $contacts = [];
34
35
    /**
36
     * Set the contacts.
37
     *
38
     * @param  array $contacts
39
     *
40
     * @return SecurityTxt
41
     */
42 2
    public function setContacts(array $contacts): SecurityTxt
43
    {
44 2
        if (!$this->validContacts($contacts, true)) {
45 1
            throw new Exception('Contacts array must contain well-formed e-mails and/or URLs.');
46
        }
47
48 1
        $this->contacts = $contacts;
49
50 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type AustinHeap\Security\Txt\Directives\Contact which includes types incompatible with the type-hinted return AustinHeap\Security\Txt\SecurityTxt.
Loading history...
51
    }
52
53
    /**
54
     * Get the contacts.
55
     *
56
     * @return array
57
     */
58 5
    public function getContacts(): array
59
    {
60 5
        return is_null($this->contacts) ? [] : array_keys($this->contacts);
0 ignored issues
show
The condition is_null($this->contacts) can never be true.
Loading history...
61
    }
62
63
    /**
64
     * Add a contact.
65
     *
66
     * @param  string $contact
67
     *
68
     * @return SecurityTxt
69
     */
70 15
    public function addContact(string $contact): SecurityTxt
71
    {
72 15
        return $this->addContacts([$contact]);
73
    }
74
75
    /**
76
     * Add contacts.
77
     *
78
     * @param  array $contacts
79
     *
80
     * @return SecurityTxt
81
     */
82 21
    public function addContacts(array $contacts): SecurityTxt
83
    {
84 21
        if (!$this->validContacts($contacts)) {
85 2
            throw new Exception('Contacts must be well-formed e-mails and/or URLs.');
86
        }
87
88 19
        foreach ($contacts as $contact) {
89 19
            $this->contacts[$contact] = true;
90
        }
91
92 19
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type AustinHeap\Security\Txt\Directives\Contact which includes types incompatible with the type-hinted return AustinHeap\Security\Txt\SecurityTxt.
Loading history...
93
    }
94
95
    /**
96
     * Validates a contact.
97
     *
98
     * @param string $contact
99
     *
100
     * @string string $contact
101
     * @return bool
102
     */
103 23
    public function validContact(string $contact): bool
104
    {
105 23
        return filter_var($contact, FILTER_VALIDATE_EMAIL) !== false ||
106 23
               filter_var($contact, FILTER_VALIDATE_URL) !== false;
107
    }
108
109
    /**
110
     * Validates an array of contacts.
111
     *
112
     * @param array $contacts
113
     * @param bool  $use_keys
114
     *
115
     * @return bool
116
     */
117 23
    public function validContacts(array $contacts, bool $use_keys = false): bool
118
    {
119 23
        if ($use_keys) {
120 2
            $contacts = array_keys($contacts);
121
        }
122
123 23
        foreach ($contacts as $contact) {
124 23
            if (!$this->validContact($contact)) {
125 23
                return false;
126
            }
127
        }
128
129 20
        return true;
130
    }
131
132
    /**
133
     * Remove a contact.
134
     *
135
     * @param  string $contact
136
     *
137
     * @return SecurityTxt
138
     */
139 2
    public function removeContact(string $contact): SecurityTxt
140
    {
141 2
        $this->removeContacts([$contact]);
142
143 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type AustinHeap\Security\Txt\Directives\Contact which includes types incompatible with the type-hinted return AustinHeap\Security\Txt\SecurityTxt.
Loading history...
144
    }
145
146
    /**
147
     * Remove contacts.
148
     *
149
     * @param  array $contacts
150
     *
151
     * @return SecurityTxt
152
     */
153 4
    public function removeContacts(array $contacts): SecurityTxt
154
    {
155 4
        if (!$this->hasContacts($contacts)) {
156 2
            throw new Exception('Cannot remove contacts that do not exist.');
157
        }
158
159 2
        foreach ($contacts as $contact) {
160 2
            unset($this->contacts[$contact]);
161
        }
162
163 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type AustinHeap\Security\Txt\Directives\Contact which includes types incompatible with the type-hinted return AustinHeap\Security\Txt\SecurityTxt.
Loading history...
164
    }
165
166
    /**
167
     * Determines if a contact exists.
168
     *
169
     * @param string $contact
170
     *
171
     * @return bool
172
     */
173 4
    public function hasContact(string $contact): bool
174
    {
175 4
        return array_key_exists($contact, $this->contacts);
176
    }
177
178
    /**
179
     * Determines if an array of contacts exists.
180
     *
181
     * @param array $contacts
182
     *
183
     * @return bool
184
     */
185 4
    public function hasContacts(array $contacts): bool
186
    {
187 4
        foreach ($contacts as $contact) {
188 4
            if (!$this->hasContact($contact)) {
189 4
                return false;
190
            }
191
        }
192
193 2
        return true;
194
    }
195
}
196