Skip to content

Commit

Permalink
Added calendar update and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThompsonTLDR committed Oct 26, 2020
1 parent f8768ed commit bea216e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
73 changes: 73 additions & 0 deletions src/Calendars/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,38 @@ public function addCalendar(array $params): array

// ------------------------------------------------------------------------------

/**
* update calendar
*
* @param array $params
*
* @return array
*/
public function updateCalendar(array $params): array
{
$rules = $this->addCalendarRules();
$rules[] = V::key('id', V::stringType()->notEmpty());

$accessToken = $this->options->getAccessToken();

V::doValidate(V::keySet(...$rules), $params);
V::doValidate(V::stringType()->notEmpty(), $accessToken);

$path = $params['id'];
$header = ['Authorization' => $accessToken];

unset($params['id']);

return $this->options
->getSync()
->setPath($path)
->setFormParams($params)
->setHeaderParams($header)
->put(API::LIST['oneCalendar']);
}

// ------------------------------------------------------------------------------

/**
* get calendar
*
Expand Down Expand Up @@ -138,6 +170,47 @@ public function getCalendar($calendarId): array

// ------------------------------------------------------------------------------

/**
* delete calendar
*
* @param mixed $contactId string|string[]
*
* @return array
*/
public function deleteCalendar($contactId): array
{
$calendarId = Helper::fooToArray($contactId);
$accessToken = $this->options->getAccessToken();

$rule = V::simpleArray(V::stringType()->notEmpty());

V::doValidate($rule, $calendarId);
V::doValidate(V::stringType()->notEmpty(), $accessToken);

$queues = [];
$target = API::LIST['oneCalendar'];
$header = ['Authorization' => $accessToken];

foreach ($calendarId as $id)
{
$request = $this->options
->getAsync()
->setPath($id)
->setHeaderParams($header);

$queues[] = static function () use ($request, $target)
{
return $request->delete($target);
};
}

$pools = $this->options->getAsync()->pool($queues, false);

return Helper::concatPoolInfos($calendarId, $pools);
}

// ------------------------------------------------------------------------------

/**
* rules for add calendar
*
Expand Down
43 changes: 42 additions & 1 deletion tests/CalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,47 @@ public function testAddCalendar(): void

// ------------------------------------------------------------------------------

public function testUpdateCalendar(): void
{
$data = $this->client->Calendars()->Calendar()->addCalendar($this->getCalendarInfo());

$params =
[
'id' => $data['id'],
'name' => 'Different Calendar',
'description' => 'This is now a different calendar.',
'timezone' => 'America/Los_Angeles',
];

$data = $this->client->Calendars()->Calendar()->updateContact($params);

$this->assertEquals($data['id'], $params['id']);
$this->assertEquals($data['name'], $params['name']);
$this->assertEquals($data['description'], $params['description']);
$this->assertEquals($data['timezone'], $params['timezone']);
}

// ------------------------------------------------------------------------------

public function testDeleteCalendar(): void
{
$data = $this->client->Calendars()->Calendar()->addCalendar($this->getCalendarInfo());

try
{
$back = true;
$this->client->Calendars()->Calendar()->deleteCalendar($data['id']);
}
catch (Exception $e)
{
$back = false;
}

$this->assertTrue($back);
}

// ------------------------------------------------------------------------------

/**
* @return array
*/
Expand All @@ -60,7 +101,7 @@ private function getCalendarInfo(): array
return [
'name' => 'Test Calendar',
'description' => 'This is a test calendar.',
'timezone' => 'America/New_York'
'timezone' => 'America/New_York',
'location' => 'Front Conference Room',
];
}
Expand Down

0 comments on commit bea216e

Please sign in to comment.