Skip to content
Permalink
Browse files

wip

  • Loading branch information
freekmurze committed May 8, 2016
1 parent aeb2a1d commit 0a8019ead1bd7cba9a1840f2e384d9a021adef48
Showing with 172 additions and 44 deletions.
  1. +1 −1 config/laravel-google-calendar.php
  2. +96 −8 src/Event.php
  3. +72 −33 src/GoogleCalendar.php
  4. +3 −2 src/GoogleCalendarFactory.php
@@ -4,7 +4,7 @@
/**
* Path to the client secret json file
*/
'client_secret_json' => storage('app/laravel-google-calendar/client_secret.json'),
'client_secret_json' => storage_path('app/laravel-google-calendar/client_secret.json'),
/**
*
@@ -1,32 +1,120 @@
<?php
use Spatie\GoogleCalendar;
namespace Spatie\GoogleCalendar;
use Carbon\Carbon;
use DateTime;
use Google_Service_Calendar_Event;
use Google_Service_Calendar_EventDateTime;
class Event
{
/** @var string */
public $id;
/** @var string */
public $name;
/** @var \Carbon\Carbon */
/** @var Carbon */
public $startDateTime;
/** @var \Carbon\Carbon */
/** @var Carbon */
public $endDateTime;
/** @var bool */
protected $allDayEvent = false;
public $allDayEvent;
/** @var Google_Service_Calendar_Event*/
public $googleEvent;
/** @var int */
protected $calendarId;
public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent) {
public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId) {
$event = new static;
$event->name = $googleEvent->summary;
$event->startDateTime = $googleEvent['start']['date'] ?? $googleEvent['start']['dateTime'];
$event->endDateTime = $googleEvent['end']['date'] ?? $googleEvent['end']['dateTime'];
if (! is_null($googleEvent['start']['date'])) {
$event->startDateTime = Carbon::createFromFormat('Y-m-d', $googleEvent['start']['date']);
}
if (! is_null($googleEvent['start']['dateTime'])) {
$event->startDateTime = Carbon::createFromFormat(DateTime::RFC3339, $googleEvent['start']['dateTime']);
}
if (! is_null($googleEvent['end']['date'])) {
$event->endDateTime = Carbon::createFromFormat('Y-m-d', $googleEvent['end']['date']);
}
if (! is_null($googleEvent['end']['dateTime'])) {
$event->endDateTime = Carbon::createFromFormat(DateTime::RFC3339, $googleEvent['end']['dateTime']);
}
$event->allDayEvent = is_null($googleEvent['start']['dateTime']);
$event->id = $googleEvent->id;
$event->googleEvent = $googleEvent;
$event->calendarId = $calendarId;
return $event;
}
public function convertToGoogleEvent() : Google_Service_Calendar_Event
{
$googleEvent = new Google_Service_Calendar_Event();
$googleEvent->summary = $this->name;
$start = new Google_Service_Calendar_EventDateTime();
$end = new Google_Service_Calendar_EventDateTime();
if ($this->allDayEvent) {
$start->setDateTime($this->startDateTime->format(DateTime::RFC3339));
$end->setDateTime($this->startDateTime->format(DateTime::RFC3339));
}
else {
$start->setDate($this->startDateTime->format('Y-m-d'));
$end->setDate($this->endDateTime->format('Y-m-d'));
}
$googleEvent->setStart($start);
$googleEvent->setEnd($end);
return $googleEvent;
}
public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, $queryParameters = [], $calendarId = null)
{
$googleCalendar = self::getGoogleCalendar($calendarId);
return $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters);
}
public static function find($id, $calendarId = null)
{
$googleCalendar = self::getGoogleCalendar($calendarId);
return $googleCalendar->getEvent($id);
}
public function save()
{
}
public function delete($id = null)
{
return $this->getGoogleCalendar($this->calendarId)->deleteEvent($id ?? $this->id);
}
protected static function getGoogleCalendar($calendarId = null) : GoogleCalendar
{
$calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id');
return GoogleCalendarFactory::createForCalendarId($calendarId);
}
}
@@ -3,7 +3,7 @@
namespace Spatie\GoogleCalendar;
use Carbon\Carbon;
use Event;
use DateTime;
use Google_Service_Calendar;
use Google_Service_Calendar_Event;
use Illuminate\Support\Collection;
@@ -16,70 +16,109 @@ class GoogleCalendar
/** @var string */
protected $calendarId;
/** @var \Closure */
protected $eventTransformer;
public function __construct(Google_Service_Calendar $calendarService, $calendarId)
{
$this->calendarService = $calendarService;
$this->calendarId = $calendarId;
$this->eventTransformer = function (Google_Service_Calendar_Event $event) {
return Event::createFromGoogleCalendarEvent($event);
};
}
public function getCalendarId() : string
{
return $this->calendarId;
}
/**
* @param callable $eventTransformer
* @return $this
*/
public function setEventTransformer(callable $eventTransformer)
{
$this->eventTransformer = $eventTransformer;
return $this;
}
/**
* List events.
*
* @param Carbon $startTime
* @param Carbon $endTime
* @param array $queryParameters
* @param Carbon $startDateTime
* @param Carbon $endDateTime
* @param array $queryParameters
*
* @link https://developers.google.com/google-apps/calendar/v3/reference/events/list#parameters
* @link https://developers.google.com/google-apps/calendar/v3/reference/events/list
*
* @return Collection
*/
public function listEvents(Carbon $startTime = null, Carbon $endTime = null, $queryParameters = []) : Collection
public function listEvents(Carbon $startDateTime = null, Carbon $endDateTime = null, $queryParameters = []) : Collection
{
$parameters = [];
$parameters = ['singleEvents' => true];
if ($startTime) {
$parameters['timeMin'] = $startTime->toAtomString();
if (is_null($startDateTime)) {
$startDateTime = Carbon::now()->startOfDay();
}
$parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339);
if ($endTime) {
$parameters['timeMax'] = $startTime->toAtomString();
if (is_null($endDateTime)) {
$endDateTime = Carbon::now()->addYear()->endOfDay();
}
$parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339);
$parameters = array_merge($parameters, $queryParameters);
$events = $this
$googleEvents = $this
->calendarService
->events
->listEvents($this->calendarId, $parameters)
->getItems();
return collect($events)->map($this->eventTransformer);
$events = collect($googleEvents)
->map(function (Google_Service_Calendar_Event $event) {
return Event::createFromGoogleCalendarEvent($event, $this->calendarId);
})
->sortBy(function (Event $event) {
return $event->startDateTime->format(DATE_ISO8601);
});
/*
$debug = $events->map(function (Event $event) {
return $event->startDateTime->format('Y-m-d H:i:s') . ' - ' . $event->startDateTime->format('Y-m-d H:i:s') . $event->name . PHP_EOL;
});
*/
return $events;
}
/**
* @param string $eventId
* @return static
*/
public function getEvent($eventId)
{
$googleEvent = $this->calendarService->events->get($this->calendarId, $eventId);
return Event::createFromGoogleCalendarEvent($googleEvent, $this->calendarId);
}
/**
* Insert an event
*
* @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event
*
* @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert
*
* @return mixed
*/
public function insertEvent($event)
{
if ($event instanceof Event) {
$event = $event->convertToGoogleEvent();
}
return $this->calendarService->events->insert($this->calendarId, $event);
}
/**
* @param string|\Spatie\GoogleCalendar\Event $eventId
*/
public function deleteEvent($eventId)
{
if ($eventId instanceof Event) {
$eventId = $eventId->id;
}
return $this->calendarService->events->delete($this->calendarId, $eventId);
}
public function getService() : Google_Service_Calendar
{
return $this->calendarService;
@@ -2,11 +2,12 @@
namespace Spatie\GoogleCalendar;
use Google_Client;
use Google_Service_Calendar;
class GoogleCalendarFactory
{
public static function createForCalendarId() : GoogleCalendar
public static function createForCalendarId($calendarId) : GoogleCalendar
{
$config = config('laravel-google-calendar');
@@ -18,6 +19,6 @@ public static function createForCalendarId() : GoogleCalendar
$service = new Google_Service_Calendar($client);
return new GoogleCalendar($service, $config['calendarId']);
return new GoogleCalendar($service, $calendarId);
}
}

0 comments on commit 0a8019e

Please sign in to comment.
You can’t perform that action at this time.