| 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 |
if ($request->get('type') == 'url_verification') { |
| 27 |
echo wp_kses_post($request->get('challenge')); |
| 28 |
} |
| 29 |
|
| 30 |
return (new SlackNotification())->processSlackEvent($request->get('event')); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* verifyProVersion method will check if the pro version is installed or not |
| 37 |
* @throws Exception |
| 38 |
* @return boolean | Exception |
| 39 |
*/ |
| 40 |
private function verifyProVersion () |
| 41 |
{ |
| 42 |
if (!defined('FLUENTSUPPORTPRO')) { |
| 43 |
throw new \Exception('Slack Integration requires pro version of Fluent Support', 400); |
| 44 |
} |
| 45 |
|
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* validateToken method will check if the token is valid or not |
| 51 |
* @param string $token |
| 52 |
* @throws Exception |
| 53 |
* @return boolean | Exception |
| 54 |
*/ |
| 55 |
private function validateToken ($token) |
| 56 |
{ |
| 57 |
if (\FluentSupportPro\App\Services\Integrations\Slack\SlackHelper::getWebhookToken() != $token) { |
| 58 |
throw new \Exception('Bot Token could not be verified', 404); |
| 59 |
} |
| 60 |
return true; |
| 61 |
} |
| 62 |
} |
| 63 |
|