|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GitScrum\Services; |
| 4 | + |
| 5 | +use GitScrum\Contracts\SlackInterface; |
| 6 | +use Log; |
| 7 | +use Maknz\Slack\Client; |
| 8 | + |
| 9 | + |
| 10 | +class SlackService implements SlackInterface |
| 11 | +{ |
| 12 | + const ISSUE_ASSIGNATION = 1; |
| 13 | + const STATUS_UPDATE = 2; |
| 14 | + |
| 15 | + private $client; |
| 16 | + private $settings; |
| 17 | + |
| 18 | + public function __construct() |
| 19 | + { |
| 20 | + $this->settings = [ |
| 21 | + 'channel' => env('SLACK_CHANNEL', ''), |
| 22 | + 'username' => env('SLACK_BOT_NAME', ''), |
| 23 | + 'icon' => ':page_facing_up:', |
| 24 | + 'unfurl_links' => true, |
| 25 | + 'link_names' => 1, |
| 26 | + 'allow_markdown' => 1, |
| 27 | + ]; |
| 28 | + $this->client = new Client(env('SLACK_WEBHOOK', ''), $this->settings); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Send an Slack notification |
| 33 | + * @param array $content This array can contains custom attributes to send different notifications |
| 34 | + * @param integer $type Type of notification |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function send($content, $type = 0) |
| 39 | + { |
| 40 | + if (empty($this->client->getEndpoint()) || empty($this->client->getDefaultChannel()) |
| 41 | + || empty($this->client->getDefaultUsername())) { |
| 42 | + Log::info('One or more settings are missing, Slack notifications are not availables'); |
| 43 | + |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + $message = $this->buildMessage($content, $type); |
| 48 | + $this->client->attach([ |
| 49 | + 'title' => $content['title'], |
| 50 | + 'title_link' => $content['url'], |
| 51 | + 'color' => 'good', |
| 52 | + ])->enableMarkdown()->send($message); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Build the final output of message |
| 57 | + * @param array $content This array can contains custom attributes to send different notifications |
| 58 | + * @param integer $type Type of notification |
| 59 | + * |
| 60 | + * @return string Message ready to send |
| 61 | + */ |
| 62 | + private function buildMessage($content, $type) |
| 63 | + { |
| 64 | + $message = ''; |
| 65 | + |
| 66 | + switch ($type) { |
| 67 | + case 1: |
| 68 | + $usersAssigned = []; |
| 69 | + |
| 70 | + foreach ($content['assigned_to'] as $username) { |
| 71 | + if (! empty($username)) { |
| 72 | + $usersAssigned[] = "@{$username}"; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + $usersAssigned = implode(' ', $usersAssigned); |
| 77 | + $params = [ |
| 78 | + 'assignedBy' => $content['assigned_by'], |
| 79 | + 'assignedTo' => $usersAssigned, |
| 80 | + ]; |
| 81 | + $message = trans('gitscrum.issue-assigned', $params); |
| 82 | + break; |
| 83 | + case 2: |
| 84 | + switch ($content['status']) { |
| 85 | + case 1: |
| 86 | + $status = 'Todo'; |
| 87 | + break; |
| 88 | + case 2: |
| 89 | + $status = 'In Progress'; |
| 90 | + break; |
| 91 | + case 3: |
| 92 | + $status = 'Done'; |
| 93 | + break; |
| 94 | + case 4: |
| 95 | + $status = 'Archived'; |
| 96 | + break; |
| 97 | + default: |
| 98 | + $status = 'Todo'; |
| 99 | + break; |
| 100 | + } |
| 101 | + |
| 102 | + $params = [ |
| 103 | + 'username' => $content['updated_by'], |
| 104 | + 'status' => $status, |
| 105 | + ]; |
| 106 | + $message = trans('gitscrum.issue-status-updated', $params); |
| 107 | + break; |
| 108 | + default: |
| 109 | + # code... |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + return $message; |
| 114 | + } |
| 115 | +} |
0 commit comments