Skip to content

Commit

Permalink
refactor(utils): update Utils references
Browse files Browse the repository at this point in the history
- Updated references to Utils class to use fully qualified namespace
- Replaced usage of Utils class with Support\Utils namespace
- Updated method calls to use fully qualified namespace for Utils class
  • Loading branch information
guanguans committed Jul 10, 2024
1 parent 906d848 commit 928d8d1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 97 deletions.
6 changes: 3 additions & 3 deletions src/Foundation/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
use Guanguans\Notify\Foundation\Concerns\HasHttpClient;
use Guanguans\Notify\Foundation\Contracts\Authenticator;
use Guanguans\Notify\Foundation\Contracts\Message;
use Guanguans\Notify\Foundation\Support\Utils;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\Utils;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -70,7 +70,7 @@ public function sendAsync(Message $message): PromiseInterface
return $this->getHttpClient()->requestAsync(
$message->toHttpMethod(),
$message->toHttpUri(),
$this->normalizeHttpOptions($this->authenticator->applyToOptions($message->toHttpOptions())),
Utils::normalizeHttpOptions($this->authenticator->applyToOptions($message->toHttpOptions())),
);
}

Expand All @@ -89,7 +89,7 @@ public function pool(iterable $messages): array
{
// return Utils::settle($promises)->wait();
/** @noinspection PhpParamsInspection */
return Utils::unwrap(
return \GuzzleHttp\Promise\Utils::unwrap(
(function (iterable $messages): \Generator {
foreach ($messages as $key => $message) {
yield $key => $this->sendAsync($message);
Expand Down
2 changes: 1 addition & 1 deletion src/Foundation/Concerns/AsMultipart.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
trait AsMultipart
{
/**
* @see \Guanguans\Notify\Foundation\Client::normalizeHttpOptions()
* @see \Guanguans\Notify\Foundation\Support\Utils::normalizeHttpOptions()
* @see \Guanguans\Notify\Foundation\Client::send()
*/
public function toHttpOptions(): array
Expand Down
14 changes: 1 addition & 13 deletions src/Foundation/Concerns/HasHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function setHttpClientResolver(callable $httpClientResolver): self

public function getHttpClientResolver(): callable
{
return $this->httpClientResolver ??= fn (): Client => new Client($this->normalizeHttpOptions(
return $this->httpClientResolver ??= fn (): Client => new Client(Utils::normalizeHttpOptions(
Utils::mergeHttpOptions($this->defaultHttpOptions(), $this->getHttpOptions()),
));
}
Expand Down Expand Up @@ -170,18 +170,6 @@ public function defaultHttpOptions(): array
];
}

public function normalizeHttpOptions(array $options): array
{
if (isset($options[RequestOptions::MULTIPART])) {
$options[RequestOptions::MULTIPART] = Utils::multipartFor(
$options[RequestOptions::MULTIPART],
MULTIPART_TRY_OPEN_FILE,
);
}

return $options;
}

/**
* @param null|list<ResponseInterface|\Throwable> $queue
*/
Expand Down
160 changes: 80 additions & 80 deletions src/Foundation/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,86 @@
*/
class Utils
{
/**
* Replace the given options with the current request options.
*/
public static function mergeHttpOptions(array $originalOptions, array ...$options): array
{
return array_replace_recursive(
array_merge_recursive($originalOptions, Arr::only($options, [
RequestOptions::COOKIES,
RequestOptions::FORM_PARAMS,
RequestOptions::HEADERS,
RequestOptions::JSON,
RequestOptions::MULTIPART,
RequestOptions::QUERY,
])),
...$options,
);
}

public static function normalizeHttpOptions(array $httpOptions, int $options = MULTIPART_TRY_OPEN_FILE): array
{
if (isset($httpOptions[RequestOptions::MULTIPART])) {
$httpOptions[RequestOptions::MULTIPART] = self::multipartFor(
$httpOptions[RequestOptions::MULTIPART],
$options,
);
}

return $httpOptions;
}

/**
* Retrieves the HTTP options constants.
*
* @return array<string, string>
*/
public static function httpOptionConstants(): array
{
$constants = (new \ReflectionClass(RequestOptions::class))->getConstants() + [
// '_CONDITIONAL' => '_conditional',
'BASE_URI' => 'base_uri',
'CURL' => 'curl',
];

asort($constants);

return $constants;
}

/**
* Return an array of defined properties for the given object.
*
* @psalm-suppress InvalidScope
*
* @param class-string|Message $object
*
* @throws \ReflectionException
*
* @return list<string>
*/
public static function definedFor($object): array
{
if (\is_string($object)) {
$reflectionClass = new \ReflectionClass($object);

$properties = $reflectionClass->getDefaultProperties();

return array_unique(array_merge(
$properties['defined'] ?? [],
$properties['required'] ?? []
));
}

return array_unique(
(fn (): array => array_merge(
$this->defined ?? [],
$this->required ?? [],
))->call($object)
);
}

/**
* Convert a form array into a multipart array.
*/
Expand Down Expand Up @@ -106,86 +186,6 @@ static function (array $part) use ($contentsNormalizer, $options): array {
);
}

/**
* Return an array of defined properties for the given object.
*
* @psalm-suppress InvalidScope
*
* @param class-string|Message $object
*
* @throws \ReflectionException
*
* @return list<string>
*/
public static function definedFor($object): array
{
if (\is_string($object)) {
$reflectionClass = new \ReflectionClass($object);

$properties = $reflectionClass->getDefaultProperties();

return array_unique(array_merge(
$properties['defined'] ?? [],
$properties['required'] ?? []
));
}

return array_unique(
(fn (): array => array_merge(
$this->defined ?? [],
$this->required ?? [],
))->call($object)
);
}

/**
* Retrieves the HTTP options constants.
*
* @return array<string, string>
*/
public static function httpOptionConstants(): array
{
$constants = (new \ReflectionClass(RequestOptions::class))->getConstants() + [
// '_CONDITIONAL' => '_conditional',
'BASE_URI' => 'base_uri',
'CURL' => 'curl',
];

asort($constants);

return $constants;
}

/**
* Replace the given options with the current request options.
*/
public static function mergeHttpOptions(array $originalOptions, array ...$options): array
{
return array_replace_recursive(
array_merge_recursive($originalOptions, Arr::only($options, [
RequestOptions::COOKIES,
RequestOptions::FORM_PARAMS,
RequestOptions::HEADERS,
RequestOptions::JSON,
RequestOptions::MULTIPART,
RequestOptions::QUERY,
])),
...$options,
);
}

public static function normalizeHttpOptions(array $httpOptions, int $options = MULTIPART_TRY_OPEN_FILE): array
{
if (isset($httpOptions[RequestOptions::MULTIPART])) {
$httpOptions[RequestOptions::MULTIPART] = self::multipartFor(
$httpOptions[RequestOptions::MULTIPART],
$options,
);
}

return $httpOptions;
}

/**
* @param array<string, scalar> $agents
*/
Expand Down

0 comments on commit 928d8d1

Please sign in to comment.