Skip to content

Commit

Permalink
Add Domain Whitelisting
Browse files Browse the repository at this point in the history
  • Loading branch information
guilebc committed Feb 21, 2017
1 parent 539bf81 commit 3c0ff01
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 11 deletions.
15 changes: 15 additions & 0 deletions spec/MessengerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ function it_should_define_greeting_text($client)
$this->setGreetingText('my text');
}

function it_should_define_domain_whitelisting($client)
{
$expectedBody = [
'setting_type' => 'domain_whitelisting',
'whitelisted_domains' => ['https://www.google.com'],
'domain_action_type' => 'add'
];

$client->post('/me/thread_settings', Argument::that(function ($body) use ($expectedBody) {
return json_encode($body) === json_encode($expectedBody);
}))->shouldBeCalled();

$this->setDomainWhitelisting(['https://www.google.com']);
}

function it_should_delete_greeting_text($client)
{
$body = [
Expand Down
63 changes: 53 additions & 10 deletions src/Messenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Tgallice\FBMessenger\Model\ThreadSetting;
use Tgallice\FBMessenger\Model\ThreadSetting\GreetingText;
use Tgallice\FBMessenger\Model\ThreadSetting\StartedButton;
use Tgallice\FBMessenger\Model\ThreadSetting\DomainWhitelisting;
use Tgallice\FBMessenger\Model\UserProfile;
use Tgallice\FBMessenger\Model\WhitelistedDomains;

class Messenger
{
Expand Down Expand Up @@ -90,6 +92,34 @@ public function subscribe()
return $decoded['success'];
}

/**
* @param array $domains
* @param string $action
*/
public function setDomainWhitelisting($domains, $action = 'add')
{
$domainWhitelisting = new DomainWhitelisting($domains, $action);
$setting = $this->buildSetting(ThreadSetting::TYPE_DOMAIN_WHITELISTING, null, $domainWhitelisting, true);

$this->postThreadSettings($setting);
}

/**
* @return WhitelistedDomains
*/
public function getDomainWhitelisting()
{
$query = [
'fields' => DomainWhitelisting::WHITELISTED_DOMAINS
];

$response = $this->client->get('/me/thread_settings', $query);
$data = $this->decodeResponse($response);

return WhitelistedDomains::create($data)
->getDomains();
}

/**
* @param $text
*/
Expand Down Expand Up @@ -153,14 +183,7 @@ public function deletePersistentMenu()

$this->deleteThreadSettings($setting);
}

public function deleteGreetingText()
{
$setting = $this->buildSetting(ThreadSetting::TYPE_GREETING);

$this->deleteThreadSettings($setting);
}


/**
* Messenger Factory
*
Expand Down Expand Up @@ -191,15 +214,25 @@ private function deleteThreadSettings(array $setting)
{
$this->client->send('DELETE', '/me/thread_settings', $setting);
}

/**
* @param array $setting
*/
private function getThreadSettings(array $setting)
{
print_r($this->client->send('GET', '/me/thread_settings', null, $setting)->getContents());

}

/**
* @param string $type
* @param null|string $threadState
* @param mixed $value
* @param bool
*
* @return array
*/
private function buildSetting($type, $threadState = null, $value = null)
private function buildSetting($type, $threadState = null, $value = null, $callJsonSerialize = false)
{
$setting = [
'setting_type' => $type,
Expand All @@ -208,7 +241,12 @@ private function buildSetting($type, $threadState = null, $value = null)
if (!empty($threadState)) {
$setting['thread_state'] = $threadState;
}


//For the custom payload format...
if($callJsonSerialize) {
return array_merge($setting, $value->jsonSerialize());
}

if (!empty($value)) {
$setting[$type] = $value;
}
Expand Down Expand Up @@ -237,4 +275,9 @@ private function createMessage($message)

throw new \InvalidArgumentException('$message should be a string, Message, Attachment or Template');
}

public function deleteGreetingText()
{
// TODO: write logic here
}
}
3 changes: 2 additions & 1 deletion src/Model/ThreadSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ interface ThreadSetting
// Setting type
const TYPE_GREETING = 'greeting';
const TYPE_CALL_TO_ACTIONS = 'call_to_actions';

const TYPE_DOMAIN_WHITELISTING = 'domain_whitelisting';

// Thread state
const NEW_THREAD = 'new_thread';
const EXISTING_THREAD = 'existing_thread';
Expand Down
85 changes: 85 additions & 0 deletions src/Model/ThreadSetting/DomainWhitelisting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Tgallice\FBMessenger\Model\ThreadSetting;

use Tgallice\FBMessenger\Model\ThreadSetting;

class DomainWhitelisting implements ThreadSetting, \JsonSerializable
{
const TYPE_ADD = 'add';
const TYPE_REMOVE = 'remove';

const WHITELISTED_DOMAINS = 'whitelisted_domains';
const DOMAIN_ACTION_TYPE = 'domain_action_type';

/**
* @var string
*/
private $action;

/**
* @var array
*/
private $domains;

/**
*
* @param string $action
* @param array $domains
*
* @throws \InvalidArgumentException
*/
public function __construct($domains, $action = DomainWhitelisting::TYPE_ADD)
{
self::validateAction($action);
self::validateDomains($domains);

$this->action = $action;
$this->domains = $domains;
}

/**
*
* @param string $action
*
* @throws \InvalidArgumentException
*/
public static function validateAction($action)
{
if(!in_array($action, [DomainWhitelisting::TYPE_ADD, DomainWhitelisting::TYPE_REMOVE])) {
throw new \InvalidArgumentException('The action must be type: "add" or "remove".');
}
}

/**
*
* @param array $domains
*
* @throws \InvalidArgumentException
*/
public static function validateDomains($domains)
{
if(!is_array($domains)) {
throw new \InvalidArgumentException('$domains must be a array.');
}

//https://developers.facebook.com/docs/messenger-platform/thread-settings/domain-whitelisting
foreach($domains as $domain) {
if(!preg_match('#^https:\/\/#', $domain)) {
throw new \InvalidArgumentException('Each domain must be a "https" protocol.');
}
}
}

/**
* @inheritdoc
*/
public function jsonSerialize()
{
return [
DomainWhitelisting::WHITELISTED_DOMAINS => $this->domains,
DomainWhitelisting::DOMAIN_ACTION_TYPE => $this->action
];
}
}

41 changes: 41 additions & 0 deletions src/Model/WhitelistedDomains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tgallice\FBMessenger\Model;

use Tgallice\FBMessenger\Model\ThreadSetting\DomainWhitelisting;

class WhitelistedDomains
{
/**
* @var array
*/
private $data;

/**
* @param array $data
*/
public function __construct(array $data)
{
$this->data = $data;
}

/**
* @return array|null
*/
public function getDomains()
{
return isset($this->data['data'][0][DomainWhitelisting::WHITELISTED_DOMAINS]) ?
$this->data['data'][0][DomainWhitelisting::WHITELISTED_DOMAINS] :
null;
}

/**
* @param array $data
*
* @return WhitelistedDomains
*/
public static function create(array $data)
{
return new self($data);
}
}

0 comments on commit 3c0ff01

Please sign in to comment.