Skip to content

Commit

Permalink
Untangle (#312) - decouple from Laravel
Browse files Browse the repository at this point in the history
Allow easy use outside of the Laravel ecosystem
  • Loading branch information
reliq committed Jul 2, 2020
1 parent 1b3a9b6 commit f989e5e
Show file tree
Hide file tree
Showing 18 changed files with 350 additions and 360 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
matrix:
laravel-version: ['5.8.*', '^6.0', '^7.0']
stabilty: ['stable', 'lowest']
php-versions: ['7.2', '7.3']
php-versions: ['7.2', '7.3', '7.4']
name: PHP ${{ matrix.php-versions }} on Laravel ${{ matrix.laravel-version }} (${{ matrix.stabilty }})
steps:
- name: Checkout
Expand Down
5 changes: 3 additions & 2 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ $config = PhpCsFixer\Config::create()
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
'ordered_imports' => true,
'ordered_imports' => false,
'php_unit_internal_class' => true,
'php_unit_method_casing' => true,
'php_unit_ordered_covers' => true,
Expand All @@ -60,8 +60,9 @@ $config = PhpCsFixer\Config::create()
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],
'php_unit_test_class_requires_covers' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_order' => true,
'phpdoc_order' => false,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_separation' => false,
'phpdoc_types_order' => true,
'return_assignment' => true,
'semicolon_after_instruction' => true,
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"guzzlehttp/guzzle": "^6.4",
"psr/log": "^1.1",
"nesbot/carbon": "^2.26",
"guzzlehttp/oauth-subscriber": "^0.3.0"
"guzzlehttp/oauth-subscriber": "^0.3.0",
"php-di/php-di": "^6.2"
},
"require-dev": {
"phpunit/phpunit": "^8.3",
Expand All @@ -49,7 +50,7 @@
"extra": {
"laravel": {
"providers": [
"Atymic\\Twitter\\TwitterServiceProvider"
"Atymic\\Twitter\\ServiceProviders\\LaravelTwitterServiceProvider"
],
"aliases": {
"Twitter": "Atymic\\Twitter\\Facades\\Twitter"
Expand Down
115 changes: 36 additions & 79 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@

class Configuration
{
public const KEY_API_URL = 'api_url';
public const KEY_UPLOAD_URL = 'upload_url';
public const KEY_API_VERSION = 'api_version';
public const KEY_CONSUMER_KEY = 'consumer_key';
public const KEY_CONSUMER_SECRET = 'consumer_secret';
public const KEY_ACCESS_TOKEN = 'access_token';
public const KEY_ACCESS_TOKEN_SECRET = 'access_token_secret';
public const KEY_AUTHENTICATE_URL = 'authenticate_url';
public const KEY_ACCESS_TOKEN_URL = 'access_token_url';
public const KEY_REQUEST_TOKEN_URL = 'request_token_url';
public const DEBUG = 'debug';

private const PACKAGE_NAME = 'atymic/twitter';
private const DEFAULT_API_URL = 'api.twitter.com';
private const DEFAULT_UPLOAD_URL = 'upload.twitter.com';
private const DEFAULT_API_VERSION = '1.1';

/**
* @var string
Expand Down Expand Up @@ -55,34 +70,20 @@ class Configuration
*/
protected $userAgent;
/**
* @var string|null
* @var null|string
*/
private $authenticateUrl;

/**
* @var string|null
* @var null|string
*/
private $accessTokenUrl;

/**
* @var string|null
* @var null|string
*/
private $requestTokenUrl;

/**
* @param string $apiUrl
* @param string $uploadUrl
* @param string $apiVersion
* @param null|string $consumerKey
* @param null|string $consumerSecret
* @param null|string $accessToken
* @param null|string $accessTokenSecret
* @param string|null $authenticateUrl
* @param string|null $accessTokenUrl
* @param string|null $requestTokenUrl
* @param bool $debugMode
* @param null|string $userAgent
*/
public function __construct(
string $apiUrl,
string $uploadUrl,
Expand Down Expand Up @@ -114,40 +115,35 @@ public function __construct(
$this->userAgent = $userAgent ?? sprintf('%s v%s php v%s', self::PACKAGE_NAME, Twitter::VERSION, PHP_VERSION);
}

public static function createWithDefaults(): self
{
return new self(self::DEFAULT_API_URL, self::DEFAULT_UPLOAD_URL, self::DEFAULT_API_VERSION, null, null);
}

/**
* @param array $config
*
* @throws InvalidConfigException
*
* @return self
*/
public static function fromLaravelConfiguration(array $config): self
{
if (!isset($config['api_url'], $config['upload_url'], $config['api_version'])) {
throw new InvalidConfigException('Required configuration options missing');
if (!isset($config[self::KEY_API_URL], $config[self::KEY_UPLOAD_URL], $config[self::KEY_API_VERSION])) {
throw new InvalidConfigException('Required configuration options missing!');
}

return new self(
$config['api_url'],
$config['upload_url'],
$config['api_version'],
$config['consumer_key'],
$config['consumer_secret'],
$config['access_token'],
$config['access_token_secret'],
$config['authenticate_url'],
$config['access_token_url'],
$config['request_token_url'],
$config['debug']
$config[self::KEY_API_URL],
$config[self::KEY_UPLOAD_URL],
$config[self::KEY_API_VERSION],
$config[self::KEY_CONSUMER_KEY],
$config[self::KEY_CONSUMER_SECRET],
$config[self::KEY_ACCESS_TOKEN],
$config[self::KEY_ACCESS_TOKEN_SECRET],
$config[self::KEY_AUTHENTICATE_URL],
$config[self::KEY_ACCESS_TOKEN_URL],
$config[self::KEY_REQUEST_TOKEN_URL],
$config[self::DEBUG]
);
}

/**
* @param string $accessToken
* @param string $accessTokenSecret
*
* @return self
*/
public function withOauthCredentials(string $accessToken, string $accessTokenSecret): self
{
$config = clone $this;
Expand All @@ -157,9 +153,6 @@ public function withOauthCredentials(string $accessToken, string $accessTokenSec
return $config;
}

/**
* @return self
*/
public function withoutOauthCredentials(): self
{
$config = clone $this;
Expand All @@ -169,97 +162,61 @@ public function withoutOauthCredentials(): self
return $config;
}

/**
* @return string
*/
public function getApiUrl(): string
{
return $this->apiUrl;
}

/**
* @return string
*/
public function getUploadUrl(): string
{
return $this->uploadUrl;
}

/**
* @return string
*/
public function getApiVersion(): string
{
return $this->apiVersion;
}

/**
* @return null|string
*/
public function getConsumerKey(): ?string
{
return $this->consumerKey;
}

/**
* @return null|string
*/
public function getConsumerSecret(): ?string
{
return $this->consumerSecret;
}

/**
* @return null|string
*/
public function getAccessToken(): ?string
{
return $this->accessToken;
}

/**
* @return null|string
*/
public function getAccessTokenSecret(): ?string
{
return $this->accessTokenSecret;
}

/**
* @return string|null
*/
public function getAuthenticateUrl(): ?string
{
return $this->authenticateUrl;
}

/**
* @return string|null
*/
public function getAccessTokenUrl(): ?string
{
return $this->accessTokenUrl;
}

/**
* @return string|null
*/
public function getRequestTokenUrl(): ?string
{
return $this->requestTokenUrl;
}

/**
* @return bool
*/
public function isDebugMode(): bool
{
return $this->debugMode;
}

/**
* @return null|string
*/
public function getUserAgent(): ?string
{
return $this->userAgent;
Expand Down
9 changes: 3 additions & 6 deletions src/Exception/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atymic\Twitter\Exception;

use JsonException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseInterface as Response;
use RuntimeException;
Expand All @@ -23,8 +24,7 @@ class RequestException extends RuntimeException implements TwitterException
protected $response;

/**
* @param Response $response
* @param null|Throwable $previousException
* @throws JsonException
*
* @return static|TwitterException
*/
Expand All @@ -33,7 +33,7 @@ public static function fromClientResponse(
Throwable $previousException = null
): TwitterException {
$responseStatusCode = $response->getStatusCode();
$responseData = json_decode((string)$response->getBody(), true);
$responseData = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);
$instance = new static(self::DEFAULT_ERROR_MESSAGE, $response->getStatusCode(), $previousException);

if (empty($responseData[self::KEY_ERRORS])) {
Expand All @@ -50,9 +50,6 @@ public static function fromClientResponse(
return $instance;
}

/**
* @return Response
*/
public function getResponse(): Response
{
return $this->response;
Expand Down
4 changes: 1 addition & 3 deletions src/Facades/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ class Twitter extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return TwitterClient::class;
}
Expand Down
Loading

0 comments on commit f989e5e

Please sign in to comment.