Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ $query = QueryBuilder::create('INBOX.Sent')
$emails = $client->getEmails($query);
```

Both `getEmails()` and `getEmailIds()` can take a third optional `boolean` argument. it can be set to false to indicate change of flag for fetched emails to seen.

```php
$query = QueryBuilder::create('INBOX.Sent')
->youngerThan(3600)
->flagSeen(true)

->getQuery();

$emails = $client->getEmails($query,'INBOX', false); // set to "false" fetched emails' state will be set to seen. "true" no change of state will occur. default is true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a space between $query,'INBOX'

Also could you put the comment at the start of the line above? That will be easier to read, else there will be a scrollbar on GitHub.

```

### Reading folders

```php
Expand Down
53 changes: 40 additions & 13 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class Client
*/
private $emailFactory;

/**
*
* @var boolean
*/
private $peek;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not store state into a property in this class. The Client class is a service, it should be stateless.

You should instead pass the $peek value in parameters of method calls.


public function __construct(Horde_Imap_Client_Socket $hordeClient, EmailFactory $emailFactory = null)
{
$this->hordeClient = $hordeClient;
Expand Down Expand Up @@ -63,24 +69,24 @@ public function getFolders() : array
return array_keys($this->hordeClient->listMailboxes('*'));
}

/**
/**
* Finds the emails matching the query. If $query is null, then it will fetch the emails in the inbox.
*
* @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true
* @return Email[]
*/
public function getEmails(Query $query = null) : array
public function getEmails(Query $query = null, bool $peek = true): array
{
$hordeQuery = new Horde_Imap_Client_Search_Query();

$query = $query ?: new Query;

if ($query->getYoungerThan() !== null) {
$hordeQuery->intervalSearch(
$query->getYoungerThan(),
Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are those lines changed? Those were formatted to follow PSR-2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDE

Copy link
Owner

@mnapoli mnapoli Nov 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert those changes?

$query->getYoungerThan(),
Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER
);
}

$this->setPeek($peek);
$this->setFlags($hordeQuery, $query);
return $this->searchAndFetch($query->getFolder(), $hordeQuery);
}
Expand All @@ -89,42 +95,50 @@ public function getEmails(Query $query = null) : array
* Finds the email Ids matching the query. If $query is null, then it will fetch the email Ids in the inbox.
*
* This method is obviously more efficient than getEmails() if you want to synchronize local mails.
*
*
* @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true
* @return string[]
*/
public function getEmailIds(Query $query = null) : array
public function getEmailIds(Query $query = null, bool $peek = true): array
{
$hordeQuery = new Horde_Imap_Client_Search_Query();

$query = $query ?: new Query;

if ($query->getYoungerThan() !== null) {
$hordeQuery->intervalSearch(
$query->getYoungerThan(),
Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER
$query->getYoungerThan(),
Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER
);
}

$this->setPeek($peek);
$this->setFlags($hordeQuery, $query);
return $this->search($query->getFolder(), $hordeQuery);
}

/**
* @param String $folder the folder to get email from
* @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true
* @return Email|null Returns null if the email was not found.
*/
public function getEmailFromId(string $id, string $folder = 'INBOX')
public function getEmailFromId(string $id, string $folder = 'INBOX', bool $peek = true)
{
$this->setPeek($peek);
$emails = $this->fetchEmails($folder, [$id]);

return (count($emails) > 0) ? $emails[0] : null;
}

/**
* @param string[] $ids
* @param String $folder the folder to get emails from
* @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true
* @return Email[]
*/
public function getEmailsFromId(array $ids, string $folder = 'INBOX') : array
public function getEmailsFromId(array $ids, string $folder = 'INBOX', bool $peek = true) : array
{
$this->setPeek($peek);
return $this->fetchEmails($folder, $ids);
}

Expand Down Expand Up @@ -178,7 +192,7 @@ private function fetchEmails(string $folder, array $ids) : array
$query = new Horde_Imap_Client_Fetch_Query();
$query->envelope();
$query->fullText([
'peek' => true,
'peek' => $this->getPeek(),
]);
$query->flags();

Expand Down Expand Up @@ -220,4 +234,17 @@ private function setFlags(Horde_Imap_Client_Search_Query $hordeQuery,Query $quer
}
}
}
/**
*
* @param boolean $peek
*/
public function setPeek($peek)
{
$this->peek = $peek;
}

public function getPeek()
{
return $this->peek;
}
}
16 changes: 13 additions & 3 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ class Query
* @var int
*/
private $youngerThan;
private $flags = [];

public function getFolder() : string
const FLAG_ANSWERED = 'ANSWERED';
const FLAG_DELETED = 'DELETED';
const FLAG_DRAFT = 'DRAFT';
const FLAG_FLAGED = 'FLAGGED';
const FLAG_RECENT = 'RECENT';
const FLAG_SEEN = 'SEEN';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this!


public function getFolder(): string
{
return $this->folder;
}
Expand All @@ -46,11 +54,13 @@ public function setYoungerThan(int $youngerThan)
$this->youngerThan = $youngerThan;
}

public function setFlags($key,$value){
public function setFlags($key,$value)
{
$this->flags[$key] = $value;
}

public function getFlags(){
public function getFlags()
{
return $this->flags;
}

Expand Down
18 changes: 12 additions & 6 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,38 @@ public function youngerThan(int $interval) : self
return $this;
}

public function flagAnswered($value) {
public function flagAnswered($value)
{
$this->query->setFlags(Query::FLAG_ANSWERED, $value);
return $this;
}

public function flagDeleted($value) {
public function flagDeleted($value)
{
$this->query->setFlags(Query::FLAG_DELETED, $value);
return $this;
}

public function flagDraft($value) {
public function flagDraft($value)
{
$this->query->setFlags(Query::FLAG_DRAFT, $value);
return $this;
}

public function flagFlaged($value) {
public function flagFlaged($value)
{
$this->query->setFlags(Query::FLAG_FLAGED, $value);
return $this;
}

public function flagRecent($value) {
public function flagRecent($value)
{
$this->query->setFlags(Query::FLAG_RECENT, $value);
return $this;
}

public function flagSeen($value) {
public function flagSeen($value)
{
$this->query->setFlags(Query::FLAG_SEEN, $value);
return $this;
}
Expand Down