Skip to content
Merged
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
19 changes: 18 additions & 1 deletion src/AdminResources/Emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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
*/
Expand All @@ -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([
Expand All @@ -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}");
Expand Down
2 changes: 1 addition & 1 deletion src/MsGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
54 changes: 41 additions & 13 deletions src/Resources/Emails/Emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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;
Expand All @@ -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
*/
Expand All @@ -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;
Expand All @@ -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');
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Resources/Emails/Folders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] ?? [];
}

Expand Down
2 changes: 0 additions & 2 deletions src/Validators/GraphQueryValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Dcblogdev\MsGraph\Validators;

use InvalidArgumentException;

class GraphQueryValidator extends Validator
{
protected static array $allowedParams = [
Expand Down
4 changes: 2 additions & 2 deletions tests/Validators/EmailFolderUpdateValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

test('valid parameters are accepted', function () {
$response = EmailFolderUpdateValidator::validate([
'displayName' => 'demo'
'displayName' => 'demo',
]);

expect($response)->toEqual([
'displayName' => 'demo'
'displayName' => 'demo',
]);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/Validators/GraphQueryValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
})->throws(InvalidArgumentException::class, 'Invalid parameters: $tops. Allowed parameters: $top, $skip, $filter, $orderby, $select, $expand, $count, $search, $format.');
4 changes: 2 additions & 2 deletions tests/Validators/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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: .');
})->throws(InvalidArgumentException::class, 'Invalid parameters: $top. Allowed parameters: .');