├── .gitignore ├── src ├── Exception │ ├── NotFoundInterface.php │ ├── ApiErrorInterface.php │ └── InvalidCredentialsInterface.php ├── Adapter │ ├── Calendar │ │ └── AclInterface.php │ ├── AdapterInterface.php │ ├── CalendarApiInterface.php │ └── EventApiInterface.php ├── User.php ├── UserPermission.php ├── AbstractCalendar.php ├── EventParticipation.php └── AbstractEvent.php ├── phpunit.xml ├── composer.json ├── LICENSE ├── README.md └── tests └── EventParticipationTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock 3 | bin 4 | -------------------------------------------------------------------------------- /src/Exception/NotFoundInterface.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | interface AclInterface 24 | { 25 | /** 26 | * Returns the permissions for a calendar (and replace all old permissions) 27 | * 28 | * @return Collection 29 | */ 30 | public function getPermissions(AbstractCalendar $calendar); 31 | } 32 | -------------------------------------------------------------------------------- /src/Adapter/AdapterInterface.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | interface AdapterInterface 22 | { 23 | /** 24 | * Get the Calendar API to use for this adapter 25 | * 26 | * @return CalendarApiInterface 27 | */ 28 | public function getCalendarApi(); 29 | 30 | /** 31 | * Get the Event API to use for this adapter 32 | * 33 | * @return EventApiInterface 34 | */ 35 | public function getEventApi(); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 15 | 16 | 17 | 18 | ./tests 19 | 20 | 21 | 22 | 23 | 24 | ./src 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "calendart/calendart", 3 | "type": "library", 4 | "description": "Manage remote calendars through an unified api", 5 | "keywords": ["Calendar", "Events", "Scheduler", "Schedule", "Agenda"], 6 | "license": "MIT", 7 | 8 | "authors": [ 9 | { 10 | "name": "Baptiste Clavié", 11 | "email": "baptiste@wisembly.com", 12 | "homepage": "http://baptiste.xn--clavi-fsa.net/", 13 | "role": "Project Lead" 14 | } 15 | ], 16 | 17 | "require": { 18 | "php": ">=5.4.0", 19 | 20 | "psr/log": "^1.0", 21 | "doctrine/collections": "^1.0" 22 | }, 23 | 24 | "autoload": { 25 | "psr-4": { 26 | "CalendArt\\": "src/" 27 | } 28 | }, 29 | 30 | "autoload-dev": { 31 | "psr-4": { 32 | "CalendArt\\Tests\\": "tests/" 33 | } 34 | }, 35 | 36 | "minimum-stability": "dev", 37 | "prefer-stable": true 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Wisembly 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CalendArt 2 | ========= 3 | 4 | Interface to handle all your calendars through an unified API, whatever their 5 | source are (Google, Office 365, ... etc), as if it was an art. Hell yeah. 6 | 7 | Installation 8 | ============ 9 | You have multiple ways to install CalendArt. If you are unsure what to do, go with 10 | [the archive release](#archive-release). 11 | 12 | ### Archive Release 13 | 1. Download the most recent release from the [release page](https://github.com/Wisembly/CalendArt/releases) 14 | 2. Unpack the archive 15 | 3. Move the files somewhere in your project 16 | 17 | ### Development version 18 | 1. Install Git 19 | 2. `git clone git://github.com/Wisembly/CalendArt.git` 20 | 21 | ### Via Composer 22 | 1. Install composer in your project: `curl -s http://getcomposer.org/installer | php` 23 | 2. Run the following command: 24 | ``` 25 | $ composer require calendArt/calendArt 26 | ``` 27 | 28 | Running Tests 29 | ============= 30 | ```console 31 | $ php composer.phar install --dev 32 | $ phpunit 33 | ``` 34 | 35 | Credits 36 | ======= 37 | Made with love by [@wisembly](http://wisembly.com/en/) 38 | -------------------------------------------------------------------------------- /src/Adapter/CalendarApiInterface.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | interface CalendarApiInterface 24 | { 25 | /** 26 | * Get all the calendars available with the current connexion 27 | * 28 | * @return Collection 29 | */ 30 | public function getList(); 31 | 32 | /** 33 | * Returns the specific information for a given calendar 34 | * 35 | * @param mixed $identifier Identifier of the calendar to fetch 36 | * 37 | * @return AbstractCalendar 38 | */ 39 | public function get($identifier); 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/Adapter/EventApiInterface.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | interface EventApiInterface 25 | { 26 | /** 27 | * Get all the events available on the selected calendar 28 | * 29 | * @return Collection 30 | */ 31 | public function getList(); 32 | 33 | /** 34 | * Returns the specific information for a given event 35 | * 36 | * @param mixed $identifier Identifier of the event to fetch 37 | * 38 | * @return AbstractEvent 39 | */ 40 | public function get($identifier); 41 | 42 | /** 43 | * Make an event persistent within the provider 44 | * $options is an array containing request's specific options 45 | * 46 | * @return void 47 | */ 48 | public function persist(AbstractEvent $event, array $options = []); 49 | } 50 | -------------------------------------------------------------------------------- /tests/EventParticipationTest.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class EventParticipationTest extends \PHPUnit_Framework_TestCase 12 | { 13 | public function testGetAvailableStatuses() 14 | { 15 | self::assertSame( 16 | [ 17 | EventParticipation::STATUS_DECLINED, 18 | EventParticipation::STATUS_TENTATIVE, 19 | EventParticipation::STATUS_ACCEPTED 20 | ], 21 | EventParticipation::getAvailableStatuses() 22 | ); 23 | } 24 | 25 | /** 26 | * @dataProvider statusProvider() 27 | */ 28 | public function testSetValueWithCorrectValue($status) 29 | { 30 | $event = $this->prophesize("CalendArt\\AbstractEvent"); 31 | $user = $this->prophesize("CalendArt\\User"); 32 | $participation = new EventParticipation($event->reveal(), $user->reveal()); 33 | $participation->setStatus($status); 34 | } 35 | 36 | public function statusProvider() 37 | { 38 | return [ 39 | [EventParticipation::STATUS_DECLINED], 40 | [EventParticipation::STATUS_TENTATIVE], 41 | [EventParticipation::STATUS_ACCEPTED] 42 | ]; 43 | } 44 | 45 | /** 46 | * @expectedException \InvalidArgumentException 47 | */ 48 | public function testSetStatusWithInvalidValueShouldThrowAnException() 49 | { 50 | $event = $this->prophesize("CalendArt\\AbstractEvent"); 51 | $user = $this->prophesize("CalendArt\\User"); 52 | $participation = new EventParticipation($event->reveal(), $user->reveal()); 53 | $participation->setStatus(null); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/User.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class User 26 | { 27 | /** @var string User's name */ 28 | protected $name; 29 | 30 | /** @var string User's email */ 31 | protected $email; 32 | 33 | /** @var Collection Collection of events the user is involved in */ 34 | protected $events; 35 | 36 | public function __construct($name, $email) 37 | { 38 | $this->name = $name; 39 | $this->email = $email; 40 | 41 | $this->events = new ArrayCollection; 42 | } 43 | 44 | /** @return string */ 45 | public function getName() 46 | { 47 | return $this->name; 48 | } 49 | 50 | /** @return string */ 51 | public function getEmail() 52 | { 53 | return $this->email; 54 | } 55 | 56 | /** @return Collection */ 57 | public function getEvents() 58 | { 59 | return $this->events; 60 | } 61 | 62 | /** @return $this */ 63 | public function addEvent(AbstractEvent $event) 64 | { 65 | if ($this->events->contains($event)) { 66 | return $this; 67 | } 68 | 69 | $this->events->add($event); 70 | 71 | return $this; 72 | } 73 | 74 | /** @return $this */ 75 | public function removeEvent(AbstractEvent $event) 76 | { 77 | $this->events->removeElement($event); 78 | 79 | return $this; 80 | } 81 | } 82 | 83 | -------------------------------------------------------------------------------- /src/UserPermission.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class UserPermission 22 | { 23 | const NOPE = 0b00; // No rights. At all. 24 | const READ = 0b01; // flag allowing to view this calendar 25 | const WRITE = 0b10; // flag allowing to edit this calendar 26 | 27 | /** @var User */ 28 | private $user; 29 | 30 | /** @var AbstractCalendar */ 31 | private $calendar; 32 | 33 | /** @var Integer Mask of permissions allocated for the User on the Calendar */ 34 | private $mask = 0b0000; 35 | 36 | public function __construct(AbstractCalendar $calendar, User $user, $mask = 0b0000) 37 | { 38 | $this->mask = $mask; 39 | $this->user = $user; 40 | $this->calendar = $calendar; 41 | } 42 | 43 | /** @return integer current mask associated to this user */ 44 | public function getMask() 45 | { 46 | return $this->mask; 47 | } 48 | 49 | /** @return User */ 50 | public function getUser() 51 | { 52 | return $this->user; 53 | } 54 | 55 | /** @return AbstractCalendar */ 56 | public function getCalendar() 57 | { 58 | return $this->calendar; 59 | } 60 | 61 | /** 62 | * Grant a permission on this calendar 63 | * 64 | * @param integer $flag Flag to activate 65 | * 66 | * @return $this 67 | */ 68 | public function grant($flag) 69 | { 70 | if (is_string($flag) && defined('static::' . strtoupper($flag))) { 71 | $flag = constant('static::' . strtoupper($flag)); 72 | } 73 | 74 | $this->mask |= $flag; 75 | 76 | return $this; 77 | } 78 | 79 | /** 80 | * Revoke a permission on this calendar 81 | * 82 | * @param integer $flag Flag to deactivate 83 | * 84 | * @return $this 85 | */ 86 | public function revoke($flag) 87 | { 88 | if (is_string($flag) && defined('static::' . strtoupper($flag))) { 89 | $flag = constant('static::' . strtoupper($flag)); 90 | } 91 | 92 | $this->mask &= ~$flag; 93 | 94 | return $this; 95 | } 96 | 97 | /** 98 | * Checks if the user has the `$flag` right 99 | * 100 | * @param integer $flag Flag to test 101 | * @return boolean 102 | */ 103 | public function isGranted($flag) 104 | { 105 | if (is_string($flag) && defined('static::' . strtoupper($flag))) { 106 | $flag = constant('static::' . strtoupper($flag)); 107 | } 108 | 109 | return $this->mask & $flag; 110 | } 111 | } 112 | 113 | -------------------------------------------------------------------------------- /src/AbstractCalendar.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | abstract class AbstractCalendar 25 | { 26 | /** @var string Calendar's name */ 27 | protected $name; 28 | 29 | /** @var string Calendar's description */ 30 | protected $description = ''; 31 | 32 | /** @var Collection Collection of events */ 33 | protected $events; 34 | 35 | /** @var Collection Collection of permissions accorded to this calendar */ 36 | protected $permissions; 37 | 38 | public function __construct($name) 39 | { 40 | $this->name = $name; 41 | 42 | $this->events = new ArrayCollection; 43 | $this->permissions = new ArrayCollection; 44 | } 45 | 46 | /** @return mixed */ 47 | abstract public function getId(); 48 | 49 | /** @return string */ 50 | public function getName() 51 | { 52 | return $this->name; 53 | } 54 | 55 | /** @return $this */ 56 | public function setName($name) 57 | { 58 | $this->name = $name; 59 | 60 | return $this; 61 | } 62 | 63 | /** @return string */ 64 | public function getDescription() 65 | { 66 | return $this->description; 67 | } 68 | 69 | /** @return $this */ 70 | public function setDescription($description) 71 | { 72 | $this->description = $description; 73 | 74 | return $this; 75 | } 76 | 77 | /** @return Collection */ 78 | public function getEvents() 79 | { 80 | return $this->events; 81 | } 82 | 83 | /** @return $this */ 84 | public function addEvent(AbstractEvent $event) 85 | { 86 | if ($this->events->contains($event)) { 87 | return $this; 88 | } 89 | 90 | $this->events->add($event); 91 | 92 | return $this; 93 | } 94 | 95 | /** @return $this */ 96 | public function detachEvent(AbstractEvent $event) 97 | { 98 | $this->events->removeElement($event); 99 | 100 | return $this; 101 | } 102 | 103 | /** @return Collection */ 104 | public function getPermissions() 105 | { 106 | return $this->permissions; 107 | } 108 | 109 | /** @return $this */ 110 | public function addPermission(UserPermission $permission) 111 | { 112 | if ($this->permissions->contains($permission)) { 113 | return; 114 | } 115 | 116 | $this->permissions->add($permission); 117 | 118 | return $this; 119 | } 120 | 121 | /** @return $this */ 122 | public function removePermission($permission) 123 | { 124 | $this->permissions->removeElement($permission); 125 | 126 | return $this; 127 | } 128 | 129 | /** @return $this */ 130 | public function setPermissions(Collection $collection) 131 | { 132 | $this->permissions = $collection; 133 | 134 | return $this; 135 | } 136 | } 137 | 138 | -------------------------------------------------------------------------------- /src/EventParticipation.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | class EventParticipation 25 | { 26 | // status of the participation 27 | const STATUS_DECLINED = -1; 28 | const STATUS_TENTATIVE = 0; 29 | const STATUS_ACCEPTED = 1; 30 | 31 | // available roles 32 | const ROLE_PARTICIPANT = 0b01; 33 | const ROLE_MANAGER = 0b10; 34 | 35 | /** @var AbstractEvent */ 36 | protected $event; 37 | 38 | /** @var User */ 39 | protected $user; 40 | 41 | /** @var integer */ 42 | protected $role = self::ROLE_PARTICIPANT; 43 | 44 | /** @var Datetime */ 45 | protected $invitedAt; 46 | 47 | /** @var Datetime */ 48 | protected $answeredAt = null; 49 | 50 | /** @var integer */ 51 | protected $status = self::STATUS_TENTATIVE; 52 | 53 | public function __construct(AbstractEvent $event, User $user, $role = self::ROLE_PARTICIPANT, $status = self::STATUS_TENTATIVE) 54 | { 55 | $this->user = $user; 56 | $this->event = $event; 57 | 58 | $this->invitedAt = new Datetime; 59 | 60 | $this->setRole($role); 61 | $this->setStatus($status); 62 | 63 | $user->addEvent($event); 64 | } 65 | 66 | /** @return Datetime|null null if the user has not answered yet to the invitation */ 67 | public function getAnsweredAt() 68 | { 69 | return $this->answeredAt; 70 | } 71 | 72 | /** @return boolean returns true if the user has answered this invitation */ 73 | public function hasAnswered() 74 | { 75 | return null !== $this->answeredAt; 76 | } 77 | 78 | /** @return $this */ 79 | public function setAnsweredAt(Datetime $date = null) 80 | { 81 | $this->answeredAt = $date ?: new Datetime; 82 | 83 | return $this; 84 | } 85 | 86 | /** @return Datetime */ 87 | public function getInvitedAt() 88 | { 89 | return $this->invitedAt; 90 | } 91 | 92 | /** @return AbstractEvent */ 93 | public function getEvent() 94 | { 95 | return $this->event; 96 | } 97 | 98 | /** @return User */ 99 | public function getUser() 100 | { 101 | return $this->user; 102 | } 103 | 104 | /** 105 | * Get the current role of the user in this event 106 | * 107 | * @return integer mask that puts the rights of the user 108 | */ 109 | public function getRole() 110 | { 111 | return $this->role; 112 | } 113 | 114 | /** 115 | * @param integer $role 116 | * 117 | * @return $this 118 | */ 119 | public function setRole($role) 120 | { 121 | $this->role = $role; 122 | 123 | return $this; 124 | } 125 | 126 | /** 127 | * Get the current status of this participation 128 | * 129 | * @return integer 130 | */ 131 | public function getStatus() 132 | { 133 | return $this->status; 134 | } 135 | 136 | /** 137 | * @param integer $status 138 | * 139 | * @return $this 140 | */ 141 | public function setStatus($status) 142 | { 143 | if (!in_array($status, static::getAvailableStatuses(), true)) { 144 | throw new InvalidArgumentException(sprintf('Status not recognized ; Had "%s", expected one of "%s"', $status, implode('", "', static::getAvailableStatuses()))); 145 | } 146 | 147 | $this->status = $status; 148 | 149 | return $this; 150 | } 151 | 152 | /** 153 | * Fetch the available statuses 154 | * 155 | * @return integer[] 156 | */ 157 | public static function getAvailableStatuses() 158 | { 159 | return [self::STATUS_DECLINED, self::STATUS_TENTATIVE, self::STATUS_ACCEPTED]; 160 | } 161 | } 162 | 163 | -------------------------------------------------------------------------------- /src/AbstractEvent.php: -------------------------------------------------------------------------------- 1 | 26 | */ 27 | abstract class AbstractEvent 28 | { 29 | /** @var Datetime Start date of this event */ 30 | protected $start; 31 | 32 | /** @var Datetime End date of this event */ 33 | protected $end; 34 | 35 | /** @var string name of this event */ 36 | protected $name; 37 | 38 | /** @var string Description of this event */ 39 | protected $description; 40 | 41 | /** @var User owner of this event */ 42 | protected $owner; 43 | 44 | /** @var AbstractCalendar Calendar associated to this event */ 45 | protected $calendar; 46 | 47 | /** @var Collection Participations registered to this event */ 48 | protected $participations; 49 | 50 | public function __construct(AbstractCalendar $calendar, User $owner, $name, Datetime $start, Datetime $end) 51 | { 52 | $this->name = $name; 53 | $this->owner = $owner; 54 | $this->calendar = $calendar; 55 | 56 | $this->participations = new ArrayCollection; 57 | 58 | if ($start > $end) { 59 | throw new InvalidArgumentException('An event cannot start after it was ended'); 60 | } 61 | 62 | $this->end = $end; 63 | $this->start = $start; 64 | 65 | $owner->addEvent($this); 66 | $calendar->getEvents()->add($this); 67 | } 68 | 69 | /** @return mixed */ 70 | abstract public function getId(); 71 | 72 | /** @return string */ 73 | public function getName() 74 | { 75 | return $this->name; 76 | } 77 | 78 | /** @return $this */ 79 | public function setName($name) 80 | { 81 | $this->name = $name; 82 | 83 | return $this; 84 | } 85 | 86 | /** @return string */ 87 | public function getDescription() 88 | { 89 | return $this->description; 90 | } 91 | 92 | /** @return $this */ 93 | public function setDescription($description) 94 | { 95 | $this->description = $description; 96 | 97 | return $this; 98 | } 99 | 100 | /** @return User */ 101 | public function getOwner() 102 | { 103 | return $this->owner; 104 | } 105 | 106 | /** @return Datetime */ 107 | public function getStart() 108 | { 109 | return $this->start; 110 | } 111 | 112 | /** @return $this */ 113 | public function setStart(Datetime $start) 114 | { 115 | if ($this->end < $start) { 116 | throw new InvalidArgumentException('An event cannot start after it was ended'); 117 | } 118 | 119 | $this->start = $start; 120 | 121 | return $this; 122 | } 123 | 124 | /** @return Datetime */ 125 | public function getEnd() 126 | { 127 | return $this->end; 128 | } 129 | 130 | /** @return $this */ 131 | public function setEnd(Datetime $end) 132 | { 133 | if ($this->start > $end) { 134 | throw new InvalidArgumentException('An event cannot end before it was started'); 135 | } 136 | 137 | $this->end = $end; 138 | 139 | return $this; 140 | } 141 | 142 | /** 143 | * Checks if this event has already ended 144 | * 145 | * @return Boolean 146 | */ 147 | public function hasEnded(Datetime $current = null) 148 | { 149 | return $this->end < ($current ?: new Datetime); 150 | } 151 | 152 | /** 153 | * Checks if this event has already started 154 | * 155 | * @return Boolean 156 | */ 157 | public function hasStarted(Datetime $current = null) 158 | { 159 | return $this->start <= ($current ?: new Datetime); 160 | } 161 | 162 | /** 163 | * Checks if this event is currently running 164 | * 165 | * @return Boolean 166 | */ 167 | public function isRunning(Datetime $current = null) 168 | { 169 | $current = $current ?: new Datetime; 170 | 171 | return $this->hasStarted($current) && !$this->hasEnded($current); 172 | } 173 | 174 | /** @return AbstractCalendar */ 175 | public function getCalendar() 176 | { 177 | return $this->calendar; 178 | } 179 | 180 | /** 181 | * Detach this event from the associated Calendar 182 | * 183 | * @return $this 184 | */ 185 | public function detach() 186 | { 187 | $this->calendar->getEvents()->removeEvent($this); 188 | $this->calendar = null; 189 | 190 | return $this; 191 | } 192 | 193 | /** @return Collection */ 194 | public function getParticipations() 195 | { 196 | return $this->participations; 197 | } 198 | 199 | /** @return $this */ 200 | public function addParticipation(EventParticipation $participation) 201 | { 202 | $email = $participation->getUser()->getEmail(); 203 | 204 | $callback = function ($key, EventParticipation $participation) use ($email) { 205 | return $email === $participation->getUser()->getEmail(); 206 | }; 207 | 208 | if ($this->participations->exists($callback)) { 209 | return; 210 | } 211 | 212 | $this->participations->add($participation); 213 | 214 | return $this; 215 | } 216 | 217 | /** @return $this */ 218 | public function removeParticipation(EventParticipation $participation) 219 | { 220 | $this->participations->removeElement($participation); 221 | 222 | return $this; 223 | } 224 | } 225 | 226 | --------------------------------------------------------------------------------