Skip to content

Commit

Permalink
Notification Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jan 22, 2019
1 parent c0dde63 commit 40dada8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/Fident.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function decodeNotification($requestBody): ?FidentNotification
$sig = self::urlsafeB64Decode(Objects::property($notification, 'Signature', ''));
if(openssl_verify($data, $sig, $this->getConfig()->getPublicKey(), OPENSSL_ALGO_SHA256))
{
return FidentNotification::fromString($data);
return FidentNotification::generate(Objects::property($notification, 'DataType', 1), $data);
}
return null;
}
Expand Down
73 changes: 7 additions & 66 deletions src/Notifications/FidentNotification.php
Original file line number Diff line number Diff line change
@@ -1,79 +1,20 @@
<?php
namespace Fident\Web\Notifications;

use Fident\Web\UserData\UserAttribute;
use Packaged\Helpers\Objects;

class FidentNotification
{
protected $_id;
protected $_created;
protected $_username;
protected $_attributes;
protected $_type;

const SUCCESS_RESPONSE = 'con';

public static function fromString($rawNotificationData)
const DT_USER_UPDATE = 1;

public static function generate($dataType, $jsonString)
{
$notification = new static();
$notificationData = json_decode($rawNotificationData);
$notification->_id = Objects::property($notificationData, 'ID');
$notification->_created = Objects::property($notificationData, 'Created');
$notification->_username = Objects::property($notificationData, 'Username');
$notification->_type = Objects::property($notificationData, 'Type');
$notification->_attributes = [];
foreach(Objects::property($notificationData, 'Attributes', []) as $attr)
switch($dataType)
{
$notification->addAttribute(Objects::property($attr, 'Key'), Objects::property($attr, 'Value'));
case self::DT_USER_UPDATE:
return UserUpdateNotification::fromString($jsonString);
}
return $notification;
}

public function addAttribute($key, $value)
{
$this->_attributes[$key] = new UserAttribute($key, $value);
return $this;
}

/**
* @return mixed
*/
public function getId()
{
return $this->_id;
}

/**
* @return mixed
*/
public function getCreated()
{
return $this->_created;
}

/**
* @return mixed
*/
public function getUsername()
{
return $this->_username;
}

/**
* @return UserAttribute[]
*/
public function getAttributes()
{
return $this->_attributes;
}

/**
* @return mixed
*/
public function getType()
{
return $this->_type;
return null;
}

}
68 changes: 68 additions & 0 deletions src/Notifications/UserUpdateNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
namespace Fident\Web\Notifications;

use Fident\Web\UserData\UserAttribute;
use Packaged\Helpers\Objects;

class UserUpdateNotification extends FidentNotification
{
protected $_id;
protected $_created;
protected $_username;
protected $_attributes;

const SUCCESS_RESPONSE = 'con';

public static function fromString($rawNotificationData)
{
$notification = new static();
$notificationData = json_decode($rawNotificationData);
$notification->_id = Objects::property($notificationData, 'ID');
$notification->_created = Objects::property($notificationData, 'Created');
$notification->_username = Objects::property($notificationData, 'Username');
$notification->_attributes = [];
foreach(Objects::property($notificationData, 'Attributes', []) as $attr)
{
$notification->addAttribute(Objects::property($attr, 'Key'), Objects::property($attr, 'Value'));
}
return $notification;
}

public function addAttribute($key, $value)
{
$this->_attributes[$key] = new UserAttribute($key, $value);
return $this;
}

/**
* @return mixed
*/
public function getId()
{
return $this->_id;
}

/**
* @return mixed
*/
public function getCreated()
{
return $this->_created;
}

/**
* @return mixed
*/
public function getUsername()
{
return $this->_username;
}

/**
* @return UserAttribute[]
*/
public function getAttributes()
{
return $this->_attributes;
}
}

0 comments on commit 40dada8

Please sign in to comment.