-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
188 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
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,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; | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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