Skip to content

Commit

Permalink
Merge pull request #3 from vimeo/refactor-response
Browse files Browse the repository at this point in the history
Refactor response and request classes
  • Loading branch information
jcmanzo authored Jan 20, 2021
2 parents 2452b95 + a0c4913 commit c90cafc
Show file tree
Hide file tree
Showing 42 changed files with 581 additions and 358 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ test:
psalm:
vendor/bin/psalm

check:
vendor/bin/psalm && vendor/bin/phpunit

# Install

install: install_with_psalm
Expand Down
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</errorLevel>
</MissingConstructor>
<PropertyNotSetInConstructor errorLevel="suppress" />
<ImplementedReturnTypeMismatch errorLevel="suppress" />
<MixedInferredReturnType errorLevel="suppress" />
</issueHandlers>

<mockClasses>
Expand Down
6 changes: 3 additions & 3 deletions psalm_plugins/StringChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class StringChecker extends \Psalm\Plugin
* Checks an expression
*
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr $stmt
* @param \PhpParser\Node\Expr $stmt
* @param Context $context
* @param CodeLocation $code_location
* @param array<string> $suppressed_issues
* @return null|false
*/
public function checkExpression(
StatementsChecker $statements_checker,
PhpParser\Node\Expr $stmt,
\PhpParser\Node\Expr $stmt,
Context $context,
CodeLocation $code_location,
array $suppressed_issues
Expand All @@ -37,7 +37,7 @@ public function checkExpression(

$project_checker = $statements_checker->getFileChecker()->project_checker;
if (Checker\ClassChecker::checkFullyQualifiedClassLikeName(
$statements_checker,
$project_checker,
$fq_class_name,
$code_location,
$suppressed_issues
Expand Down
7 changes: 4 additions & 3 deletions src/ExtendedGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,19 @@ public function fetchSubscription(array $parameters = array())
* Fetch BlueSnap subscription objects by customer or in a time range.
*
* See Message\ExtendedFetchSubscriptionsRequest for more details on fetching
* by customer. See Message\FetchSubscriptionsRequest for more details on
* by customer. See Message\ReportingFetchSubscriptionsRequest for more details on
* fetching by time range. The correct request will be made depending on the
* parameters passed.
*
* @param array $parameters
* @return \Omnipay\BlueSnap\Message\FetchSubscriptionsRequest
*
* @return \Omnipay\BlueSnap\Message\ReportingFetchSubscriptionsRequest
*/
public function fetchSubscriptions(array $parameters = array())
{
if (isset($parameters['customerReference'])) {
/**
* @var \Omnipay\BlueSnap\Message\FetchSubscriptionsRequest
* @var \Omnipay\BlueSnap\Message\ReportingFetchSubscriptionsRequest
*/
return $this->createRequest(
'\Omnipay\BlueSnap\Message\ExtendedFetchSubscriptionsRequest',
Expand Down
29 changes: 17 additions & 12 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,49 +81,54 @@ public function setPassword($value)
/**
* Fetch all transactions in a time range.
*
* See Message\FetchTransactionsRequest for more details.
* See Message\ReportingFetchTransactionsRequest for more details.
*
* @param array $parameters
* @return \Omnipay\BlueSnap\Message\FetchTransactionsRequest
*
* @return \Omnipay\BlueSnap\Message\ReportingFetchTransactionsRequest
*/
public function fetchTransactions(array $parameters = array())
{
/**
* @var \Omnipay\BlueSnap\Message\FetchTransactionsRequest
* @var \Omnipay\BlueSnap\Message\ReportingFetchTransactionsRequest
*/
return $this->createRequest('\Omnipay\BlueSnap\Message\FetchTransactionsRequest', $parameters);
return $this->createRequest('\Omnipay\BlueSnap\Message\ReportingFetchTransactionsRequest', $parameters);
}

/**
* Fetch all active subscriptions in a time range.
*
* See Message\FetchSubscriptionsRequest for more details.
* See Message\ReportingFetchSubscriptionsRequest for more details.
*
* @param array $parameters
* @return \Omnipay\BlueSnap\Message\FetchSubscriptionsRequest
*
* @return \Omnipay\BlueSnap\Message\ReportingFetchSubscriptionsRequest
*/
public function fetchSubscriptions(array $parameters = array())
{
/**
* @var \Omnipay\BlueSnap\Message\FetchSubscriptionsRequest
* @var \Omnipay\BlueSnap\Message\ReportingFetchSubscriptionsRequest
*/
return $this->createRequest('\Omnipay\BlueSnap\Message\FetchSubscriptionsRequest', $parameters);
return $this->createRequest('\Omnipay\BlueSnap\Message\ReportingFetchSubscriptionsRequest', $parameters);
}

/**
* Fetch all canceled subscriptions in a time range.
*
* See Message\FetchCanceledSubscriptionsRequest for more details.
* See Message\ReportingFetchCanceledSubscriptionsRequest for more details.
*
* @param array $parameters
* @return \Omnipay\BlueSnap\Message\FetchCanceledSubscriptionsRequest
*
* @return \Omnipay\BlueSnap\Message\ReportingFetchCanceledSubscriptionsRequest
*/
public function fetchCanceledSubscriptions(array $parameters = array())
{
$class = '\Omnipay\BlueSnap\Message\ReportingFetchCanceledSubscriptionsRequest';

/**
* @var \Omnipay\BlueSnap\Message\FetchCanceledSubscriptionsRequest
* @var \Omnipay\BlueSnap\Message\ReportingFetchCanceledSubscriptionsRequest
*/
return $this->createRequest('\Omnipay\BlueSnap\Message\FetchCanceledSubscriptionsRequest', $parameters);
return $this->createRequest($class, $parameters);
}

/**
Expand Down
18 changes: 10 additions & 8 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
const XMLNS = 'http://ws.plimus.com';

/**
* @var Response
* @var string
*/
protected $response;
protected static $RESPONSE_CLASS = '\Omnipay\BlueSnap\Message\AbstractResponse';

/**
* @var string
* @var AbstractResponse
*/
protected static $RESPONSE_CLASS = '\Omnipay\BlueSnap\Message\Response';
protected $response;


/**
* Gets the username for making API calls
Expand Down Expand Up @@ -305,7 +306,8 @@ protected function validateTimeZone(DateTime $datetime)
* Makes the request
*
* @param SimpleXMLElement|null $data
* @return Response
*
* @return AbstractResponse
*
* @throws RuntimeException if $data is invalid XML
* @throws Exception if there is a problem when initiating the request.
Expand Down Expand Up @@ -391,7 +393,7 @@ function ($event) {
}

/**
* @var Response
* @var AbstractResponse
*/
$this->response = new static::$RESPONSE_CLASS($this, $responseData);

Expand Down Expand Up @@ -425,12 +427,12 @@ public function validate()
/**
* Overriding to provide a more precise return type
*
* @return Response
* @return AbstractResponse
*/
public function send()
{
/**
* @var Response
* @var AbstractResponse
*/
return parent::send();
}
Expand Down
135 changes: 135 additions & 0 deletions src/Message/AbstractResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php


namespace Omnipay\BlueSnap\Message;

use Omnipay\BlueSnap\Transaction;
use Omnipay\Common\Message\RequestInterface;
use SimpleXMLElement;

abstract class AbstractResponse extends \Omnipay\Common\Message\AbstractResponse
{
/**
* HTTP request id
*
* @var string|null
*/
protected $requestId;

/**
* HTTP response code
*
* @var string|null
*/
protected $code;

/**
* @var string|SimpleXMLElement|array
*/
protected $data;

/**
* @var Transaction|null
*/
protected $transaction;

/**
* @param RequestInterface $request the initiating request.
* @param mixed $data
*/
public function __construct(RequestInterface $request, $data)
{
parent::__construct($request, $data);
$this->transaction = null;
}

/**
* Returns true if the request was successful, false otherwise
*
* @return bool
*/
public function isSuccessful()
{
return substr($this->getCode() ?: '', 0, 1) === '2';
}

/**
* Get the ID from the HTTP request
*
* @return string|null
*/
public function getRequestId()
{
return $this->requestId;
}

/**
* Set the ID from the HTTP request
*
* @param string|null $requestId
* @return static
*/
public function setRequestId($requestId)
{
$this->requestId = $requestId;
return $this;
}

/**
* Get the HTTP response code
*
* @return string|null
*/
public function getCode()
{
return $this->code;
}

/**
* Set the HTTP response code
*
* @param string $code
* @return static
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}

/**
* Overriding to provide more specific return type
*
* @return \Omnipay\BlueSnap\Message\AbstractRequest
*/
public function getRequest()
{
/**
* @var \Omnipay\BlueSnap\Message\AbstractRequest
*/
return parent::getRequest();
}

/**
* Get the error message from the response. Returns null if request was successful.
*
* A response error can be returned as XML if no content-type header is set. We also need to account for JSON
* error responses returned as strings.
*
* @return string|null
* @psalm-suppress MixedPropertyFetch because we check the data typing before using.
*/
public function getErrorMessage()
{
if (!$this->isSuccessful()) {
if ($this->data instanceof SimpleXMLElement && isset($this->data->message->description)) {
return (string) $this->data->message->description;
}
if (is_string($this->data)) {
return $this->data;
} // some error responses are plain text instead of XML
}

return null;
}
}
10 changes: 8 additions & 2 deletions src/Message/ExtendedAbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
*/
abstract class ExtendedAbstractRequest extends AbstractRequest
{
/**
* @var string
*/
protected static $RESPONSE_CLASS = '\Omnipay\BlueSnap\Message\ExtendedResponse';


/**
* Gets the gateway's identifier for the store (BlueSnap calls this the store ID)
*
Expand Down Expand Up @@ -84,12 +90,12 @@ public function setStoreParameters($parameters)
/**
* Overriding to provide a more precise return type
*
* @return Response
* @return ExtendedResponse
*/
public function send()
{
/**
* @var Response
* @var ExtendedResponse
*/
return parent::send();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Message/ExtendedFetchSubscriptionsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
/**
* Fetch subscriptions by customer reference.
*
* If you want to fetch all subscriptions in a time range, see FetchSubscriptionsRequest.
* If you want to fetch all canceled subscriptions, see FetchCanceledSubscriptionsRequest.
* If you want to fetch all subscriptions in a time range, see ReportingFetchSubscriptionsRequest.
* If you want to fetch all canceled subscriptions, see ReportingFetchCanceledSubscriptionsRequest.
* If you only want to fetch a single subscription, see ExtendedFetchSubscriptionRequest.
*
* Parameters:
Expand Down
Loading

0 comments on commit c90cafc

Please sign in to comment.