Skip to content

Commit

Permalink
Merge pull request #15 from Calendart/graph
Browse files Browse the repository at this point in the history
Switch from Office365 to Graph API
  • Loading branch information
Taluu committed Mar 23, 2016
2 parents e87d72a + deba247 commit a34c72a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 52 deletions.
3 changes: 1 addition & 2 deletions src/Api/CalendarApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Doctrine\Common\Collections\ArrayCollection;

use CalendArt\AbstractCalendar;
use CalendArt\Adapter\CalendarApiInterface;

use CalendArt\Adapter\Office365\Model\Calendar;
Expand Down Expand Up @@ -55,7 +54,7 @@ public function getList()
$list = new ArrayCollection;

foreach ($result['value'] as $item) {
$list[$item['Id']] = Calendar::hydrate($item);
$list[$item['id']] = Calendar::hydrate($item);
}

return $list;
Expand Down
2 changes: 1 addition & 1 deletion src/Api/EventApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function requestEvents($url, array $params = [])
$list = new ArrayCollection;

foreach ($result['value'] as $item) {
$list[$item['Id']] = Event::hydrate($item);
$list[$item['id']] = Event::hydrate($item);
}

return $list;
Expand Down
10 changes: 5 additions & 5 deletions src/Model/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public function getEtag()

public static function hydrate(array $data)
{
if (!isset($data['Id'], $data['Name'], $data['ChangeKey'])) {
throw new InvalidArgumentException(sprintf('Missing at least one of the mandatory properties "Id", "Name", "ChangeKey" ; got ["%s"]', implode('", "', array_keys($data))));
if (!isset($data['id'], $data['name'], $data['changeKey'])) {
throw new InvalidArgumentException(sprintf('Missing at least one of the mandatory properties "id", "name", "changeKey" ; got ["%s"]', implode('", "', array_keys($data))));
}

$calendar = new static($data['Name']);
$calendar->id = $data['Id'];
$calendar->etag = $data['ChangeKey'];
$calendar = new static($data['name']);
$calendar->id = $data['id'];
$calendar->etag = $data['changeKey'];

return $calendar;
}
Expand Down
66 changes: 35 additions & 31 deletions src/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
namespace CalendArt\Adapter\Office365\Model;

use Datetime;
use DateTimezone;
use DateTimeZone;

use Exception;
use InvalidArgumentException;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;

use CalendArt\AbstractEvent;
Expand Down Expand Up @@ -248,62 +247,67 @@ public function getRaw()
*/
public static function hydrate(array $data, Calendar $calendar = null)
{
if (!isset($data['Id'], $data['ChangeKey'])) {
throw new InvalidArgumentException(sprintf('Missing at least one of the mandatory properties "Id", "ChangeKey" ; got ["%s"]', implode('", "', array_keys($data))));
if (!isset($data['id'], $data['changeKey'])) {
throw new InvalidArgumentException(sprintf('Missing at least one of the mandatory properties "id", "changeKey" ; got ["%s"]', implode('", "', array_keys($data))));
}

$event = new static($calendar);

$event->id = $data['Id'];
$event->etag = $data['ChangeKey'];
$event->id = $data['id'];
$event->etag = $data['changeKey'];
$event->raw = $data;

// if subject is not set or is null, we use null as name
$event->name = isset($data['Subject']) ? $data['Subject'] : null;
$event->name = isset($data['subject']) ? $data['subject'] : null;

if (!empty($data['BodyPreview'])) {
$event->description = $data['BodyPreview'];
if (!empty($data['bodyPreview'])) {
$event->description = $data['bodyPreview'];
}

if (isset($data['Location']) && isset($data['Location']['DisplayName'])) {
$event->location = $data['Location']['DisplayName'];
if (isset($data['location']) && isset($data['location']['displayName'])) {
$event->location = $data['location']['displayName'];
}

$event->createdAt = new Datetime($data['DateTimeCreated']);
$event->updatedAt = new Datetime($data['DateTimeLastModified']);
$event->createdAt = new Datetime($data['createdDateTime']);
$event->updatedAt = new Datetime($data['lastModifiedDateTime']);

$event->end = new Datetime($data['End']);
$event->start = new Datetime($data['Start']);
if (isset($data['start']) && isset($data['start']['dateTime'])) {
$event->start = new Datetime($data['start']['dateTime']);
}

if (isset($data['end']) && isset($data['end']['dateTime'])) {
$event->end = new Datetime($data['end']['dateTime']);
}

$windowsTimezone = new WindowsTimezone();

try {
$event->end->setTimezone(new DateTimezone($windowsTimezone->getTimezone($data['EndTimeZone'])));
$event->end->setTimezone(new DateTimeZone($windowsTimezone->getTimezone($data['end']['timeZone'])));
} catch (Exception $e) { }

try {
$event->start->setTimezone(new DateTimezone($windowsTimezone->getTimezone($data['StartTimeZone'])));
$event->start->setTimezone(new DateTimeZone($windowsTimezone->getTimezone($data['start']['timeZone'])));
} catch (Exception $e) { }

$event->recurrence = $data['Recurrence'];
$event->allDay = true === $data['IsAllDay'];
$event->cancelled = true === $data['IsCancelled'];
$event->recurrence = $data['recurrence'];
$event->allDay = true === $data['isAllDay'];
$event->cancelled = true === $data['isCancelled'];

$event->categories = $data['Categories'];
$event->categories = $data['categories'];

$event->importance = self::translateConstantToValue('IMPORTANCE_', $data['Importance']);
$event->status = self::translateConstantToValue('STATUS_', $data['ShowAs']);
$event->type = self::translateConstantToValue('TYPE_', $data['Type']);
$event->importance = self::translateConstantToValue('IMPORTANCE_', $data['importance']);
$event->status = self::translateConstantToValue('STATUS_', $data['showAs']);
$event->type = self::translateConstantToValue('TYPE_', $data['type']);

$event->owner = Office365Adapter::buildUser($data['Organizer']);
$event->owner = Office365Adapter::buildUser($data['organizer']);
$event->owner->addEvent($event);

$event->participations = new ArrayCollection;

//now the fun stuff : the attendees
foreach ($data['Attendees'] ?: [] as $attendee) {
foreach ($data['attendees'] ?: [] as $attendee) {
// a resource is not an attendee
if ('Resource' === $attendee['Type']) {
if ('resource' === $attendee['type']) {
continue;
}

Expand All @@ -315,14 +319,14 @@ public static function hydrate(array $data, Calendar $calendar = null)
}

$participation = new EventParticipation($event, $user, $role, EventParticipation::STATUS_NONE);
if (isset($attendee['Status'])) {
$participation->setStatus(EventParticipation::translateStatus($attendee['Status']['Response']));
if (isset($attendee['status'])) {
$participation->setStatus(EventParticipation::translateStatus($attendee['status']['response']));

if (EventParticipation::STATUS_NONE !== $participation->getStatus()) {
$participation->setAnsweredAt(new Datetime($attendee['Status']['Time']));
$participation->setAnsweredAt(new Datetime($attendee['status']['time']));
}
}
$participation->setType(EventParticipation::translateType($attendee['Type']));
$participation->setType(EventParticipation::translateType($attendee['type']));

$user->addEvent($event);
$event->participations->add($participation);
Expand Down
12 changes: 6 additions & 6 deletions src/Model/EventParticipation.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ public static function translateStatus($status)
{
switch($status) {
case null:
case 'None':
case 'NotResponded':
case 'none':
case 'notResponded':
return static::STATUS_NONE;

case 'TentativelyAccepted':
case 'tentativelyAccepted':
return static::STATUS_TENTATIVE;

case 'Accepted':
case 'accepted':
return static::STATUS_ACCEPTED;

case 'Declined':
case 'declined':
return static::STATUS_DECLINED;

default:
throw new InvalidArgumentException(sprintf('Wrong status sent. Expected one of [\'None\', \'NotResponded\', \'TentativelyAccepted\', \'Accepted\', \'Declined\'], had "%s"', $status));
throw new InvalidArgumentException(sprintf('Wrong status sent. Expected one of [\'none\', \'notResponded\', \'tentativelyAccepted\', \'accepted\', \'declined\'], had "%s"', $status));
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace CalendArt\Adapter\Office365\Model;

use InvalidArgumentException;

use CalendArt\User as BaseUser;

/**
Expand Down Expand Up @@ -45,14 +47,14 @@ public function getRawData()

public static function hydrate(array $data)
{
if (!isset($data['Address'], $data['Name'])) {
throw new InvalidArgumentException(sprintf('Missing some required key (required : [\'Address\', \'Name\'], got [\'%s\'])', array_keys($data)));
if (!isset($data['address'], $data['name'])) {
throw new InvalidArgumentException(sprintf('Missing some required key (required : [\'address\', \'name\'], got [\'%s\'])', array_keys($data)));
}

$user = new static($data['Name'], $data['Address']);
$user = new static($data['name'], $data['address']);

$user->raw = $data;
$user->id = sha1($data['Address']);
$user->id = sha1($data['address']);

return $user;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Office365Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Office365Adapter implements AdapterInterface
/** @param string $token access token delivered by azure's oauth system */
public function __construct($token)
{
$this->guzzle = new Guzzle(['base_url' => 'https://outlook.office365.com/api/v1.0/me/',
$this->guzzle = new Guzzle(['base_url' => 'https://graph.microsoft.com/v1.0/me/',
'defaults' => ['exceptions' => false,
'headers' => ['Authorization' => sprintf('Bearer %s', $token),
'Content-Type' => 'application/json',
Expand Down Expand Up @@ -78,10 +78,10 @@ public function getEventApi()
*/
public static function buildUser(array $data)
{
$id = sha1($data['EmailAddress']['Address']);
$id = sha1($data['emailAddress']['address']);

if (!isset(static::$users[$id])) {
static::$users[$id] = User::hydrate($data['EmailAddress']);
static::$users[$id] = User::hydrate($data['emailAddress']);
}

return static::$users[$id];
Expand Down

0 comments on commit a34c72a

Please sign in to comment.