Skip to content
Open
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
53 changes: 52 additions & 1 deletion src/Api/EventApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be $list ?

}
}
40 changes: 40 additions & 0 deletions src/Model/Attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* This file is part of the CalendArt package
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*
* @copyright Wisembly
* @license http://www.opensource.org/licenses/MIT-License MIT License
*/

namespace CalendArt\Adapter\Office365\Model;

use InvalidArgumentException;

abstract class Attachment
{
/**
* 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']) {
return FileAttachment::hydrate($data);
}

if ('#Microsoft.OutlookServices.ItemAttachment' === $data['@odata.type']) {
return ItemAttachment::hydrate($data);
}

throw new InvalidArgumentException(sprintf('Unknown attachment type %s', $data['@odata.type']));
}
}
16 changes: 12 additions & 4 deletions src/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ class Event extends AbstractEvent
/** @var Datetime */
private $updatedAt;

/** @var Attachment */
private $attachments = [];

/** @var integer */
private $importance = self::IMPORTANCE_NORMAL;

Expand All @@ -89,9 +86,12 @@ class Event extends AbstractEvent
/** @var array */
private $raw;

/** @var boolean */
private $hasAttachments = false;

public function __construct(Calendar $calendar = null)
{
$this->calendar = $calendar;
$this->calendar= $calendar;

if (null !== $calendar) {
$calendar->getEvents()->add($this);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -327,4 +330,9 @@ public static function hydrate(array $data, Calendar $calendar = null)

return $event;
}

public function hasAttachments()
{
return $this->hasAttachments;
}
}
94 changes: 94 additions & 0 deletions src/Model/FileAttachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* This file is part of the CalendArt package
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*
* @copyright Wisembly
* @license http://www.opensource.org/licenses/MIT-License MIT License
*/

namespace CalendArt\Adapter\Office365\Model;

use InvalidArgumentException;

use CalendArt\UriAttachment;

class FileAttachment implements UriAttachment
{
private $name;
private $uri;
private $content;
private $id;
private $size;
private $contentType;
private $raw;

public function __construct($name, $uri)
{
$this->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;
}
}
79 changes: 79 additions & 0 deletions src/Model/ItemAttachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* This file is part of the CalendArt package
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*
* @copyright Wisembly
* @license http://www.opensource.org/licenses/MIT-License MIT License
*/

namespace CalendArt\Adapter\Office365\Model;

use InvalidArgumentException;

use CalendArt\Attachement;

class FileAttachment implements UriAttachment
{
private $id;
private $name;
private $contentType;
private $item;

public function __construct($name, Event $event)
{
$this->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();
}
}