-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add domain whitelisting #45
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace spec\Tgallice\FBMessenger\Model\ThreadSetting; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Tgallice\FBMessenger\Model\ThreadSetting; | ||
|
||
class DomainWhitelistingSpec extends ObjectBehavior | ||
{ | ||
function let() | ||
{ | ||
$this->beConstructedWith(['https://google.com']); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Tgallice\FBMessenger\Model\ThreadSetting\DomainWhitelisting'); | ||
} | ||
|
||
function it_has_a_domains() | ||
{ | ||
$this->getDomains()->shouldReturn(['https://google.com']); | ||
} | ||
|
||
function it_has_a_add_action() | ||
{ | ||
$this->getAction()->shouldReturn('add'); | ||
} | ||
|
||
function it_has_a_remove_action() | ||
{ | ||
$this->beConstructedWith(['https://google.com'], 'remove'); | ||
|
||
$this->getAction()->shouldReturn('remove'); | ||
} | ||
|
||
function it_throws_exception_when_is_not_add_or_remove_action() | ||
{ | ||
$this->beConstructedWith(['https://google.com'], 'update'); | ||
|
||
$this | ||
->shouldThrow(new \InvalidArgumentException('The action must be type: "add" or "remove".')) | ||
->duringInstantiation(); | ||
} | ||
|
||
function it_throws_exception_when_domains_is_not_a_array() | ||
{ | ||
$this->beConstructedWith('not array'); | ||
|
||
$this | ||
->shouldThrow(new \InvalidArgumentException('Domains must be a array.')) | ||
->duringInstantiation(); | ||
} | ||
|
||
function it_throws_exception_when_one_domains_has_not_a_https_protocol() | ||
{ | ||
$this->beConstructedWith([ | ||
'https://petersfancyapparel.com', | ||
'http://google.com', | ||
'https://www.google.com' | ||
], 'add'); | ||
|
||
$this | ||
->shouldThrow(new \InvalidArgumentException('Each domain must be a "https" protocol.')) | ||
->duringInstantiation(); | ||
} | ||
|
||
function it_should_be_serializable() | ||
{ | ||
$this->shouldImplement(\JsonSerializable::class); | ||
|
||
$expected = [ | ||
'whitelisted_domains' => ['https://google.com'], | ||
'domain_action_type' => 'add' | ||
]; | ||
|
||
$this->jsonSerialize()->shouldBeLike($expected); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?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; | ||
} | ||
|
||
/** | ||
* | ||
* @return array | ||
*/ | ||
public function getDomains() | ||
{ | ||
return $this->domains; | ||
} | ||
|
||
/** | ||
* | ||
* @return string | ||
*/ | ||
public function getAction() | ||
{ | ||
return $this->action; | ||
} | ||
|
||
/** | ||
* | ||
* @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this is needed ? No mention of that in the documentation. |
||
]; | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about the need of this class. Or It's should be more generic. Maybe rename |
||
{ | ||
/** | ||
* @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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation look like to be updated. No need to
threadState
andsetting_type
anymore. Maybe a rewrite of this function should be better.