Skip to content

Commit

Permalink
Merge pull request #3 from dldl/more-tests
Browse files Browse the repository at this point in the history
WIP: More tests !
  • Loading branch information
quentinus95 authored Dec 4, 2016
2 parents 3b11fc7 + ed66923 commit 0f7d1de
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 92 deletions.
8 changes: 4 additions & 4 deletions AbstractAdapter.php → AbstractConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use dLdL\WebService\Adapter\ParameterBagInterface;
use Psr\Log\LoggerInterface;

abstract class AbstractAdapter implements AdapterInterface
abstract class AbstractConnector implements ConnectorInterface
{
/**
* @var null|CacheHelperInterface
Expand All @@ -39,11 +39,11 @@ abstract protected function handleRequest(Request $request);
public function sendRequest(Request $request)
{
if (!$this->isConnected() || $this->getHost() === null) {
throw new ConnectionException($this->currentAdapter().' must be connected before sending data.');
throw new ConnectionException($this->currentConnector().' must be connected before sending data.');
}

if (!$this->supportsMethod($request->getMethod())) {
throw new RequestException('Method '.$request->getMethod().' is not supported by '.$this->currentAdapter().'.');
throw new RequestException('Method '.$request->getMethod().' is not supported by '.$this->currentConnector().'.');
}

if ($this->hasCache() && $this->getCache()->getPool()->hasItem($request)) {
Expand Down Expand Up @@ -93,7 +93,7 @@ private function saveToCache(Request $request, $response)
$this->log->cacheAdd($this, $request, (new \ReflectionClass($this->getCache()))->getShortName(), $duration);
}

private function currentAdapter()
private function currentConnector()
{
return (new \ReflectionClass($this))->getShortName();
}
Expand Down
22 changes: 11 additions & 11 deletions Adapter/LoggerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace dLdL\WebService\Adapter;

use dLdL\WebService\AdapterInterface;
use dLdL\WebService\ConnectorInterface;
use dLdL\WebService\Http\Request;
use Psr\Log\LoggerInterface;

Expand All @@ -15,20 +15,20 @@ public function __construct(LoggerInterface $logger)
$this->logger = $logger;
}

public function request(AdapterInterface $adapter, Request $request)
public function request(ConnectorInterface $connector, Request $request)
{
$this->logger->info(
sprintf('Sending request to %s%s using %s.',
$adapter->getHost(), $request->getUrl(), $this->className($adapter)
$connector->getHost(), $request->getUrl(), $this->className($connector)
),
$request->getParameters()
);
}

public function response(AdapterInterface $adapter, $response, Request $request)
public function response(ConnectorInterface $connector, $response, Request $request)
{
$this->logger->debug(
sprintf('Response trace for %s%s.', $adapter->getHost(), $request->getUrl()),
sprintf('Response trace for %s%s.', $connector->getHost(), $request->getUrl()),
[$response]
);
}
Expand All @@ -51,29 +51,29 @@ public function cacheAdd($host, Request $request, $cacheClass, $time)
);
}

public function connectionFailure(AdapterInterface $adapter, Request $request)
public function connectionFailure(ConnectorInterface $connector, Request $request)
{
$this->logger->error(
sprintf('Failed to connect to %s%s using %s.',
$adapter->getHost(), $request->getUrl(), $this->className($adapter)
$connector->getHost(), $request->getUrl(), $this->className($connector)
),
$request->getParameters()
);
}

public function requestFailure(AdapterInterface $adapter, Request $request, $exceptionMessage)
public function requestFailure(ConnectorInterface $connector, Request $request, $exceptionMessage)
{
$this->logger->error(
sprintf(
'Failed to send request to %s%s using %s. Exception message : %s',
$adapter->getHost(), $request->getUrl(), $this->className($adapter), $exceptionMessage
$connector->getHost(), $request->getUrl(), $this->className($connector), $exceptionMessage
),
$request->getParameters()
);
}

private function className(AdapterInterface $adapter)
private function className(ConnectorInterface $connector)
{
return (new \ReflectionClass($adapter))->getShortName();
return (new \ReflectionClass($connector))->getShortName();
}
}
2 changes: 1 addition & 1 deletion Adapter/ParameterBagInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use dLdL\WebService\Exception\UndefinedParameterException;

/**
* ParameterBag that can be used to define parameters (used by adapters and cache system).
* ParameterBag that can be used to define parameters (used by connectors and adapters such as cache system).
*/
interface ParameterBagInterface
{
Expand Down
24 changes: 12 additions & 12 deletions AdapterInterface.php → ConnectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
use dLdL\WebService\Http\Request;

/**
* AdapterInterface defines basic rules to connect to a WebService.
* ConnectorInterface defines rules to connect to a WebService.
*/
interface AdapterInterface
interface ConnectorInterface
{
/**
* Initialize the adapter.
* Initialize the connector and the connection to the host if needed.
*
* @param string $host Host of the webservice
*
Expand All @@ -23,30 +23,30 @@ interface AdapterInterface
public function connect($host);

/**
* Get the current adapter host.
* Get the current connector host.
*
* @throws ConnectionException if adapter is not connected
* @throws ConnectionException if connector is not connected and initialized
*
* @see AdapterInterface::isConnected() to check if adapter is connected
* @see ConnectorInterface::isConnected() to check if connector is connected
*
* @return string The current hostname
*/
public function getHost();

/**
* Check if the adapter is initialized.
* Check if the connector is initialized and connected if needed.
*
* @return bool true if this adapter can handle requests
* @return bool true if this connector can handle requests
*/
public function isConnected();

/**
* Disconnect the adapter and free resources.
* Disconnect the connector and free resources.
*/
public function disconnect();

/**
* Check if the interface supports a specific request method.
* Check if the connector supports a specific request method.
*
* @param string $method The method to be checked, for instance : 'GET', 'POST', ...
*
Expand All @@ -64,7 +64,7 @@ public function supportsMethod($method);
public function sendRequest(Request $request);

/**
* Get the parameters defined for the adapter.
* Get the defined parameters of the connector.
*
* @return ParameterBag The ParameterBag with defined parameters
*/
Expand All @@ -78,7 +78,7 @@ public function getParameters();
public function setCache(CacheHelperInterface $cache = null);

/**
* Check if a cache handler is defined for the adapter.
* Check if a cache handler is defined for the parameters.
*
* @return bool
*/
Expand Down
2 changes: 1 addition & 1 deletion Exception/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class RequestException extends WebServiceException
{
protected $message = 'Request cannot be handled by the adapter.';
protected $message = 'Request cannot be handled by the connector.';

public function __construct($message = null)
{
Expand Down
4 changes: 2 additions & 2 deletions ParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace dLdL\WebService;

/**
* ParserInterface can be used to parse a raw response from an adapter.
* ParserInterface can be used to parse a raw response from a connector.
*/
interface ParserInterface
{
/**
* This method can be used to parse a raw response from an adapter and convert
* This method can be used to parse a raw response from a connector and convert
* it to a business object.
*
* @param mixed $response Response to parse
Expand Down
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
dLdLWebService
==============

[![Build Status](https://travis-ci.org/dldl/webservice.svg?branch=master)](https://travis-ci.org/dldl/webservice)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/d5e04165-7382-4cfa-aa34-8860f96af5ab/mini.png)](https://insight.sensiolabs.com/projects/d5e04165-7382-4cfa-aa34-8860f96af5ab)
[![Latest Stable Version](https://poser.pugx.org/dldl/webservice/v/stable)](https://packagist.org/packages/dldl/webservice)
![Licence](https://img.shields.io/github/license/dldl/webservice.svg)

**This library is still under heavy development.**

This PHP library allows you to follow a normalized way to connect to your WebServices, with logs and cache following
PSR-3 and PSR-6.

[![Build Status](https://travis-ci.org/dldl/webservice.svg?branch=master)](https://travis-ci.org/dldl/webservice)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/d5e04165-7382-4cfa-aa34-8860f96af5ab/mini.png)](https://insight.sensiolabs.com/projects/d5e04165-7382-4cfa-aa34-8860f96af5ab)
[![Latest Stable Version](https://poser.pugx.org/dldl/webservice/v/stable)](https://packagist.org/packages/dldl/webservice)
![Licence](https://img.shields.io/github/license/dldl/webservice.svg)

Installation
------------

Expand All @@ -31,47 +31,47 @@ Here is a simple commented example to see how it can be used:
namespace MyApp\WebService;

use dLdL\WebService\WebServiceInterface;
use dLdL\WebService\AdapterInterface;
use dLdL\WebService\ConnectorInterface;
use dLdL\WebService\ParserInterface;
use dLdL\WebService\Exception\WebServiceException;
use dLdL\WebService\Http\Request;

class FacebookWebService implements WebServiceInterface
{
private $adapter;
private $connector;
private $host;
private $parser;

public function __construct(AdapterInterface $adapter, ParserInterface $parser, $host)
public function __construct(ConnectorInterface $connector, ParserInterface $parser, $host)
{
$this->adapter = $adapter;
$this->connector = $connector;
$this->host = $host;
$this->parser = $parser;
}

public function getAdapter()
public function getConnector()
{
return $this->adapter;
return $this->connector;
}

public function getPosts($facebookUsername)
{
try {
$this->getAdapter()->connect($this->host);
$this->getConnector()->connect($this->host);
} catch (WebServiceException $e) {
return [];
}

$request = new Request($facebookUsername . '/feed');
$this->getAdapter()->getCache()->getConfig()->add($request, 60*60*24);
$this->getConnector()->getCache()->getConfig()->add($request, 60*60*24);

try {
$postData = $this->getAdapter()->sendRequest($request);
$postData = $this->getConnector()->sendRequest($request);
} catch (WebServiceException $e) {
return [];
}

$this->getAdapter()->disconnect();
$this->getConnector()->disconnect();

return $this->parser->parse($postData);
}
Expand All @@ -80,16 +80,16 @@ class FacebookWebService implements WebServiceInterface

The main idea is to split the WebService requests into three parts:

- *Adapters*, in charge to connect to the WebService and to grab and/or send raw data from a predefined request
- *Connectors*, in charge to connect to the WebService and to grab and/or send raw data from a predefined request
- *Parsers*, in charge to transform this raw data to business objects
- *WebServices*, in charge to check business conditions calling services but also to delegate the call to *adapters*
- *WebServices*, in charge to check business conditions calling services but also to delegate the call to *connectors*
and to call *parsers*

This allow to separate the way data is retrieve from the way data is aim to be used. It can be easy to cache, log,
add extensions such as proxies and change the type of adapter at any moment (for example, to move from a SOAP to a
add extensions such as proxies and change the type of connector at any moment (for example, to move from a SOAP to a
REST WebService).

Adapters must implement the `AdapterInterface`. The easiest way is to extend the `AbstractAdapter` class. Adapters
Connectors must implement the `ConnectorInterface`. The easiest way is to extend the `AbstractConnector` class. Connectors
can use any technology such as `cURL`, `Guzzle`, `Soap` or any specific library but must be independent to the data
it handles.

Expand Down
Loading

0 comments on commit 0f7d1de

Please sign in to comment.