| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\ThirdParty; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentSupport\App\App; |
| 7 |
use FluentSupportPro\App\Services\Integrations\Slack\SlackNotification; |
| 8 |
|
| 9 |
class HandleSlackEvent |
| 10 |
{ |
| 11 |
|
| 12 |
/** |
| 13 |
* handleEvent method is responsible for handling slack events |
| 14 |
* @param string $token |
| 15 |
* @return bool |
| 16 |
* @throws Exception |
| 17 |
*/ |
| 18 |
|
| 19 |
public function handleEvent ( $token ) |
| 20 |
{ |
| 21 |
$this->verifyProVersion(); |
| 22 |
$this->validateToken($token); |
| 23 |
|
| 24 |
$request = App::getInstance('request'); |
| 25 |
|
| 26 |
return (new SlackNotification())->processSlackEvent($request->get('event')); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* verifyProVersion method will check if the pro version is installed or not |
| 33 |
* @throws Exception |
| 34 |
* @return boolean | Exception |
| 35 |
*/ |
| 36 |
private function verifyProVersion () |
| 37 |
{ |
| 38 |
if (!defined('FLUENTSUPPORTPRO')) { |
| 39 |
throw new \Exception('Slack Integration requires pro version of Fluent Support', 400); |
| 40 |
} |
| 41 |
|
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* validateToken method will check if the token is valid or not |
| 47 |
* @param string $token |
| 48 |
* @throws Exception |
| 49 |
* @return boolean | Exception |
| 50 |
*/ |
| 51 |
private function validateToken ($token) |
| 52 |
{ |
| 53 |
if (!hash_equals(\FluentSupportPro\App\Services\Integrations\Slack\SlackHelper::getWebhookToken(), $token)) { |
| 54 |
throw new \Exception('Bot Token could not be verified', 403); |
| 55 |
} |
| 56 |
return true; |
| 57 |
} |
| 58 |
} |
| 59 |
|