Skip to content

Commit

Permalink
php 8.0+ fixes, phpcs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Oct 18, 2023
1 parent 75d507f commit 1711327
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 114 deletions.
2 changes: 1 addition & 1 deletion api_tools.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:

api_tools.rest.api_manager:
class: Drupal\api_tools\Rest\ApiManager
arguments: ['@http_client', '@logger.channel.api_tools']
arguments: ['@http_client']

api_tools.rest.request_factory:
class: Drupal\api_tools\Rest\RequestFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

namespace Drupal\api_tools_example\Plugin\RestApiRequest;

use Drupal\Core\Url;
use Drupal\api_tools\Request\Request;
use Drupal\api_tools\Rest\ApiRequestBase;
use Drupal\api_tools_example\Mock\ResponseEntity;
use Drupal\api_tools_example\Response\ExampleResponse;
use Generator;
use Drupal\Core\Url;
use League\Uri\Uri;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -54,7 +53,7 @@ public function getExampleData() : ExampleResponse {
* @yield \Drupal\api_tools_example\Response\ExampleResponse
* The response.
*/
public function getMultipleExampleData(int $num) : Generator {
public function getMultipleExampleData(int $num) : \Generator {
$requests = [];

for ($i = 0; $i < $num; $i++) {
Expand Down
14 changes: 2 additions & 12 deletions src/Exception/ErrorResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@
namespace Drupal\api_tools\Exception;

use Drupal\api_tools\Response\ErrorResponse;
use Throwable;

/**
* An exception for ErrorResponse responses.
*/
final class ErrorResponseException extends \Exception {

/**
* The response.
*
* @var \Drupal\api_tools\Response\ErrorResponse
*/
private $response;

/**
* Constructs a new instance.
*
Expand All @@ -27,9 +19,7 @@ final class ErrorResponseException extends \Exception {
* @param \Throwable|null $previous
* The throwable.
*/
public function __construct(ErrorResponse $response, Throwable $previous = NULL) {
$this->response = $response;

public function __construct(private ErrorResponse $response, \Throwable $previous = NULL) {
$message = '';

if ($response->getResponseDebug()) {
Expand All @@ -44,7 +34,7 @@ public function __construct(ErrorResponse $response, Throwable $previous = NULL)
* @return \Drupal\api_tools\Response\ErrorResponse
* The response.
*/
public function getResposne(): ErrorResponse {
public function getResponse(): ErrorResponse {
return $this->response;
}

Expand Down
32 changes: 5 additions & 27 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,6 @@
*/
class Request {

/**
* The method.
*
* @var string
*/
protected string $method = 'GET';

/**
* The uri.
*
* @var \League\Uri\Contracts\UriInterface
*/
protected UriInterface $uri;

/**
* The request options.
*
* @var array
*
* @see \GuzzleHttp\Client::requestAsync()
*/
protected array $options = [];

/**
* Constructs a new instance.
*
Expand All @@ -44,10 +21,11 @@ class Request {
* @param array $options
* The request options.
*/
public function __construct(UriInterface $uri, string $method = 'GET', array $options = []) {
$this->uri = $uri;
$this->method = $method;
$this->options = $options;
public function __construct(
private UriInterface $uri,
private string $method = 'GET',
private array $options = []
) {
}

/**
Expand Down
24 changes: 4 additions & 20 deletions src/Response/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,6 @@
*/
final class Debug {

/**
* The response description.
*
* @var string
*/
protected string $description;

/**
* The improvement instructions.
*
* @var string[]
*/
protected array $instructions = [];

/**
* Constructs a new instance.
*
Expand All @@ -32,11 +18,9 @@ final class Debug {
* The improvement instructions.
*/
public function __construct(
string $description,
array $instructions = []
private string $description,
private array $instructions = []
) {
$this->description = $description;
$this->instructions = $instructions;
}

/**
Expand All @@ -50,12 +34,12 @@ public function getDescription() {
}

/**
* Gets the improvement instructoins.
* Gets the improvement instructions.
*
* @return string[]
* The improvement instructions.
*/
public function getInstructions() {
public function getInstructions() : array {
return $this->instructions;
}

Expand Down
28 changes: 4 additions & 24 deletions src/Rest/ApiManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,27 @@

namespace Drupal\api_tools\Rest;

use Drupal\api_tools\Exception\ErrorResponseException;
use Drupal\api_tools\Request\Request;
use Drupal\api_tools\Response\Debug;
use Drupal\api_tools\Response\ErrorResponse;
use Drupal\api_tools\Exception\ErrorResponseException;
use Generator;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\Utils;
use Psr\Log\LoggerInterface;

/**
* Provides connector for sending API requests to Portal API.
*/
final class ApiManager {

/**
* The http client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected ClientInterface $client;

/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected LoggerInterface $logger;

/**
* Constructs a new instance.
*
* @param \GuzzleHttp\ClientInterface $httpClient
* @param \GuzzleHttp\ClientInterface $client
* The http client.
* @param \Psr\Log\LoggerInterface $logger
* The logger.
*/
public function __construct(ClientInterface $httpClient, LoggerInterface $logger) {
$this->client = $httpClient;
$this->logger = $logger;
public function __construct(private ClientInterface $client) {
}

/**
Expand All @@ -62,7 +42,7 @@ public function __construct(ClientInterface $httpClient, LoggerInterface $logger
*
* @throws \Drupal\api_tools\Exception\ErrorResponseException
*/
public function handlePromises(array $promises, callable $callable) : Generator {
public function handlePromises(array $promises, callable $callable) : \Generator {
// Wait all promises to be finished.
try {
$results = Utils::unwrap($promises);
Expand Down
20 changes: 5 additions & 15 deletions src/Rest/ApiRequestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@

namespace Drupal\api_tools\Rest;

use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\api_tools\Request\Request;
use Drupal\api_tools\Response\Response;
use Generator;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Base class for rest request plugins.
*/
abstract class ApiRequestBase extends PluginBase implements ContainerFactoryPluginInterface {

/**
* The api manager.
*
* @var \Drupal\api_tools\Rest\ApiManager
*/
protected ApiManager $apiManager;

/**
* Constructs a new instance.
*
Expand All @@ -35,16 +27,14 @@ abstract class ApiRequestBase extends PluginBase implements ContainerFactoryPlug
* @param \Drupal\api_tools\Rest\ApiManager $apiManager
* The api manager.
*/
public function __construct(array $configuration, string $plugin_id, array $plugin_definition, ApiManager $apiManager) {
$this->apiManager = $apiManager;

public function __construct(array $configuration, string $plugin_id, array $plugin_definition, protected ApiManager $apiManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : static {
return new static(
$configuration,
$plugin_id,
Expand Down Expand Up @@ -85,7 +75,7 @@ public function request(Request $request, callable $callback) : Response {
*
* @throws \Drupal\api_tools\Exception\ErrorResponseException
*/
public function requestMultiple(array $requests, callable $callback): Generator {
public function requestMultiple(array $requests, callable $callback): \Generator {
$promises = [];

foreach ($requests as $request) {
Expand Down
13 changes: 2 additions & 11 deletions src/Rest/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@
*/
final class RequestFactory {

/**
* The request plugin manager.
*
* @var \Drupal\api_tools\Rest\ApiRequestPluginManager
*/
private ApiRequestPluginManager $requestManager;

/**
* Constructs a new instance.
*
* @param \Drupal\api_tools\Rest\ApiRequestPluginManager $apiRequestManager
* @param \Drupal\api_tools\Rest\ApiRequestPluginManager $requestManager
* The api request manager.
*/
public function __construct(ApiRequestPluginManager $apiRequestManager) {
$this->requestManager = $apiRequestManager;
public function __construct(private ApiRequestPluginManager $requestManager) {
}

/**
Expand All @@ -42,4 +34,3 @@ public function create(string $id) : ApiRequestBase {
}

}

4 changes: 3 additions & 1 deletion tests/src/Kernel/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ public function testPostRequest() {
$this->assertEquals('Test 2', $response->getEntities()[0]->title);
}

/**
* Tests post requests.
*/
public function testPostRequests() {
$requestFactory = $this->getMockRequestFactory([
new Response(200, [], json_encode([
Expand All @@ -157,4 +160,3 @@ public function testPostRequests() {
}

}

0 comments on commit 1711327

Please sign in to comment.