Skip to content

Commit

Permalink
Added support for drafts endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
timoj committed Apr 30, 2019
1 parent 8948def commit 84d7fa0
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/Wuunder/Api/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/Wuunder/Api/Config/DraftConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Wuunder\Api\Config;

class DraftConfig extends Config
{
public function __construct()
{
parent::__construct();
$this->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;
}
}
21 changes: 21 additions & 0 deletions src/Wuunder/Api/DraftsApiResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Wuunder\Api;

class DraftsApiResponse extends ApiResponse {

public function __construct($header, $body, $error)
{
parent::__construct($header, $body, $error);
}

/**
* Returns booking url
*
* @return mixed
*/
public function getBookingUrl()
{
return $this->getBody();
}
}
89 changes: 89 additions & 0 deletions src/Wuunder/Api/Endpoints/Drafts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Wuunder\Api\Endpoints;

use Wuunder\Api\BookingApiResponse;
use Wuunder\Api\Config\BookingConfig;
use Wuunder\Api\Config\DraftConfig;
use Wuunder\Api\DraftsApiResponse;
use Wuunder\Api\Environment;
use Wuunder\Api\Key;
use Wuunder\Http\PostRequest;
use Wuunder\Util\Helper;

class Drafts
{
private $config;
private $apiKey;
private $apiEnvironment;
private $draftsResponse;
private $logger;

public function __construct(Key $apiKey, Environment $apiEnvironment)
{
$this->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;
}
}
10 changes: 10 additions & 0 deletions src/Wuunder/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wuunder;

use Wuunder\Api\Endpoints\Drafts;
use Wuunder\Api\Key;
use Wuunder\Api\Environment;
use Wuunder\Api\Endpoints\Booking;
Expand All @@ -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
*
Expand Down

0 comments on commit 84d7fa0

Please sign in to comment.