-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathMailProvider.php
More file actions
174 lines (159 loc) Β· 4.48 KB
/
MailProvider.php
File metadata and controls
174 lines (159 loc) Β· 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Provider;
use OCA\Mail\Account;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Service\AccountService;
use OCP\IL10N;
use OCP\Mail\Provider\Address as MailAddress;
use OCP\Mail\Provider\IProvider;
use OCP\Mail\Provider\IService;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
class MailProvider implements IProvider {
public function __construct(
protected ContainerInterface $container,
protected AccountService $accountService,
protected LoggerInterface $logger,
protected IL10N $l10n,
) {
}
/**
* Arbitrary unique text string identifying this provider
*
* @since 4.0.0
*
* @return string id of this provider (e.g. UUID or 'IMAP/SMTP' or anything else)
*/
#[\Override]
public function id(): string {
return 'mail-application';
}
/**
* Localized human friendly name of this provider
*
* @since 4.0.0
*
* @return string label/name of this provider (e.g. Plain Old IMAP/SMTP)
*/
#[\Override]
public function label(): string {
return $this->l10n->t('Mail Application');
}
/**
* Determine if any services are configured for a specific user
*
* @since 4.0.0
*
* @param string $userId system user id
*
* @return bool true if any services are configure for the user
*/
#[\Override]
public function hasServices(string $userId): bool {
return ($this->listServices($userId) !== []);
}
/**
* Retrieve collection of services for a specific user
*
* @since 4.0.0
*
* @param string $userId system user id
*
* @return array<string,IService> collection of service id and object ['1' => IServiceObject]
*/
#[\Override]
public function listServices(string $userId): array {
// retrieve service(s) details from data store
$accounts = $this->accountService->findByUserId($userId);
// construct temporary collection
$services = [];
// add services to collection
foreach ($accounts as $entry) {
$services[(string)$entry->getId()] = $this->serviceFromAccount($userId, $entry);
}
// return list of services for user
return $services;
}
/**
* Retrieve a service with a specific id
*
* @since 4.0.0
*
* @param string $userId system user id
* @param string $serviceId mail account id
*
* @return IService|null returns service object or null if none found
*
*/
#[\Override]
public function findServiceById(string $userId, string $serviceId): ?IService {
// determine if a valid user and service id was submitted
if (empty($userId) && !ctype_digit($serviceId)) {
return null;
}
// retrieve service details from data store
try {
$account = $this->accountService->find($userId, (int)$serviceId);
} catch (ClientException $e) {
$this->logger->error('Error occurred while retrieving mail account details', [ 'exception' => $e ]);
return null;
}
// return mail service object
return $this->serviceFromAccount($userId, $account);
}
/**
* Retrieve a service for a specific mail address
*
* @since 4.0.0
*
* @param string $userId system user id
* @param string $address mail address (e.g. test@example.com)
*
* @return IService|null returns service object or null if none found
*/
#[\Override]
public function findServiceByAddress(string $userId, string $address): ?IService {
// retrieve service details from data store
$accounts = $this->accountService->findByUserIdAndAddress($userId, $address);
// evaluate if service details where found
if ($accounts !== []) {
// return mail service object
return $this->serviceFromAccount($userId, $accounts[0]);
}
return null;
}
/**
* Construct a new fresh service object
*
* @since 4.0.0
*
* @return IService fresh service object
*/
#[\Override]
public function initiateService(): IService {
return new MailService($this->container);
}
/**
* Construct a service object from a mail account
*
* @since 4.0.0
*
* @param string $userId system user id
* @param Account $account mail account
*
* @return IService service object
*/
protected function serviceFromAccount(string $userId, Account $account): IService {
// extract values
$serviceId = (string)$account->getId();
$serviceLabel = $account->getName();
$serviceAddress = new MailAddress($account->getEmail(), $account->getName());
// return mail service object
return new MailService($this->container, $userId, $serviceId, $serviceLabel, $serviceAddress);
}
}