Skip to content

Commit

Permalink
Refactor du notifier Slack
Browse files Browse the repository at this point in the history
Ce refactor permet de moderniser le code du notifier en le rendant
testable avec un client HTTP et un serializer à jour.
  • Loading branch information
Mopolo committed Jan 17, 2025
1 parent 871a527 commit 9c839f3
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 588 deletions.
2 changes: 1 addition & 1 deletion app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters:
database_user: afup
database_password: afup
github_authenticator: "AppBundle\\Security\\TestGithubAuthenticator"
slack_url: null
slack_url: "http://fake-slack-url"

smtp_host: "mailcatcher"
smtp_tls: 0
Expand Down
9 changes: 8 additions & 1 deletion app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,15 @@ services:
AppBundle\Slack\MessageFactory:
autowire: true

app.slack.http_client:
class: GuzzleHttp\Client

AppBundle\Notifier\SlackNotifier:
arguments: ["%slack_url%", '@AppBundle\Slack\MessageFactory', "@jms_serializer.serializer"]
arguments:
$postUrl: "%slack_url%"
$messageFactory: '@AppBundle\Slack\MessageFactory'
$serializer: '@jms_serializer.serializer'
$httpClient: '@app.slack.http_client'

AppBundle\Routing\LegacyRouter:
autowire: true
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"friendsofpear/pear_exception": "0.0.*",
"guzzlehttp/guzzle": "^6.5",
"j7mbo/twitter-api-php": "^1.0",
"jms/serializer-bundle": "^1.1",
"jms/serializer-bundle": "5.*",
"knpuniversity/oauth2-client-bundle": "^1.4",
"league/iso3166": "^4.0",
"league/oauth2-github": "^0.2.1",
Expand Down
429 changes: 165 additions & 264 deletions composer.lock

Large diffs are not rendered by default.

72 changes: 17 additions & 55 deletions sources/AppBundle/Notifier/SlackNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,40 @@
use AppBundle\Event\Model\Vote;
use AppBundle\Slack\Message;
use AppBundle\Slack\MessageFactory;
use GuzzleHttp\ClientInterface;
use JMS\Serializer\Serializer;

class SlackNotifier
{
/**
* @var string
*/
private $postUrl;
private string $postUrl;
private MessageFactory $messageFactory;
private Serializer $serializer;
private ClientInterface $httpClient;

/**
* @var MessageFactory
*/
private $messageFactory;

/**
* @var Serializer
*/
private $serializer;

/**
* SlackNotifier constructor.
* @param $postUrl
* @param MessageFactory $messageFactory
* @param Serializer $serializer
*/
public function __construct($postUrl, MessageFactory $messageFactory, Serializer $serializer)
public function __construct(string $postUrl, MessageFactory $messageFactory, Serializer $serializer, ClientInterface $httpClient)
{
$this->postUrl = $postUrl;
$this->messageFactory = $messageFactory;
$this->serializer = $serializer;
$this->httpClient = $httpClient;
}

/**
* Send a message to slack for a new vote
*
* @param Vote $vote
* @return bool
*/
public function notifyVote(Vote $vote)
public function notifyVote(Vote $vote): void
{
$message = $this->messageFactory->createMessageForVote($vote);
return $this->sendMessage($message);
$this->sendMessage($this->messageFactory->createMessageForVote($vote));
}

/**
* Send a message to slack for a new talk
*
* @param Talk $talk
* @param Event $event
*
* @return bool
*/
public function notifyTalk(Talk $talk, Event $event)
public function notifyTalk(Talk $talk, Event $event): void
{
$message = $this->messageFactory->createMessageForTalk($talk, $event);
return $this->sendMessage($message);
$this->sendMessage($this->messageFactory->createMessageForTalk($talk, $event));
}

/**
* @param Message $message
* @return bool
*/
public function sendMessage(Message $message)
public function sendMessage(Message $message): void
{
$ch = curl_init($this->postUrl);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, ['payload' => $this->serializer->serialize($message, 'json')]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

return true;
$this->httpClient->request('POST', $this->postUrl, [
'form_params' => [
'payload' => $this->serializer->serialize($message, 'json'),
],
]);
}
}
Loading

0 comments on commit 9c839f3

Please sign in to comment.