Skip to content

Commit

Permalink
Fixing phpcs complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasmiller committed Feb 21, 2020
1 parent dc48da3 commit 959ccd0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
6 changes: 3 additions & 3 deletions lib/recurly/base_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ abstract class BaseClient

private $_baseUrl = 'https://v3.recurly.com';
private $_api_key;
protected $_http;
protected $http;

/**
* Constructor
Expand All @@ -18,7 +18,7 @@ abstract class BaseClient
public function __construct(string $api_key)
{
$this->_api_key = $api_key;
$this->_http = new HttpAdapter;
$this->http = new HttpAdapter;
}

/**
Expand Down Expand Up @@ -61,7 +61,7 @@ private function _getResponse(string $method, string $path, ?array $body = [], ?
$request = new \Recurly\Request($method, $path, $body, $params);

$url = $this->_buildPath($path, $params);
list($result, $response_header) = $this->_http->execute($method, $url, $body, $this->_headers());
list($result, $response_header) = $this->http->execute($method, $url, $body, $this->_headers());

// TODO: The $request should be added to the $response
$response = new \Recurly\Response($result);
Expand Down
31 changes: 18 additions & 13 deletions lib/recurly/http_adapter.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
<?php

namespace Recurly;

/**
* This class abstracts away all the PHP-level HTTP
* code. This allows us to easily mock out the HTTP
* calls in BaseClient by injecting a mocked version of
* this adapter.
*/

namespace Recurly;

class HttpAdapter
{
private static $_default_options = [
'ignore_errors' => true
];

/**
* @param string $method HTTP method to use
* @param string $url Fully qualified URL
* @param array $body The request body
* @param array $headers HTTP headers
* Performs HTTP request
*
* @param string $method HTTP method to use
* @param string $url Fully qualified URL
* @param array $body The request body
* @param array $headers HTTP headers
*
* @return array The API response as a string and the headers as an array
*/
public function execute($method, $url, $body, $headers)
public function execute($method, $url, $body, $headers): array
{
$body = empty($body) ? NULL : json_encode($body);
$options = array_replace(self::$_default_options, [
$body = empty($body) ? null : json_encode($body);
$options = array_replace(
self::$_default_options, [
'method' => $method,
'content' => $body,
]);
]
);
$headers_str = "";
foreach ($headers as $k => $v)
{
foreach ($headers as $k => $v) {
$headers_str .= "$k: $v\r\n";
}
$options['header'] = $headers_str;
Expand Down
14 changes: 13 additions & 1 deletion lib/recurly/recurly_traits.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@

trait RecurlyTraits
{
/**
* Generates User-Agent for API requests
*
* @return string Recurly client User-Agent string
*/
protected static function getUserAgent(): string
{
$php_version = phpversion();
return "Recurly/" . \Recurly\Version::CURRENT . "; php " . $php_version;
}

protected static function encodeApiKey($key): string
/**
* Base64 encodes the API key
*
* @param string $key The API key to encode
*
* @return string base64 encoded API key
*/
protected static function encodeApiKey(string $key): string
{
return base64_encode($key);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/mock_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MockClient extends BaseClient
public function __construct()
{
parent::__construct("apikey");
$this->_http = (new Generator())->getMock(HttpAdapter::class);
$this->http = (new Generator())->getMock(HttpAdapter::class);
}

protected function apiVersion(): string
Expand Down Expand Up @@ -55,7 +55,7 @@ public function deleteResource(string $resource_id): \Recurly\EmptyResource
public function addScenario($method, $url, $body, $result, $status): void
{
$resp_header = self::_generateRespHeader($status);
$this->_http->method('execute')->with(
$this->http->method('execute')->with(
$method,
$url,
$body,
Expand All @@ -65,7 +65,7 @@ public function addScenario($method, $url, $body, $result, $status): void

public function clearScenarios(): void
{
$this->_http = (new Generator())->getMock(HttpAdapter::class);
$this->http = (new Generator())->getMock(HttpAdapter::class);
}

private static function _generateRespHeader($status): array
Expand Down

0 comments on commit 959ccd0

Please sign in to comment.