Mailbox::create()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 4
nc 1
nop 5
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 a full mailbox with POP/IMAP/webmail access.
17
 *
18
 * @author Niels Keurentjes <[email protected]>
19
 */
20
class Mailbox extends MailObject
21
{
22
    const CACHE_DATA = 'mailbox';
23
24
    /**
25
     * Construct the object.
26
     *
27
     * @param string $prefix The part before the @ in the address
28
     * @param Domain $domain The containing domain
29
     * @param string|array|null $config URL encoded config string as returned by CMD_API_POP
30
     */
31
    public function __construct($prefix, Domain $domain, $config = null)
32
    {
33
        parent::__construct($prefix, $domain);
34
        if (isset($config)) {
35
            $this->setCache(self::CACHE_DATA, is_string($config) ? \GuzzleHttp\Psr7\parse_query($config) : $config);
36
        }
37
    }
38
39
    /**
40
     * Creates a new mailbox.
41
     *
42
     * @param Domain $domain Domain to add the account to
43
     * @param string $prefix Prefix for the account
44
     * @param string $password Password for the account
45
     * @param int|null $quota Quota in megabytes, or zero/null for unlimited
46
     * @param int|null $sendLimit Send limit, or 0 for unlimited, or null for system default
47
     * @return Mailbox The created mailbox
48
     */
49
    public static function create(Domain $domain, $prefix, $password, $quota = null, $sendLimit = null)
50
    {
51
        $domain->invokePost('POP', 'create', [
52
            'user' => $prefix,
53
            'passwd' => $password,
54
            'passwd2' => $password,
55
            'quota' => intval($quota) ?: 0,
56
            'limit' => isset($sendLimit) ? (intval($sendLimit) ?: 0) : null,
57
        ]);
58
        return new self($prefix, $domain);
59
    }
60
61
    /**
62
     * Deletes the mailbox.
63
     */
64
    public function delete()
65
    {
66
        $this->invokeDelete('POP', 'user');
67
    }
68
69
    /**
70
     * Reset the password for this mailbox.
71
     *
72
     * @param string $newPassword
73
     */
74
    public function setPassword($newPassword)
75
    {
76
        $this->invokePost('POP', 'modify', [
77
            'user' => $this->getPrefix(),
78
            'passwd' => $newPassword,
79
            'passwd2' => $newPassword,
80
        ], false);
81
    }
82
83
    /**
84
     * Returns the disk quota in megabytes.
85
     *
86
     * @return float|null
87
     */
88
    public function getDiskLimit()
89
    {
90
        return floatval($this->getData('quota')) ?: null;
91
    }
92
93
    /**
94
     * Returns the disk usage in megabytes.
95
     *
96
     * @return float
97
     */
98
    public function getDiskUsage()
99
    {
100
        return floatval($this->getData('usage'));
101
    }
102
103
    /**
104
     * Return the amount of mails sent in the current period.
105
     *
106
     * @return int
107
     */
108
    public function getMailsSent()
109
    {
110
        return intval($this->getData('sent'));
111
    }
112
113
    /**
114
     * Return the maximum number of mails that can be send each day
115
     *
116
     * @return int
117
     */
118
    public function getMailLimit()
119
    {
120
        return intval($this->getData('limit'));
121
    }
122
123
    /**
124
     * Returns if the mailbox is suspended or not
125
     *
126
     * @return bool
127
     */
128
    public function getMailSuspended()
129
    {
130
        return( strcasecmp($this->getData('suspended'),"yes" ) == 0 );
131
    }
132
133
134
    /**
135
     * Cache wrapper to keep mailbox stats up to date.
136
     *
137
     * @param string $key
138
     * @return mixed
139
     */
140
    protected function getData($key)
141
    {
142
        return $this->getCacheItem(self::CACHE_DATA, $key, function () {
143
            $result = $this->getContext()->invokeApiGet('POP', [
144
                'domain' => $this->getDomainName(),
145
                'action' => 'full_list',
146
            ]);
147
148
            return \GuzzleHttp\Psr7\parse_query($result[$this->getPrefix()]);
149
        });
150
    }
151
}
152