From 84d7fa06db7990fa6220572f70ef87870f2fe913 Mon Sep 17 00:00:00 2001 From: Timo Janssen Date: Tue, 30 Apr 2019 11:16:20 +0200 Subject: [PATCH] Added support for drafts endpoint --- README.md | 31 +++++++++ src/Wuunder/Api/Config/Config.php | 4 ++ src/Wuunder/Api/Config/DraftConfig.php | 33 ++++++++++ src/Wuunder/Api/DraftsApiResponse.php | 21 ++++++ src/Wuunder/Api/Endpoints/Drafts.php | 89 ++++++++++++++++++++++++++ src/Wuunder/Connector.php | 10 +++ 6 files changed, 188 insertions(+) create mode 100644 src/Wuunder/Api/Config/DraftConfig.php create mode 100644 src/Wuunder/Api/DraftsApiResponse.php create mode 100644 src/Wuunder/Api/Endpoints/Drafts.php diff --git a/README.md b/README.md index 6e7f708..8fa9c1a 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,37 @@ Installation: Set-up connection: `$connector = new Wuunder\Connector("API_KEY");` + +Create Draft bulk booking: +```php +$draftsRequest = $connector->createBulkDrafts(); + +$draftsConfig = new \Wuunder\Api\Config\DraftConfig(); + +$booking1 = new \Wuunder\Api\Config\BookingConfig(); +$booking1->setWebhookUrl("url"); +$booking1->setRedirectUrl("url"); + +$booking2 = new \Wuunder\Api\Config\BookingConfig(); +$booking2->setWebhookUrl("url"); +$booking2->setRedirectUrl("url"); + +$draftsConfig->addBookingConfig(1, $booking1, true); +$draftsConfig->addBookingConfig(2, $booking2, true); + +if ($draftsConfig->validate()) { + $draftsRequest->setConfig($draftsConfig); + + if ($draftsRequest->fire()) { + var_dump($draftsRequest->getDraftsResponse()); + } else { + var_dump($draftsRequest->getDraftsResponse()->getError()); + } +} else { + print("DraftsConfig not valid"); +} +``` + Create booking: ```php $booking = $connector->createBooking(); diff --git a/src/Wuunder/Api/Config/Config.php b/src/Wuunder/Api/Config/Config.php index 2c0a5d9..3072a55 100644 --- a/src/Wuunder/Api/Config/Config.php +++ b/src/Wuunder/Api/Config/Config.php @@ -24,6 +24,10 @@ public function __call($method, $args) $key = $this->_underscore(substr($method, 3)); $this->setFields[$key] = isset($args[0]) ? $args[0] : null; break; + case 'get' : + $key = $this->_underscore(substr($method, 3)); + return isset($this->setFields[$key]) ? $this->setFields[$key] : null; + break; } } diff --git a/src/Wuunder/Api/Config/DraftConfig.php b/src/Wuunder/Api/Config/DraftConfig.php new file mode 100644 index 0000000..e125698 --- /dev/null +++ b/src/Wuunder/Api/Config/DraftConfig.php @@ -0,0 +1,33 @@ +defaultFields = array(); + $this->requiredFields = array( + "drafts", + ); + } + + public function addBookingConfig($unique_id, BookingConfig $bookingConfig, $safeInsert = False) + { + $currentDraftList = $this->getDrafts(); + if (empty($currentDraftList)) { + $currentDraftList = array(); + } else if ($safeInsert) { + foreach ($currentDraftList as $draft) { + if ($draft['id'] == $unique_id) { + return false; + } + } + } + array_push($currentDraftList, array("id" => (string)$unique_id, "draft" => $bookingConfig)); + $this->setDrafts($currentDraftList); + + return true; + } +} diff --git a/src/Wuunder/Api/DraftsApiResponse.php b/src/Wuunder/Api/DraftsApiResponse.php new file mode 100644 index 0000000..1e2f8c1 --- /dev/null +++ b/src/Wuunder/Api/DraftsApiResponse.php @@ -0,0 +1,21 @@ +getBody(); + } +} \ No newline at end of file diff --git a/src/Wuunder/Api/Endpoints/Drafts.php b/src/Wuunder/Api/Endpoints/Drafts.php new file mode 100644 index 0000000..6e508c7 --- /dev/null +++ b/src/Wuunder/Api/Endpoints/Drafts.php @@ -0,0 +1,89 @@ +config = new DraftConfig(); + $this->apiKey = $apiKey; + $this->apiEnvironment = $apiEnvironment; + $this->logger = Helper::getInstance(); + } + + /** + * Set data to send to API + * + * @param DraftConfig $config + * @internal param mixed $data JSON encoded + */ + public function setConfig(DraftConfig $config) + { + $this->config = $config; + } + + /** + * Return BookingConfig object of current booking + * + * @return DraftConfig + */ + public function getConfig() + { + return $this->config; + } + + /** + * Fires the request and handles the result. + * + * @return bool + */ + public function fire() + { + $bookingRequest = new PostRequest($this->apiEnvironment->getStageBaseUrl() . "/drafts", + $this->apiKey->getApiKey(), json_encode($this->config->getDrafts())); + try { + $bookingRequest->send(); + } catch(Exception $e) { + $this->logger->log($e); + } + + $body = null; + $header = null; + $error = null; + + if (isset($bookingRequest->getResponseHeaders()["location"])) { + $header = $bookingRequest->getResponseHeaders(); + } else { + $error = $bookingRequest->getResponse(); + } + $this->draftsResponse = new DraftsApiResponse($header, $body, $error); + + return is_null($error); + } + + /** + * Returns drafts response object + * + * @return mixed + */ + public function getDraftsResponse() + { + return $this->draftsResponse; + } +} diff --git a/src/Wuunder/Connector.php b/src/Wuunder/Connector.php index 3a816c5..f4d2904 100644 --- a/src/Wuunder/Connector.php +++ b/src/Wuunder/Connector.php @@ -2,6 +2,7 @@ namespace Wuunder; +use Wuunder\Api\Endpoints\Drafts; use Wuunder\Api\Key; use Wuunder\Api\Environment; use Wuunder\Api\Endpoints\Booking; @@ -25,6 +26,15 @@ public function __construct($apiKey, $isStaging = true) $this->helper = Helper::getInstance(); } + /** + * Creates a new Booking + * + * @return Drafts + */ + public function createBulkDrafts() { + return new Drafts($this->apiKey, $this->apiEnvironment); + } + /** * Creates a new Booking *