Forwarder::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * DirectAdmin API Client
5
 * (c) Omines Internetbureau B.V. - https://omines.nl/
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Omines\DirectAdmin\Objects\Email;
12
13
use Omines\DirectAdmin\Objects\Domain;
14
15
/**
16
 * Encapsulates an email forwarder.
17
 *
18
 * @author Niels Keurentjes <[email protected]>
19
 */
20
class Forwarder extends MailObject
21
{
22
    /** @var string[] */
23
    private $recipients;
24
25
    /**
26
     * Construct the object.
27
     *
28
     * @param string $prefix The part before the @ in the address
29
     * @param Domain $domain The containing domain
30
     * @param array|string $recipients Array or string containing the recipients
31
     */
32
    public function __construct($prefix, Domain $domain, $recipients)
33
    {
34
        parent::__construct($prefix, $domain);
35
        $this->recipients = is_string($recipients) ? array_map('trim', explode(',', $recipients)) : $recipients;
36
    }
37
38
    /**
39
     * Creates a new forwarder.
40
     *
41
     * @param Domain $domain
42
     * @param string $prefix
43
     * @param string|string[] $recipients
44
     * @return Forwarder
45
     */
46
    public static function create(Domain $domain, $prefix, $recipients)
47
    {
48
        $domain->invokePost('EMAIL_FORWARDERS', 'create', [
49
            'user' => $prefix,
50
            'email' => is_array($recipients) ? implode(',', $recipients) : $recipients,
51
        ]);
52
        return new self($prefix, $domain, $recipients);
53
    }
54
55
    /**
56
     * Deletes the forwarder.
57
     */
58
    public function delete()
59
    {
60
        $this->invokeDelete('EMAIL_FORWARDERS', 'select0');
61
    }
62
63
    /**
64
     * Returns a list of the recipients of this forwarder.
65
     *
66
     * @return string[]
67
     */
68
    public function getRecipients()
69
    {
70
        return $this->recipients;
71
    }
72
73
    /**
74
     * Returns the list of valid aliases for this account.
75
     *
76
     * @return string[]
77
     */
78
    public function getAliases()
79
    {
80
        return array_map(function ($domain) {
81
            return $this->getPrefix() . '@' . $domain;
82
        }, $this->getDomain()->getDomainNames());
83
    }
84
}
85