Skip to content
Snippets Groups Projects
Commit 0020cd06 authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

Init

parent b5ccf9d6
No related branches found
No related tags found
1 merge request!1Feature/first release
Pipeline #4731 passed
Showing
with 638 additions and 25 deletions
......@@ -9,8 +9,8 @@
"require": {
"php": ">=5.5",
"psr/log": "^1.0.1",
"wpdesk/wp-cache": "dev-master",
"wpdesk/wp-http-client": "dev-master",
"wpdesk/wp-cache": "dev-feature/first-release",
"wpdesk/wp-http-client": "dev-feature/first-release",
"psr/simple-cache": "^1.0"
},
"require-dev": {
......
<?php
namespace WPDesk\ApiClient\Authentication;
class JWTSaasToken implements Token
{
const SHOP_ID_PARAM = 'shop';
const ROLE_PARAM = 'ROLE_SHOP';
/** @var JWTToken */
private $token;
/**
* JWTToken constructor.
* @param string $token
*/
public function __construct(JWTToken $token)
{
$this->token = $token;
}
public function getAuthString()
{
return $this->token->getAuthString();
}
public function isExpired()
{
return $this->token->isExpired();
}
public function isSignatureValid()
{
return $this->token->isSignatureValid();
}
public function __toString()
{
return $this->token->__toString();
}
/**
* If there is shop id in the token
*
* @return bool
*/
public function hasShopId()
{
$info = $this->token->getDecodedPublicTokenInfo();
return !empty($info[self::SHOP_ID_PARAM]) && in_array(self::ROLE_PARAM, $info['roles']);
}
/**
* Get shop id from token
*
* @return int
*/
public function getShopId()
{
$info = $this->token->getDecodedPublicTokenInfo();
return (int)$info[self::SHOP_ID_PARAM];
}
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Authentication;
class JWTToken implements Token
{
const CONSIDER_EXPIRED_WHEN_LESS = 2;
const EXPIRED_IN_SECONDS_PARAM = 'exp';
/** @var string */
private $token;
/**
* JWTToken constructor.
* @param string $token
*/
public function __construct($token)
{
$this->token = $token;
}
/**
* @return string
*/
public function __toString()
{
return $this->token;
}
/**
* Get string to perform authentication
*
* @return string
*/
public function getAuthString()
{
return 'Bearer ' . $this->__toString();
}
/**
* Returns public data from token
*
* @return array
*/
public function getDecodedPublicTokenInfo()
{
$tokenParts = explode('.', $this->__toString());
if (!empty($tokenParts[1])) {
$infoPart = base64_decode($tokenParts[1]);
return json_decode($infoPart, true);
}
return [];
}
/**
* Is token expired or very soon to be expired?
*
* @return bool
*/
public function isExpired()
{
$tokenInfo = $this->getDecodedPublicTokenInfo();
if (!empty($tokenInfo[self::EXPIRED_IN_SECONDS_PARAM])) {
return $tokenInfo[self::EXPIRED_IN_SECONDS_PARAM] - time() < self::CONSIDER_EXPIRED_WHEN_LESS;
}
return true;
}
/**
* Validates token signature
*
* @return bool
*/
public function isSignatureValid()
{
// @TODO
return true;
}
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Authentication;
/**
* Null object pattern
*
* @package WPDesk\SaasPlatformClient\Authentication
*/
class NullToken implements Token
{
public function __construct()
{
}
/**
* @return string
*/
public function __toString()
{
return '';
}
/**
* Get string to perform authentication
*
* @return string
*/
public function getAuthString()
{
return '';
}
/**
* Is token expired or very soon to be expired?
*
* @return bool
*/
public function isExpired()
{
return true;
}
/**
* Validates token signature
*
* @return bool
*/
public function isSignatureValid()
{
return false;
}
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Authentication;
interface Token
{
/**
* Get string to perform authentication
*
* @return string
*/
public function getAuthString();
/**
* Is token expired or very soon to be expired?
*
* @return bool
*/
public function isExpired();
/**
* Validates token signature
*
* @return bool
*/
public function isSignatureValid();
/**
* @return string
*/
public function __toString();
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Client;
use Psr\Log\LoggerInterface;
use WPDesk\ApiClient\Serializer\SerializerOptions;
use WPDesk\HttpClient\HttpClientOptions;
interface ApiClientOptions extends HttpClientOptions, SerializerOptions
{
/**
* @return LoggerInterface
*/
public function getLogger();
/**
* @return string
*/
public function getApiUrl();
/**
* @return array
*/
public function getDefaultRequestHeaders();
/**
* @return bool
*/
public function isCachedClient();
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\ApiClient;
namespace WPDesk\ApiClient\Client;
use Psr\SimpleCache\CacheInterface;
use WPDesk\ApiClient\Cache\CacheDispatcher;
use WPDesk\ApiClient\Cache\CacheItemCreator;
use WPDesk\ApiClient\Cache\CacheItemVerifier;
use WPDesk\ApiClient\HttpClient\HttpClient;
use WPDesk\Cache\CacheDispatcher;
use WPDesk\Cache\CacheItemCreator;
use WPDesk\Cache\CacheItemVerifier;
use WPDesk\HttpClient\HttpClient;
use WPDesk\ApiClient\Request\Request;
use WPDesk\ApiClient\Response\Response;
use WPDesk\ApiClient\Serializer\Serializer;
......
<?php
namespace WPDesk\ApiClient\ApiClient;
namespace WPDesk\ApiClient\Client;
use WPDesk\ApiClient\HttpClient\HttpClient;
use WPDesk\HttpClient\HttpClient;
use WPDesk\ApiClient\Request\Request;
use WPDesk\ApiClient\Response\Response;
use WPDesk\ApiClient\Serializer\Serializer;
......
<?php
namespace WPDesk\ApiClient\ApiClient;
namespace WPDesk\ApiClient\Client;
use WPDesk\ApiClient\Cache\WordpressCache;
use WPDesk\ApiClient\HttpClient\HttpClientFactory;
use WPDesk\ApiClient\PlatformFactoryOptions;
use WPDesk\Cache\WordpressCache;
use WPDesk\HttpClient\HttpClientFactory;
use WPDesk\ApiClient\Serializer\SerializerFactory;
class ClientFactory
{
/**
* @param PlatformFactoryOptions $options
* @param ApiClientOptions $options
* @return Client
*/
public function createClient(PlatformFactoryOptions $options)
public function createClient(ApiClientOptions $options)
{
$httpClientFactory = new HttpClientFactory();
$serializerFactory = new SerializerFactory();
......
<?php
namespace WPDesk\ApiClient\ApiClient;
namespace WPDesk\ApiClient\Client;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use WPDesk\HttpClient\HttpClient;
use WPDesk\HttpClient\HttpClientResponse;
use WPDesk\SaasPlatformClient\Platform;
use WPDesk\SaasPlatformClient\Request\Request;
use WPDesk\SaasPlatformClient\Response\RawResponse;
use WPDesk\SaasPlatformClient\Response\Response;
use WPDesk\SaasPlatformClient\Serializer\Serializer;
use WPDesk\SaasPlatformClient\HttpClient\HttpClientRequestException;
use WPDesk\ApiClient\Request\Request;
use WPDesk\ApiClient\Response\RawResponse;
use WPDesk\ApiClient\Response\Response;
use WPDesk\ApiClient\Serializer\Serializer;
use WPDesk\HttpClient\HttpClientRequestException;
class ClientImplementation implements Client, LoggerAwareInterface
{
......
<?php
namespace WPDesk\SaasPlatformClient\ApiClient;
namespace WPDesk\ApiClient\Client;
use WPDesk\SaasPlatformClient\Cache\CacheInfoResolver;
use WPDesk\SaasPlatformClient\Cache\HowToCache;
use WPDesk\Cache\CacheInfoResolver;
use WPDesk\Cache\HowToCache;
use WPDesk\SaasPlatformClient\Request\AuthRequest;
use WPDesk\SaasPlatformClient\Request\BasicRequest;
use WPDesk\SaasPlatformClient\Request\Request;
......
<?php
namespace WPDesk\ApiClient\Response;
use WPDesk\ApiClient\Response\Traits\AuthApiResponseDecorator;
class AuthApiResponse implements ApiResponse
{
const RESPONSE_CODE_BAD_CREDENTIALS = 401;
const RESPONSE_CODE_NOT_EXISTS = 404;
use AuthApiResponseDecorator;
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Response\Traits;
use WPDesk\ApiClient\Response\AuthApiResponse;
use WPDesk\ApiClient\Response\RawResponse;
use WPDesk\ApiClient\Response\Response;
trait ApiResponseDecorator
{
/** @var RawResponse */
private $rawResponse;
/**
* RawResponseDecorator constructor.
* @param Response $rawResponse
*/
public function __construct(Response $rawResponse)
{
$this->rawResponse = $rawResponse;
}
/**
* Returns response http code
*
* @return int
*/
public function getResponseCode()
{
return $this->rawResponse->getResponseCode();
}
/**
* Returns response body as array
*
* @return array
*/
public function getResponseBody()
{
return $this->rawResponse->getResponseBody();
}
/**
* Returns response body as array
*
* @return array
*/
public function getResponseErrorBody()
{
return $this->rawResponse->getResponseErrorBody();
}
/**
* Returns response body as array
*
* @return array
*/
public function getResponseHeaders()
{
return $this->rawResponse->getResponseHeaders();
}
/**
* Get links structure to the other request
*
* @return array
*/
public function getLinks()
{
$body = $this->getResponseBody();
return $body['_links'];
}
/**
* Is it a BAD REQUEST response
*
* @return bool
*/
public function isBadRequest()
{
return $this->getResponseCode() === RawResponse::RESPONSE_CODE_ERROR_BAD_REQUEST;
}
/**
* Is it a DOMAIN NOT ALLOWED response
*
* @return bool
*/
public function isDomainNotAllowed()
{
return $this->getResponseCode() === RawResponse::RESPONSE_CODE_DOMAIN_NOT_ALLOWED;
}
/**
* Is it a FATAL ERROR response
*
* @return bool
*/
public function isServerFatalError()
{
return $this->getResponseCode() === RawResponse::RESPONSE_CODE_ERROR_FATAL;
}
/**
* Is any error occured
*
* @return bool
*/
public function isError()
{
return $this->rawResponse->isError();
}
/**
* Is requested resource exists
*
* @return bool
*/
public function isNotExists()
{
return $this->getResponseCode() === AuthApiResponse::RESPONSE_CODE_NOT_EXISTS;
}
/**
* Is maintenance.
*
* @return bool
*/
public function isMaintenance()
{
return $this->rawResponse->isMaintenance();
}
/**
* Get platform version hash string.
*
* @return bool|string
*/
public function getPlatformVersionHash()
{
return $this->rawResponse->getPlatformVersionHash();
}
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Response\Traits;
use WPDesk\ApiClient\Response\AuthApiResponse;
trait AuthApiResponseDecorator
{
use ApiResponseDecorator;
/**
* @return bool
*/
public function isBadCredentials()
{
return $this->getResponseCode() === AuthApiResponse::RESPONSE_CODE_BAD_CREDENTIALS;
}
/**
* Is bad credential because token expires
*
* @return bool
*/
public function isTokenExpired()
{
return $this->isBadCredentials();
}
/**
* Is bad credential because token is invalid
*
* @return bool
*/
public function isTokenInvalid()
{
return $this->isBadCredentials();
}
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Response\Traits;
trait PagedListImplementation
{
/*
* @return array
*/
public function getRawPage()
{
$body = $this->getResponseBody();
if ($body['_embedded'] !== null && $body['_embedded']['item'] !== null) {
return $body['_embedded']['item'];
}
return [];
}
/**
* @return int
*/
public function getPageCount()
{
return (int)floor($this->getItemCount() / $this->getItemsPerPage());
}
/**
* @return int
*/
public function getItemsPerPage()
{
$body = $this->getResponseBody();
return (int)$body['itemsPerPage'];
}
/**
* @return int
*/
public function getItemCount()
{
$body = $this->getResponseBody();
return (int)$body['totalItems'];
}
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Serializer\Exception;
/**
* Thrown when serializer cannot unserialize string data
*
* @package WPDesk\ApiClient\Serializer\Exception
*/
class CannotUnserializeException extends \RuntimeException
{
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Serializer;
use WPDesk\ApiClient\Serializer\Exception\CannotUnserializeException;
class JsonSerializer implements Serializer
{
/**
* Convert data to string
*
* @param mixed $data
* @return string
*/
public function serialize($data)
{
return json_encode($data, JSON_FORCE_OBJECT);
}
/**
* Convert string to php data
*
* @param string $data
* @return mixed
*/
public function unserialize($data)
{
$unserializedResult = json_decode($data, true);
if ($unserializedResult === null) {
throw new CannotUnserializeException("Cannot unserialize data: {$data}");
}
return $unserializedResult;
}
/**
* @return string
*/
public function getMime()
{
return 'application/json';
}
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Serializer;
interface Serializer
{
/**
* @param mixed $data
* @return string
*/
public function serialize($data);
/**
* @param string $data
* @return mixed
*/
public function unserialize($data);
/**
* @return string
*/
public function getMime();
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Serializer;
class SerializerFactory
{
/**
* @param SerializerOptions $options
* @return Serializer
*/
public function createSerializer(SerializerOptions $options)
{
$className = $options->getSerializerClass();
return new $className;
}
}
\ No newline at end of file
<?php
namespace WPDesk\ApiClient\Serializer;
interface SerializerOptions
{
/**
* @return string
*/
public function getSerializerClass();
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment