Skip to content

Commit

Permalink
wip Slack
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Jan 23, 2024
1 parent 51d548e commit 10ac670
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Slack/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\Slack;

class Client extends \Guanguans\Notify\Foundation\Client
{
}
19 changes: 19 additions & 0 deletions src/Slack/Credential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\Slack;

use Guanguans\Notify\Foundation\NullCredential;

class Credential extends NullCredential
{
}
100 changes: 100 additions & 0 deletions src/Slack/Messages/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\Slack\Messages;

use Guanguans\Notify\Foundation\Concerns\AsJson;
use Guanguans\Notify\Foundation\Concerns\AsPost;
use Symfony\Component\OptionsResolver\OptionsResolver;

class Message extends \Guanguans\Notify\Foundation\Message
{
use AsPost;
use AsJson;

/**
* @var string[]
*/
protected $defined = [
'text',
'channel',
'username',
'icon_emoji',
'icon_url',
'unfurl_links',
'attachments',
];

/**
* @var array<string, mixed>
*/
protected array $options = [
'unfurl_links' => false,
'attachments' => [],
];

/**
* @var array
*/
protected $allowedTypes = [
'unfurl_links' => 'bool',
'attachments' => 'array',
];

protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver
{
return tap(parent::configureOptionsResolver($optionsResolver), static function (OptionsResolver $resolver): void {
$resolver->setNormalizer('attachments', static function (OptionsResolver $optionsResolver, array $value): array {
return isset($value[0]) ? $value : [$value];
});
});
}

public function setAttachments(array $attachments): self
{
return $this->addAttachments($attachments);
}

public function addAttachments(array $attachments): self
{
foreach ($attachments as $attachment) {
$this->addAttachment($attachment);
}

return $this;
}

public function setAttachment(array $attachment): self
{
return $this->addAttachment($attachment);
}

public function addAttachment(array $attachment): self
{
$this->options['attachments'][] = configure_options($attachment, static function (OptionsResolver $optionsResolver): void {
$optionsResolver->setDefined([
'fallback',
'text',
'pretext',
'color',
'fields',
]);
});

return $this;
}

public function httpUri()
{
return 'webhook_url';
}
}

0 comments on commit 10ac670

Please sign in to comment.