From 1864df339464352b3f11cc485b4d807f53ca716a Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Mon, 22 Feb 2016 17:51:19 +0100 Subject: [PATCH] Add attachments feature --- src/Api/EventApi.php | 53 +++++++++++++++++++- src/Model/Attachment.php | 40 +++++++++++++++ src/Model/Event.php | 16 ++++-- src/Model/FileAttachment.php | 94 ++++++++++++++++++++++++++++++++++++ src/Model/ItemAttachment.php | 79 ++++++++++++++++++++++++++++++ 5 files changed, 277 insertions(+), 5 deletions(-) create mode 100644 src/Model/Attachment.php create mode 100644 src/Model/FileAttachment.php create mode 100644 src/Model/ItemAttachment.php diff --git a/src/Api/EventApi.php b/src/Api/EventApi.php index 07f0372..23060bc 100644 --- a/src/Api/EventApi.php +++ b/src/Api/EventApi.php @@ -22,6 +22,7 @@ use CalendArt\Adapter\Office365\Model\Event; use CalendArt\Adapter\Office365\Model\Calendar; +use CalendArt\Adapter\Office365\Model\Attachment; use CalendArt\Adapter\Office365\Office365Adapter; use CalendArt\Adapter\Office365\Exception\ApiErrorException; @@ -152,11 +153,61 @@ public function get($identifier) throw new ApiErrorException($response); } - return Event::hydrate($response->json()); + $event = Event::hydrate($response->json()); + + if ($event->hasAttachments()) { + foreach ($this->getAttachments($identifier) as $attachment) { + $event->addAttachment($attachment); + } + } + + return $event; } /** {@inheritDoc} */ public function persist(AbstractEvent $event) { } + + /** + * Get the attachments collection of an event + * + * @param string identifier of the event + * @param string $filter `$filter` query parameter to give to the request + * @param string $orderBy `$orderBy` query, to have an order of elements + * + * @see https://msdn.microsoft.com/office/office365/APi/calendar-rest-operations#GetCalendarView + */ + public function getAttachments($identifier, $filter = '', $orderBy = '', array $extraParameters = []) + { + $url = sprintf('events/%s/attachments', $identifier); + + $params = []; + + if (!empty($filter)) { + $params['$filter'] = $filter; + } + + if (!empty($orderBy)) { + $params['$orderBy'] = $orderBy; + } + + $params = ['query' => array_merge($params, $extraParameters)]; + + $request = $this->guzzle->createRequest('GET', $url, $params); + $response = $this->guzzle->send($request); + + if (200 > $response->getStatusCode() || 300 <= $response->getStatusCode()) { + throw new ApiErrorException($response); + } + + $result = $response->json(); + $attachments = new ArrayCollection; + + foreach ($result['value'] as $attachment) { + $attachments[$attachment['Id']] = Attachment::hydrate($attachment); + } + + return $attachments; + } } diff --git a/src/Model/Attachment.php b/src/Model/Attachment.php new file mode 100644 index 0000000..c0fc2c5 --- /dev/null +++ b/src/Model/Attachment.php @@ -0,0 +1,40 @@ +calendar = $calendar; + $this->calendar= $calendar; if (null !== $calendar) { $calendar->getEvents()->add($this); @@ -297,6 +297,9 @@ public static function hydrate(array $data, Calendar $calendar = null) $event->participations = new ArrayCollection; + $event->attachments = new ArrayCollection; + $event->hasAttachments = $data['HasAttachments']; + //now the fun stuff : the attendees foreach ($data['Attendees'] ?: [] as $attendee) { // a resource is not an attendee @@ -327,4 +330,9 @@ public static function hydrate(array $data, Calendar $calendar = null) return $event; } + + public function hasAttachments() + { + return $this->hasAttachments; + } } diff --git a/src/Model/FileAttachment.php b/src/Model/FileAttachment.php new file mode 100644 index 0000000..d15d803 --- /dev/null +++ b/src/Model/FileAttachment.php @@ -0,0 +1,94 @@ +name = $name; + $this->uri = $uri; + } + + /** + * Hydrate a new Event object with data received from Office365 api + * + * @param array $data Data to feed the Event object with + * @return CalendArt\Attachment + */ + public static function hydrate(array $data) + { + if (!isset($data['Id'], $data['@odata.type'])) { + throw new InvalidArgumentException(sprintf('Missing at least one of the mandatory properties "Id", "@odata.type" ; got ["%s"]', implode('", "', array_keys($data)))); + } + + if ('#Microsoft.OutlookServices.FileAttachment' !== $data['@odata.type']) { + throw new InvalidArgumentException(sprintf('Unknown attachment type %s', $data['@odata.type'])); + } + + $attachment = new static($data['Name'], $data['ContentLocation']); + $attachment->id = $data['Id']; + $attachment->content = $data['ContentBytes']; + $attachment->size = $data['Size']; + $attachment->contentType = $data['ContentType']; + $attachment->raw = $data; + + return $attachment; + } + + public function getName() + { + return $this->name; + } + + public function getUri() + { + return $this->uri; + } + + public function getSize() + { + return $this->size; + } + + public function getContentType() + { + return $this->contentType; + } + + public function getId() + { + return $this->id; + } + + public function getRaw() + { + return $this->raw; + } + + public function getContents() + { + return $this->content; + } +} diff --git a/src/Model/ItemAttachment.php b/src/Model/ItemAttachment.php new file mode 100644 index 0000000..0da3371 --- /dev/null +++ b/src/Model/ItemAttachment.php @@ -0,0 +1,79 @@ +event = $event; + $this->name = $name; + } + + /** + * Hydrate a new Event object with data received from Office365 api + * + * @param array $data Data to feed the Event object with + * @return CalendArt\Attachment + */ + public static function hydrate(array $data) + { + if (!isset($data['Id'], $data['@odata.type'])) { + throw new InvalidArgumentException(sprintf('Missing at least one of the mandatory properties "Id", "@odata.type" ; got ["%s"]', implode('", "', array_keys($data)))); + } + + if ('#Microsoft.OutlookServices.ItemAttachment' !== $data['@odata.type']) { + throw new InvalidArgumentException(sprintf('Unknown attachment type %s', $data['@odata.type'])); + } + + $attachment = new static($data['Name'], Event::hydrate($data['Event'])); + $attachment->id = $data['Id']; + $attachment->contentType = $data['ContentType']; + $attachment->raw = $data; + + return $attachment; + } + + public function getEvent() + { + return $this->event; + } + + public function getContentType() + { + return $this->contentType; + } + + public function getId() + { + return $this->id; + } + + public function getRaw() + { + return $this->raw; + } + + public function getContents() + { + return $this->getEvent(); + } +}