forked from cronofy/cronofy-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cronofy#97 from basekit/batch
Add batch endpoint support
- Loading branch information
Showing
7 changed files
with
792 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cronofy\Batch; | ||
|
||
use Cronofy\Cronofy; | ||
|
||
class Batch | ||
{ | ||
/** @var BatchRequest[] */ | ||
private $requests = []; | ||
|
||
public static function create(): self | ||
{ | ||
return new self(); | ||
} | ||
|
||
public function upsertEvent(string $calendarId, array $data): self | ||
{ | ||
$path = $this->buildEventsPath($calendarId); | ||
|
||
return $this->addPostRequest($path, $data); | ||
} | ||
|
||
public function deleteEvent(string $calendarId, string $eventId): self | ||
{ | ||
$path = $this->buildEventsPath($calendarId); | ||
$data = ['event_id' => $eventId]; | ||
|
||
return $this->addDeleteRequest($path, $data); | ||
} | ||
|
||
public function updateExternalEvent(string $calendarId, array $data): self | ||
{ | ||
$path = $this->buildEventsPath($calendarId); | ||
|
||
return $this->addPostRequest($path, $data); | ||
} | ||
|
||
public function deleteExternalEvent(string $calendarId, string $eventUid): self | ||
{ | ||
$path = $this->buildEventsPath($calendarId); | ||
$data = ['event_uid' => $eventUid]; | ||
|
||
return $this->addDeleteRequest($path, $data); | ||
} | ||
|
||
public function upsertAvailablePeriod(array $data): self | ||
{ | ||
$path = $this->buildAvailablePeriodsPath(); | ||
|
||
return $this->addPostRequest($path, $data); | ||
} | ||
|
||
public function deleteAvailablePeriod(string $availablePeriodId): self | ||
{ | ||
$path = $this->buildAvailablePeriodsPath(); | ||
$data = ['available_period_id' => $availablePeriodId]; | ||
|
||
return $this->addDeleteRequest($path, $data); | ||
} | ||
|
||
private function buildEventsPath(string $calendarId): string | ||
{ | ||
return sprintf('/%s/calendars/%s/events', Cronofy::API_VERSION, $calendarId); | ||
} | ||
|
||
private function buildAvailablePeriodsPath(): string | ||
{ | ||
return sprintf('/%s/available_periods', Cronofy::API_VERSION); | ||
} | ||
|
||
private function addPostRequest(string $relativeUrl, array $data): self | ||
{ | ||
return $this->addRequest('POST', $relativeUrl, $data); | ||
} | ||
|
||
private function addDeleteRequest(string $relativeUrl, array $data): self | ||
{ | ||
return $this->addRequest('DELETE', $relativeUrl, $data); | ||
} | ||
|
||
private function addRequest(string $method, string $relativeUrl, array $data): self | ||
{ | ||
$this->requests[] = new BatchRequest($method, $relativeUrl, $data); | ||
|
||
return $this; | ||
} | ||
|
||
public function requests(): array | ||
{ | ||
return $this->requests; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cronofy\Batch; | ||
|
||
class BatchRequest | ||
{ | ||
private $method; | ||
private $relativeUrl; | ||
private $data; | ||
|
||
public function __construct(string $method, string $relativeUrl, array $data) | ||
{ | ||
$this->method = $method; | ||
$this->relativeUrl = $relativeUrl; | ||
$this->data = $data; | ||
} | ||
|
||
public function method(): string | ||
{ | ||
return $this->method; | ||
} | ||
|
||
public function relativeUrl(): string | ||
{ | ||
return $this->relativeUrl; | ||
} | ||
|
||
public function data(): array | ||
{ | ||
return $this->data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cronofy\Batch; | ||
|
||
class BatchResponse | ||
{ | ||
private $status; | ||
private $headers; | ||
private $data; | ||
private $request; | ||
|
||
public function __construct(int $status, ?array $headers, ?array $data, BatchRequest $request) | ||
{ | ||
$this->status = $status; | ||
$this->headers = $headers; | ||
$this->data = $data; | ||
$this->request = $request; | ||
} | ||
|
||
public function status(): int | ||
{ | ||
return $this->status; | ||
} | ||
|
||
public function hasSuccessStatus(): bool | ||
{ | ||
$status = $this->status(); | ||
|
||
return $status >= 200 && $status < 300; | ||
} | ||
|
||
public function hasErrorStatus(): bool | ||
{ | ||
return !$this->hasSuccessStatus(); | ||
} | ||
|
||
public function headers(): ?array | ||
{ | ||
return $this->headers; | ||
} | ||
|
||
public function data(): ?array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function request(): BatchRequest | ||
{ | ||
return $this->request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cronofy\Batch; | ||
|
||
class BatchResult | ||
{ | ||
private $responses; | ||
private $errors; | ||
|
||
public function __construct(BatchResponse ...$responses) | ||
{ | ||
$this->responses = $responses; | ||
} | ||
|
||
public function responses(): array | ||
{ | ||
return $this->responses; | ||
} | ||
|
||
public function errors(): array | ||
{ | ||
if ($this->errors === null) { | ||
$this->errors = []; | ||
|
||
foreach ($this->responses as $response) { | ||
if ($response->hasErrorStatus()) { | ||
$this->errors[] = $response; | ||
} | ||
} | ||
} | ||
|
||
return $this->errors; | ||
} | ||
|
||
public function hasErrors(): bool | ||
{ | ||
return count($this->errors()) > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Cronofy\Exception; | ||
|
||
use Cronofy\Batch\BatchResult; | ||
use Exception; | ||
|
||
class PartialBatchFailureException extends Exception | ||
{ | ||
private $result; | ||
|
||
public function __construct(string $message, BatchResult $result) | ||
{ | ||
$this->result = $result; | ||
|
||
parent::__construct($message); | ||
} | ||
|
||
public function result(): BatchResult | ||
{ | ||
return $this->result; | ||
} | ||
} |
Oops, something went wrong.