Inplement singleton pattern in AbstractProcessor

This commit is contained in:
Dmitriy Simushev 2014-03-05 08:58:10 +00:00
parent f467c35bd3
commit 9069570f0b
4 changed files with 57 additions and 116 deletions

View File

@ -90,6 +90,8 @@ use Mibew\RequestProcessor\Exception\AbstractProcessorException;
* }
* </code>
*
* Implements Singleton pattern for children classes using Late Static Bindings.
*
* @see \Mibew\RequestProcessor\AbstractProcessor::__construct()
*/
abstract class AbstractProcessor
@ -123,42 +125,24 @@ abstract class AbstractProcessor
protected $config = array();
/**
* Class constructor
* An instance of the AbstractProcessor class
*
* @param type $config Configuration data.
* It must contains following keys:
* - 'signature': Use for verification sender
* - 'trusted_signatures': array of trusted signatures. Uses for identify
* another side of interaction.
* And may contains following (if not default values will be used)
* - 'event_prefix': prefix that uses for all events triggered by the
* class. The default value is the class name with first character in
* lower case
* @var \Mibew\RequestProcessor\AbstractProcessor
*/
public function __construct($config)
protected static $instance = null;
/**
* Return an instance of the AbstractProcessor class.
*
* @return \Mibew\RequestProcessor\AbstractProcessor
*/
public static function getInstance()
{
// Check signature
if (!isset($config['signature'])) {
trigger_error("Signature is not specified", E_USER_ERROR);
if (is_null(static::$instance)) {
static::$instance = new static();
}
// Check trusted signatures
if (!isset($config['trusted_signatures'])) {
trigger_error("Trusted signatures is not specified", E_USER_ERROR);
}
// Get an instance of the MibewAPI class
$this->mibewAPI = $this->getMibewAPIInstance();
// Get class name and prefix for events and etc.
$class_name_parts = explode('\\', get_class($this));
$class_name = array_pop($class_name_parts);
$this->eventPrefix = empty($config['event_prefix'])
? strtolower(substr($class_name, 0, 1)) . substr($class_name, 1)
: $config['event_prefix'];
// Store config
$this->config = $config;
return static::$instance;
}
/**
@ -336,6 +320,45 @@ abstract class AbstractProcessor
return $result;
}
/**
* Class constructor
*
* @param type $config Configuration data.
* It must contains following keys:
* - 'signature': Use for verification sender
* - 'trusted_signatures': array of trusted signatures. Uses for identify
* another side of interaction.
* And may contains following (if not default values will be used)
* - 'event_prefix': prefix that uses for all events triggered by the
* class. The default value is the class name with first character in
* lower case
*/
protected function __construct($config)
{
// Check signature
if (!isset($config['signature'])) {
trigger_error("Signature is not specified", E_USER_ERROR);
}
// Check trusted signatures
if (!isset($config['trusted_signatures'])) {
trigger_error("Trusted signatures is not specified", E_USER_ERROR);
}
// Get an instance of the MibewAPI class
$this->mibewAPI = $this->getMibewAPIInstance();
// Get class name and prefix for events and etc.
$class_name_parts = explode('\\', get_class($this));
$class_name = array_pop($class_name_parts);
$this->eventPrefix = empty($config['event_prefix'])
? strtolower(substr($class_name, 0, 1)) . substr($class_name, 1)
: $config['event_prefix'];
// Store config
$this->config = $config;
}
/**
* Process request
*

View File

@ -30,41 +30,13 @@ use Mibew\RequestProcessor\Exception\InviteProcessorException;
* - inviteReceiveRequestError
* - inviteCallError
* - inviteFunctionCall
*
* Implements Singleton pattern
*/
class InviteProcessor extends ClientSideProcessor
{
/**
* An instance of the InviteProcessor class
*
* @var \Mibew\RequestProcessor\InviteProcessor
*/
protected static $instance = null;
/**
* Return an instance of the InviteProcessor class.
*
* @return \Mibew\RequestProcessor\InviteProcessor
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Class constructor
*
* Do not use directly __construct method! Use
* \Mibew\RequestProcessor\InviteProcessor::getInstance() instead!
*
* @todo Think about why the method is not protected
*/
public function __construct()
protected function __construct()
{
parent::__construct(array(
'signature' => '',

View File

@ -36,32 +36,9 @@ use Mibew\RequestProcessor\Exception\ThreadProcessorException;
* WARNING:
* threadResponseReceived registered but never called because of asynchronous
* nature of Core-to-Window interaction
*
* Implements Singleton pattern
*/
class ThreadProcessor extends ClientSideProcessor
{
/**
* An instance of the ThreadProcessor class
*
* @var \Mibew\RequestProcessor\ThreadProcessor
*/
protected static $instance = null;
/**
* Return an instance of the ThreadProcessor class.
*
* @return \Mibew\RequestProcessor\ThreadProcessor
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Loads thread by id and token and checks if thread loaded
*
@ -131,11 +108,8 @@ class ThreadProcessor extends ClientSideProcessor
/**
* Class constructor
*
* Do not use directly __construct method! Use
* \Mibew\RequestProcessor\ThreadProcessor::getInstance() instead!
*/
public function __construct()
protected function __construct()
{
parent::__construct(array(
'signature' => '',

View File

@ -38,41 +38,13 @@ use Mibew\RequestProcessor\Exception\UsersProcessorException;
* Also triggers follow events (see description of apiUpdateVisitors method):
* - usersUpdateVisitorsLoad
* - usersUpdateVisitorsAlter
*
* Implements Singleton pattern
*/
class UsersProcessor extends ClientSideProcessor
{
/**
* An instance of the UsersProcessor class
*
* @var \Mibew\RequestProcessor\UsersProcessor
*/
protected static $instance = null;
/**
* Return an instance of the UsersProcessor class.
*
* @return \Mibew\RequestProcessor\UsersProcessor
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Class constructor
*
* Do not use directly __construct method! Use
* \Mibew\RequestProcessor\UsersProcessor::getInstance() instead!
*
* @todo Think about why the method is not protected
*/
public function __construct()
protected function __construct()
{
parent::__construct(array(
'signature' => '',