diff --git a/src/AdminResources/Emails.php b/src/AdminResources/Emails.php index c984f97..c93391b 100644 --- a/src/AdminResources/Emails.php +++ b/src/AdminResources/Emails.php @@ -7,12 +7,16 @@ class Emails extends MsGraphAdmin { + private ?bool $delta = null; + private string $userId = ''; private string $top = '100'; private string $skip = '0'; + private string $search = ''; + private string $subject = ''; private string $body = ''; @@ -106,6 +110,13 @@ public function attachments(array $attachments): static return $this; } + public function delta(?bool $delta = true): static + { + $this->delta = $delta; + + return $this; + } + /** * @throws Exception */ @@ -117,6 +128,11 @@ public function get(array $params = []): array $top = request('top', $this->top); $skip = request('skip', $this->skip); + $search = request('search', $this->search); + + if (filled($search) && $this->delta) { + throw new Exception('Search is not supported in delta queries.'); + } if ($params == []) { $params = http_build_query([ @@ -130,7 +146,8 @@ public function get(array $params = []): array } // get messages from folderId - $emails = MsGraphAdmin::get('users/'.$this->userId.'/messages?'.$params); + $messages = $this->delta ? 'messages/delta' : 'messages'; + $emails = MsGraphAdmin::get('users/'.$this->userId.'/'.$messages.'?'.$params); if (isset($emails->error)) { throw new Exception("Graph API Error, code: {$emails->error->code}, Message: {$emails->error->message}"); diff --git a/src/MsGraph.php b/src/MsGraph.php index 2094237..4b7bb8f 100755 --- a/src/MsGraph.php +++ b/src/MsGraph.php @@ -341,7 +341,7 @@ protected function guzzle(string $type, string $request, array $data = [], array } catch (ClientException $e) { throw new Exception($e->getMessage()); - //return json_decode(($e->getResponse()->getBody()->getContents())); + // return json_decode(($e->getResponse()->getBody()->getContents())); } catch (Exception $e) { throw new Exception($e->getMessage()); } diff --git a/src/Resources/Emails/Emails.php b/src/Resources/Emails/Emails.php index 70d4b22..10627f5 100644 --- a/src/Resources/Emails/Emails.php +++ b/src/Resources/Emails/Emails.php @@ -8,15 +8,14 @@ class Emails extends MsGraph { - public function folders(): Folders - { - return new Folders; - } + private ?bool $delta = null; private string $top = ''; private string $skip = ''; + private string $search = ''; + private string $subject = ''; private string $body = ''; @@ -91,6 +90,11 @@ public function attachments(array $attachments): static return $this; } + public function folders(): Folders + { + return new Folders; + } + public function singleValueExtendedProperties(array $singleValueExtendedProperties): static { $this->singleValueExtendedProperties = $singleValueExtendedProperties; @@ -112,6 +116,13 @@ public function skip(string $skip): static return $this; } + public function delta(?bool $delta = true): static + { + $this->delta = $delta; + + return $this; + } + /** * @throws Exception */ @@ -121,6 +132,11 @@ public function get(string $folderIdOrName = 'Inbox', array $params = []): array $top = request('top', $this->top); $skip = request('skip', $this->skip); + $search = request('search', $this->search); + + if (filled($search) && $this->delta) { + throw new Exception('Search is not supported in delta queries.'); + } if ($top === '') { $top = 25; @@ -138,16 +154,28 @@ public function get(string $folderIdOrName = 'Inbox', array $params = []): array ]; } - if ($this->isId($folderIdOrName)) { - $folder = MsGraph::emails()->folders()->find($folderIdOrName); - } else { - $folder = MsGraph::emails()->folders()->findByName($folderIdOrName); - } + $folder = $folderId == '' ? 'Inbox' : $folderId; + + // get inbox from folders list + $folder = MsGraph::get("me/mailFolders?\$filter=startswith(displayName,'$folder')"); - if ($folder !== []) { - return MsGraph::get("me/mailFolders/".$folder['id']."/messages?".http_build_query($params)); - } else { - throw new Exception('Email folder not found'); + if (isset($folder['value'][0])) { + // folder id + $folderId = $folder['value'][0]['id']; + $messages = $this->delta ? 'messages/delta' : 'messages'; + + // get messages from folderId + if ($this->isId($folderIdOrName)) { + $folder = MsGraph::emails()->folders()->find($folderIdOrName); + } else { + $folder = MsGraph::emails()->folders()->findByName($folderIdOrName); + } + + if ($folder !== []) { + return MsGraph::get('me/mailFolders/'.$folder['id']."/{$messages}?".http_build_query($params)); + } else { + throw new Exception('Email folder not found'); + } } } diff --git a/src/Resources/Emails/Folders.php b/src/Resources/Emails/Folders.php index 454342e..cf6c013 100644 --- a/src/Resources/Emails/Folders.php +++ b/src/Resources/Emails/Folders.php @@ -39,6 +39,7 @@ public function find(string $id): array public function findByName(string $name): array { $response = MsGraph::get("me/mailFolders?\$filter=startswith(displayName,'$name')"); + return $response['value'][0] ?? []; } diff --git a/src/Validators/GraphQueryValidator.php b/src/Validators/GraphQueryValidator.php index 79914da..f0e2f8b 100644 --- a/src/Validators/GraphQueryValidator.php +++ b/src/Validators/GraphQueryValidator.php @@ -4,8 +4,6 @@ namespace Dcblogdev\MsGraph\Validators; -use InvalidArgumentException; - class GraphQueryValidator extends Validator { protected static array $allowedParams = [ diff --git a/tests/Validators/EmailFolderUpdateValidatorTest.php b/tests/Validators/EmailFolderUpdateValidatorTest.php index b04ba8a..519dcd8 100644 --- a/tests/Validators/EmailFolderUpdateValidatorTest.php +++ b/tests/Validators/EmailFolderUpdateValidatorTest.php @@ -4,11 +4,11 @@ test('valid parameters are accepted', function () { $response = EmailFolderUpdateValidator::validate([ - 'displayName' => 'demo' + 'displayName' => 'demo', ]); expect($response)->toEqual([ - 'displayName' => 'demo' + 'displayName' => 'demo', ]); }); diff --git a/tests/Validators/GraphQueryValidatorTest.php b/tests/Validators/GraphQueryValidatorTest.php index b3d87ad..54b138f 100644 --- a/tests/Validators/GraphQueryValidatorTest.php +++ b/tests/Validators/GraphQueryValidatorTest.php @@ -40,4 +40,4 @@ '$search' => 'test', '$format' => 'pdf', ]); -})->throws(InvalidArgumentException::class, 'Invalid parameters: $tops. Allowed parameters: $top, $skip, $filter, $orderby, $select, $expand, $count, $search, $format.'); \ No newline at end of file +})->throws(InvalidArgumentException::class, 'Invalid parameters: $tops. Allowed parameters: $top, $skip, $filter, $orderby, $select, $expand, $count, $search, $format.'); diff --git a/tests/Validators/ValidatorTest.php b/tests/Validators/ValidatorTest.php index 701777e..f7f20ce 100644 --- a/tests/Validators/ValidatorTest.php +++ b/tests/Validators/ValidatorTest.php @@ -10,6 +10,6 @@ test('cannot validate none existing params', function () { Validator::validate([ - '$top' => 10 + '$top' => 10, ]); -})->throws(InvalidArgumentException::class, 'Invalid parameters: $top. Allowed parameters: .'); \ No newline at end of file +})->throws(InvalidArgumentException::class, 'Invalid parameters: $top. Allowed parameters: .');