Skip to content

Commit

Permalink
wip Pushover
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Feb 2, 2024
1 parent de9a2dc commit b772378
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/Foundation/Concerns/AsMultipart.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait AsMultipart
public function toHttpOptions(): array
{
return [
RequestOptions::MULTIPART => $this->getOptions(),
RequestOptions::MULTIPART => to_multipart($this->getOptions()),
];
}
}
39 changes: 39 additions & 0 deletions src/Foundation/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,47 @@
* This source file is subject to the MIT license that is bundled.
*/

use GuzzleHttp\Psr7\Utils;
use Symfony\Component\OptionsResolver\OptionsResolver;

if (! function_exists('to_multipart')) {
/**
* @noinspection SlowArrayOperationsInLoopInspection
*/
function to_multipart(array $form): array
{
$normalizeMultipartField = function (string $name, $contents) use (&$normalizeMultipartField): array {
$field = [];
if (! is_array($contents)) {
if (is_string($contents) && is_file($contents)) {
$contents = Utils::tryFopen($contents, 'r');
}

return [compact('name', 'contents')];
}

foreach ($contents as $key => $value) {
$key = "{$name}[$key]";
$field = array_merge(
$field,
is_array($value)
? $normalizeMultipartField($key, $value)
: [['name' => $key, 'contents' => $value]]
);
}

return $field;
};

$multipart = [];
foreach ($form as $name => $contents) {
$multipart = array_merge($multipart, $normalizeMultipartField($name, $contents));
}

return $multipart;
}
}

if (! function_exists('configure_options')) {
/**
* Configuration options.
Expand Down
21 changes: 5 additions & 16 deletions src/Pushover/Credential.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,13 @@

namespace Guanguans\Notify\Pushover;

use Guanguans\Notify\Foundation\Credentials\NullCredential;
use Guanguans\Notify\Foundation\Credentials\AggregateCredential;
use Guanguans\Notify\Foundation\Credentials\QueryCredential;

class Credential extends NullCredential
class Credential extends AggregateCredential
{
private ?string $token;
private ?string $user;

public function __construct(?string $token = null, ?string $user = null)
public function __construct(string $user, string $token)
{
$this->token = $token;
$this->user = $user;
}

public function applyToOptions(array $options): array
{
$options['multipart']['token'] = $this->token;
$options['multipart']['user'] = $this->user;

return $options;
parent::__construct(new QueryCredential('user', $user), new QueryCredential('token', $token));
}
}
102 changes: 52 additions & 50 deletions src/Pushover/Messages/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Guanguans\Notify\Foundation\Concerns\AsMultipart;
use Guanguans\Notify\Foundation\Concerns\AsPost;
use GuzzleHttp\Psr7\Utils;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -39,9 +40,6 @@ class Message extends \Guanguans\Notify\Foundation\Message
use AsMultipart;
use AsPost;

/**
* @var array<string>
*/
protected array $defined = [
// 'token',
// 'user',
Expand All @@ -59,11 +57,11 @@ class Message extends \Guanguans\Notify\Foundation\Message
'callback',
'device',
'attachment',

// 'attachment_base64',
// 'attachment_type',
];

/**
* @var array<string>
*/
protected array $required = [
// 'token',
// 'user',
Expand All @@ -89,58 +87,62 @@ class Message extends \Guanguans\Notify\Foundation\Message
'monospace' => [0, 1],
];

public function toHttpUri()
public function toHttpUri(): string
{
return 'https://api.pushover.net/1/messages.json';
}

protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver
{
return tap(parent::configureOptionsResolver($optionsResolver), static function (OptionsResolver $optionsResolver): void {
$optionsResolver->setNormalizer('priority', static function (OptionsResolver $optionsResolver, $value): int {
if (2 !== $value) {
return $value;
}

if (isset($optionsResolver['retry'], $optionsResolver['expire'])) {
return $value;
}

throw new MissingOptionsException('The required option "retry" or "expire" is missing.');
});

$optionsResolver->setNormalizer('html', static function (OptionsResolver $optionsResolver, $value): int {
if (1 !== $value) {
return $value;
}

if (! isset($optionsResolver['monospace'])) {
return $value;
}

if (1 !== $optionsResolver['monospace']) {
return $value;
}

throw new InvalidOptionsException('Html cannot be set with monospace, Monospace cannot be set with html, Html and monospace are mutually.');
});

$optionsResolver->setNormalizer('attachment', static function (OptionsResolver $optionsResolver, $value): array {
if (\is_string($value)) {
if ('' === $value) {
throw new InvalidOptionsException('The attachment cannot be empty.');
return tap(
parent::configureOptionsResolver($optionsResolver),
static function (OptionsResolver $optionsResolver): void {
$optionsResolver->setNormalizer(
'priority',
static function (OptionsResolver $optionsResolver, $value): int {
if (2 !== $value) {
return $value;
}

if (isset($optionsResolver['retry'], $optionsResolver['expire'])) {
return $value;
}

throw new MissingOptionsException('The required option "retry" or "expire" is missing.');
}
);

$value = fopen($value, 'r');
if (false === $value) {
throw new InvalidOptionsException("The attachment resource file does not exist: {$value}.");
}
}
$optionsResolver->setNormalizer(
'html',
static function (OptionsResolver $optionsResolver, $value): int {
if (1 !== $value) {
return $value;
}

if (! isset($optionsResolver['monospace'])) {
return $value;
}

return [
'attachment' => $value,
];
});
});
if (1 !== $optionsResolver['monospace']) {
return $value;
}

throw new InvalidOptionsException('Html cannot be set with monospace, Monospace cannot be set with html, Html and monospace are mutually.');
}
);

// $optionsResolver->setNormalizer(
// 'attachment',
// static function (OptionsResolver $optionsResolver, $value) {
// if (\is_string($value)) {
// // $value = fopen($value, 'r');
// $value = Utils::tryFopen($value, 'r');
// }
//
// return $value;
// }
// );
}
);
}
}

0 comments on commit b772378

Please sign in to comment.