Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ abstract class Api extends Extensions\Strict
/** @var Extensions\ISettings */
protected $settings;

public function __construct($action, Extensions\Api\IRequest $data, Extensions\Database\IDatabase $database, Extensions\ISettings $settings)
{
public function __construct(
$action,
Extensions\Api\IRequest $data,
Extensions\Database\IDatabase $database,
Extensions\ISettings $settings
) {
$this->database = $database;
$this->settings = $settings;

$method = 'action'.ucfirst($action);

if(method_exists($this, $method))
{
call_user_func_array(array($this, $method), array($data));
}
else
{
if (method_exists($this, $method)) {
call_user_func_array([$this, $method], [$data]);
} else {
throw new ConnectionException('Not Found', 404);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function __construct(Extensions\ISettings $settings)

public function authenticate($application_id, $application_token)
{
if($this->settings->load("static:application_id") === $application_id && $this->settings->load("static:application_token") === $application_token)
{
if ($this->settings->load("static:application_id") === $application_id &&
$this->settings->load("static:application_token") === $application_token) {
return true;
}
throw new ConnectionException('Unauthorized', 401);
Expand Down
44 changes: 18 additions & 26 deletions src/Api/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
namespace BulkGate\Extensions\Api;

use BulkGate\Extensions;
use stdClass;

/**
* @author Lukáš Piják 2018 TOPefekt s.r.o.
* @link https://www.bulkgate.com/
*/
class Request extends \stdClass implements IRequest
class Request extends stdClass implements IRequest
{
/** @var array */
private $data = array();
private $data = [];

/** @var Extensions\Headers */
private $headers;

public function __construct(Extensions\Headers $headers)
{
if(!isset($_SERVER['REQUEST_METHOD']) || (isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) !== 'post'))
{
throw new ConnectionException("Method Not Allowed", 405);
if (!isset($_SERVER['REQUEST_METHOD']) || (isset($_SERVER['REQUEST_METHOD']) &&
strtolower($_SERVER['REQUEST_METHOD']) !== 'post')) {
throw new ConnectionException('Method Not Allowed', 405);
}

$this->headers = $headers;
Expand All @@ -28,38 +29,29 @@ public function __construct(Extensions\Headers $headers)

$data = file_get_contents('php://input');

if(is_string($data))
{
if($content_type === 'application/json')
{
try
{
if (is_string($data)) {
if ($content_type === 'application/json') {
try {
$this->data = Extensions\Json::decode($data, Extensions\Json::FORCE_ARRAY);
}
catch (Extensions\JsonException $e)
{
} catch (Extensions\JsonException $e) {
throw new ConnectionException('Bad Request', 400);
}
}
elseif($content_type === 'application/zip')
{
$this->data = Extensions\Json::decode(Extensions\Compress::decompress($data), Extensions\Json::FORCE_ARRAY);
}
else
{
} elseif ($content_type === 'application/zip') {
$this->data = Extensions\Json::decode(
Extensions\Compress::decompress($data),
Extensions\Json::FORCE_ARRAY
);
} else {
throw new ConnectionException('Bad Request', 400);
}
}
else
{
} else {
throw new ConnectionException('Bad Request', 400);
}
}

public function __get($name)
{
if(isset($this->data[$name]))
{
if (isset($this->data[$name])) {
return $this->data[$name];
}
return null;
Expand Down
7 changes: 2 additions & 5 deletions src/Api/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ public function send()
{
header("Content-Type: {$this->contentType}; charset=utf-8");

if($this->contentType === 'application/zip')
{
if ($this->contentType === 'application/zip') {
echo BulkGate\Extensions\Compress::compress(BulkGate\Extensions\Json::encode($this->payload));
}
else
{
} else {
echo BulkGate\Extensions\Json::encode($this->payload);
}
}
Expand Down
28 changes: 24 additions & 4 deletions src/Api/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,30 @@
* @author Lukáš Piják 2018 TOPefekt s.r.o.
* @link https://www.bulkgate.com/
*/
class ConnectionException extends Extensions\Exception {}
class ConnectionException extends Extensions\Exception
{
}

class InvalidRequestException extends ConnectionException {}
/**
* Class InvalidRequestException
* @package BulkGate\Extensions\Api
*/
class InvalidRequestException extends ConnectionException
{
}

class UnknownActionException extends ConnectionException {}
/**
* Class UnknownActionException
* @package BulkGate\Extensions\Api
*/
class UnknownActionException extends ConnectionException
{
}

class MethodNotAllowedException extends ConnectionException {}
/**
* Class MethodNotAllowedException
* @package BulkGate\Extensions\Api
*/
class MethodNotAllowedException extends ConnectionException
{
}
13 changes: 7 additions & 6 deletions src/Buffer.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
<?php
namespace BulkGate\Extensions;

use stdClass;

/**
* @author Lukáš Piják 2018 TOPefekt s.r.o.
* @link https://www.bulkgate.com/
*/
class Buffer extends \stdClass
class Buffer extends stdClass
{
/** @var array */
private $buffer = array();
private $buffer = [];

/** @var mixed */
private $default_value = null;
private $default_value;

public function __construct(array $array = array(), $default_value = null)
public function __construct(array $array = [], $default_value = null)
{
$this->buffer = $array;
$this->default_value = $default_value;
}

public function __get($name)
{
if(isset($this->buffer[$name]))
{
if (isset($this->buffer[$name])) {
return $this->buffer[$name];
}
return $this->default_value;
Expand Down
7 changes: 3 additions & 4 deletions src/Compress.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
*/
class Compress
{
static public function compress($data, $encoding_mode = 9)
public static function compress($data, $encoding_mode = 9)
{
return base64_encode(gzencode(serialize($data), $encoding_mode));
}

static public function decompress($data)
public static function decompress($data)
{
if($data)
{
if ($data) {
return unserialize(gzinflate(substr(base64_decode($data), 10, -8)));
}
return false;
Expand Down
Loading