Skip to content

Commit

Permalink
Use PSR-18 (mailgun#522)
Browse files Browse the repository at this point in the history
* Use PSR-18

* minor fixes

* phpstan fixes

* cs
  • Loading branch information
Nyholm authored Feb 2, 2019
1 parent 398989d commit 6954b4d
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 89 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"description": "The Mailgun SDK provides methods for all API functions.",
"require": {
"php": "^7.1",
"php-http/httplug": "^1.0 || ^2.0",
"psr/http-client": "^1.0",
"php-http/multipart-stream-builder": "^1.0",
"php-http/client-common": "^1.9",
"php-http/discovery": "^1.0",
"php-http/client-common": "^1.9 || ^2.0",
"php-http/discovery": "^1.6",
"webmozart/assert": "^1.2"
},
"require-dev": {
Expand Down
8 changes: 5 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
parameters:
level: 5
paths:
- src
level: 5
paths:
- src

excludes_analyse:
- %currentWorkingDirectory%/src/HttpClient/Plugin/HistoryTrait.php
16 changes: 8 additions & 8 deletions src/Api/HttpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

namespace Mailgun\Api;

use Http\Client\Exception as HttplugException;
use Http\Client\HttpClient;
use Mailgun\Exception\UnknownErrorException;
use Mailgun\Hydrator\Hydrator;
use Mailgun\Hydrator\NoopHydrator;
use Mailgun\Exception\HttpClientException;
use Mailgun\Exception\HttpServerException;
use Mailgun\HttpClient\RequestBuilder;
use Psr\Http\Client as Psr18;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface;

/**
Expand All @@ -29,7 +29,7 @@ abstract class HttpApi
/**
* The HTTP client.
*
* @var HttpClient
* @var ClientInterface
*/
protected $httpClient;

Expand All @@ -43,7 +43,7 @@ abstract class HttpApi
*/
protected $requestBuilder;

public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator)
public function __construct(ClientInterface $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator)
{
$this->httpClient = $httpClient;
$this->requestBuilder = $requestBuilder;
Expand Down Expand Up @@ -113,7 +113,7 @@ protected function httpGet(string $path, array $parameters = [], array $requestH
$response = $this->httpClient->sendRequest(
$this->requestBuilder->create('GET', $path, $requestHeaders)
);
} catch (HttplugException\NetworkException $e) {
} catch (Psr18\NetworkExceptionInterface $e) {
throw HttpServerException::networkError($e);
}

Expand Down Expand Up @@ -145,7 +145,7 @@ protected function httpPostRaw(string $path, $body, array $requestHeaders = []):
$response = $this->httpClient->sendRequest(
$this->requestBuilder->create('POST', $path, $requestHeaders, $body)
);
} catch (HttplugException\NetworkException $e) {
} catch (Psr18\NetworkExceptionInterface $e) {
throw HttpServerException::networkError($e);
}

Expand All @@ -165,7 +165,7 @@ protected function httpPut(string $path, array $parameters = [], array $requestH
$response = $this->httpClient->sendRequest(
$this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createRequestBody($parameters))
);
} catch (HttplugException\NetworkException $e) {
} catch (Psr18\NetworkExceptionInterface $e) {
throw HttpServerException::networkError($e);
}

Expand All @@ -185,7 +185,7 @@ protected function httpDelete(string $path, array $parameters = [], array $reque
$response = $this->httpClient->sendRequest(
$this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createRequestBody($parameters))
);
} catch (HttplugException\NetworkException $e) {
} catch (Psr18\NetworkExceptionInterface $e) {
throw HttpServerException::networkError($e);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Api/Suppression.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace Mailgun\Api;

use Http\Client\HttpClient;
use Mailgun\Api\Suppression\Bounce;
use Mailgun\Api\Suppression\Complaint;
use Mailgun\Api\Suppression\Unsubscribe;
use Mailgun\Hydrator\Hydrator;
use Psr\Http\Client\ClientInterface;
use Mailgun\HttpClient\RequestBuilder;

/**
Expand All @@ -26,7 +26,7 @@
class Suppression
{
/**
* @var HttpClient
* @var ClientInterface
*/
private $httpClient;

Expand All @@ -40,7 +40,7 @@ class Suppression
*/
private $hydrator;

public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator)
public function __construct(ClientInterface $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator)
{
$this->httpClient = $httpClient;
$this->requestBuilder = $requestBuilder;
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Mailgun\Api;

use Http\Client\HttpClient;
use Mailgun\Assert;
use Mailgun\Hydrator\Hydrator;
use Mailgun\Model\Webhook\CreateResponse;
Expand All @@ -21,6 +20,7 @@
use Mailgun\Model\Webhook\UpdateResponse;
use Mailgun\HttpClient\RequestBuilder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Client\ClientInterface;

/**
* @author Tobias Nyholm <[email protected]>
Expand All @@ -32,7 +32,7 @@ class Webhook extends HttpApi
*/
private $apiKey;

public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator, string $apiKey)
public function __construct(ClientInterface $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator, string $apiKey)
{
parent::__construct($httpClient, $requestBuilder, $hydrator);
$this->apiKey = $apiKey;
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/HttpServerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function serverError(int $httpStatus = 500)
return new self('An unexpected error occurred at Mailgun\'s servers. Try again later and contact support if the error still exists.', $httpStatus);
}

public static function networkError(\Exception $previous)
public static function networkError(\Throwable $previous)
{
return new self('Mailgun\'s servers are currently unreachable.', 0, $previous);
}
Expand Down
12 changes: 6 additions & 6 deletions src/HttpClient/HttpClientConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

namespace Mailgun\HttpClient;

use Http\Client\HttpClient;
use Http\Client\Common\PluginClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Http\Message\UriFactory;
use Http\Client\Common\Plugin;
use Mailgun\HttpClient\Plugin\History;
use Mailgun\HttpClient\Plugin\ReplaceUriPlugin;
use Psr\Http\Client\ClientInterface;

/**
* Configure a HTTP client.
Expand Down Expand Up @@ -50,7 +50,7 @@ final class HttpClientConfigurator
private $uriFactory;

/**
* @var HttpClient
* @var ClientInterface
*/
private $httpClient;

Expand Down Expand Up @@ -124,16 +124,16 @@ public function setUriFactory(UriFactory $uriFactory): self
return $this;
}

private function getHttpClient(): HttpClient
private function getHttpClient(): ClientInterface
{
if (null === $this->httpClient) {
$this->httpClient = HttpClientDiscovery::find();
$this->httpClient = Psr18ClientDiscovery::find();
}

return $this->httpClient;
}

public function setHttpClient(HttpClient $httpClient): self
public function setHttpClient(ClientInterface $httpClient): self
{
$this->httpClient = $httpClient;

Expand Down
6 changes: 1 addition & 5 deletions src/HttpClient/Plugin/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Mailgun\HttpClient\Plugin;

use Http\Client\Common\Plugin\Journal;
use Http\Client\Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -23,6 +22,7 @@
*/
final class History implements Journal
{
use HistoryTrait;
/**
* @var ResponseInterface
*/
Expand All @@ -40,8 +40,4 @@ public function addSuccess(RequestInterface $request, ResponseInterface $respons
{
$this->lastResponse = $response;
}

public function addFailure(RequestInterface $request, Exception $exception)
{
}
}
41 changes: 41 additions & 0 deletions src/HttpClient/Plugin/HistoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace Mailgun\HttpClient\Plugin;

use Http\Client\Exception;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;

/*
* Below is a some code to make the History plugin compatible with both 1.x and 2.x of php-client/client-common
*/
if (\class_exists(\Http\Client\Common\HttpMethodsClientInterface::class)) {
/**
* @internal code for php-http/client-common:2.x
*/
trait HistoryTrait
{
public function addFailure(RequestInterface $request, ClientExceptionInterface $exception)
{
}
}
} else {
/**
* @internal code for php-http/client-common:1.x
*/
trait HistoryTrait
{
public function addFailure(RequestInterface $request, Exception $exception)
{
}
}
}
7 changes: 3 additions & 4 deletions src/HttpClient/Plugin/ReplaceUriPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
final class ReplaceUriPlugin implements Plugin
{
use Plugin\VersionBridgePlugin;

/**
* @var UriInterface
*/
Expand All @@ -32,10 +34,7 @@ public function __construct(UriInterface $uri)
$this->uri = $uri;
}

/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
public function doHandleRequest(RequestInterface $request, callable $next, callable $first)
{
$request = $request->withUri($this->uri);

Expand Down
Loading

0 comments on commit 6954b4d

Please sign in to comment.