Created
February 11, 2016 09:21
-
-
Save cotcotquedec/3db90f274b08bd394ff8 to your computer and use it in GitHub Desktop.
Mon model pour la gestion de slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Models; | |
use Maknz\Slack\Client; | |
use Maknz\Slack\Message; | |
/** | |
* A mettre dans un service provider | |
* | |
* | |
public function register() | |
{ | |
$this->slack(); | |
} | |
public function slack() | |
{ | |
$this->mergeConfigFrom(app_path() . '/../vendor/maknz/slack/src/config/config.php', 'slack'); | |
$this->app['maknz.slack'] = $this->app->share(function($app) | |
{ | |
return new \Models\Slack( | |
$app['config']->get('slack.endpoint'), | |
[ | |
'channel' => $app['config']->get('slack.channel'), | |
'username' => $app['config']->get('slack.username'), | |
'icon' => $app['config']->get('slack.icon'), | |
'link_names' => $app['config']->get('slack.link_names'), | |
'unfurl_links' => $app['config']->get('slack.unfurl_links'), | |
'unfurl_media' => $app['config']->get('slack.unfurl_media'), | |
'allow_markdown' => $app['config']->get('slack.allow_markdown'), | |
'markdown_in_attachments' => $app['config']->get('slack.markdown_in_attachments') | |
], | |
new Guzzle | |
); | |
}); | |
$this->app->bind('Maknz\Slack\Client', 'maknz.slack'); | |
} | |
*/ | |
class Slack extends Client | |
{ | |
/** | |
* | |
* | |
* @var Message | |
*/ | |
protected $message; | |
/** | |
* Envoie un message | |
* | |
* @param \Maknz\Slack\Message $message | |
* @return $this | |
*/ | |
public function sendMessage(Message $message) | |
{ | |
// Ajout d'un emoji si dev | |
if (app()->environment() != 'production') { | |
$message->setText( ':chicken: ' . $message->getText()); | |
} | |
// envoie du message | |
parent::sendMessage($message); // TODO: Change the autogenerated stub | |
// suppression du message | |
$this->removeMessage(); | |
return $this; | |
} | |
/** | |
* Send the current message | |
* | |
* @return \Models\Slack | |
*/ | |
public function send($channel = null) | |
{ | |
if (!$this->hasMessage()) { | |
throw new \LogicException('Pas de message de setté'); | |
} | |
// on set le channel | |
if (!is_null($channel)) { | |
$this->to($channel); | |
} | |
return $this->sendMessage($this->message); | |
} | |
/** | |
* Return TRUE if a message is send | |
* | |
* @return bool | |
*/ | |
public function hasMessage() | |
{ | |
return isset($this->message); | |
} | |
/** | |
* Remove current message | |
* | |
* @return $this | |
*/ | |
public function removeMessage() | |
{ | |
unset($this->message); | |
return $this; | |
} | |
/** | |
* Set $message | |
* | |
* @param \Maknz\Slack\Message $message | |
* @return $this | |
*/ | |
public function setMessage(Message $message) | |
{ | |
$this->message = $message; | |
return $this; | |
} | |
/** | |
* Renvoie le message | |
* | |
* @return \Maknz\Slack\Message | |
*/ | |
public function getMessage() | |
{ | |
if (!$this->hasMessage()) { | |
$this->setMessage($this->createMessage()); | |
} | |
return $this->message; | |
} | |
/** | |
* Set le trext du message | |
* | |
* @param $text | |
* @return $this | |
*/ | |
public function setText($text) | |
{ | |
$this->getMessage()->setText($text); | |
return $this; | |
} | |
/** | |
* Append text to the message | |
* | |
* @param $text | |
* @return $this | |
*/ | |
public function appendText($text) | |
{ | |
$this->getMessage()->setText($this->getMessage()->getText() . $text); | |
return $this; | |
} | |
/** | |
* Preprend text to the message | |
* | |
* @param $text | |
* @return $this | |
*/ | |
public function prependText($text) | |
{ | |
$this->getMessage()->setText($text . $this->getMessage()->getText()); | |
return $this; | |
} | |
/** | |
* Set le channel du message | |
* | |
* @param $channel | |
* @return $this | |
*/ | |
public function to($channel) | |
{ | |
$this->getMessage()->to($channel); | |
return $this; | |
} | |
/** | |
* Set un icon | |
* | |
* @param $emoji | |
* @return $this | |
*/ | |
public function withIcon($emoji) | |
{ | |
$this->getMessage()->withIcon($emoji); | |
return $this; | |
} | |
/** | |
* Set from message attribute | |
* | |
* @param $from | |
* @return $this | |
*/ | |
public function from($from) | |
{ | |
$this->getMessage()->from($from); | |
return $this; | |
} | |
/** | |
* Add an attachement | |
* | |
* @param $text | |
* @param null $pretext | |
* @param null $color | |
* @param null $fields | |
* @param null $fallback | |
* @return $this | |
*/ | |
public function attach($text, $pretext = null, $color = null, $fields = null, $fallback = null) | |
{ | |
$attachment = new \Maknz\Slack\Attachment([]); | |
$attachment->setText($text); | |
if (!is_null($pretext)) { | |
$attachment->setPretext($pretext); | |
} | |
if (!is_null($color)) { | |
$attachment->setColor($color); | |
} | |
if (!is_null($fields)) { | |
$attachment->setFields($fields); | |
} | |
if (!is_null($fallback)) { | |
$fallback = $text; | |
} | |
$attachment->setFallback($fallback); | |
$this->getMessage()->setAttachments([$attachment]); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ex d'utilisation