-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathProvider.php
More file actions
71 lines (61 loc) · 1.75 KB
/
Copy pathProvider.php
File metadata and controls
71 lines (61 loc) · 1.75 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\IMAP\Search;
use Horde_Imap_Client_Exception;
use Horde_Imap_Client_Search_Query;
use OCA\Mail\Account;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Service\Search\SearchQuery;
use function array_reduce;
class Provider {
/** @var IMAPClientFactory */
private $clientFactory;
public function __construct(IMAPClientFactory $clientFactory) {
$this->clientFactory = $clientFactory;
}
/**
* @return int[]
* @throws ServiceException
*/
public function findMatches(Account $account,
Mailbox $mailbox,
SearchQuery $searchQuery): array {
$client = $this->clientFactory->getClient($account);
try {
$fetchResult = $client->search(
$mailbox->getName(),
$this->convertMailQueryToHordeQuery($searchQuery)
);
} catch (Horde_Imap_Client_Exception $e) {
throw new ServiceException('Could not get message IDs: ' . $e->getMessage(), 0, $e);
} finally {
$client->logout();
}
return $fetchResult['match']->ids;
}
/**
* @param SearchQuery $searchQuery
*
* @todo possible optimization: filter flags here as well as it might speed up IMAP search
*
* @return Horde_Imap_Client_Search_Query
*/
private function convertMailQueryToHordeQuery(SearchQuery $searchQuery): Horde_Imap_Client_Search_Query {
$query = new Horde_Imap_Client_Search_Query();
$query->charset('UTF-8');
return array_reduce(
$searchQuery->getBodies(),
static function (Horde_Imap_Client_Search_Query $query, string $textToken) {
$query->text($textToken, true);
return $query;
},
$query
);
}
}