Skip to content

Instantly share code, notes, and snippets.

@nclundsten
Created May 29, 2014 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nclundsten/f0054c67647ec27e9e83 to your computer and use it in GitHub Desktop.
Save nclundsten/f0054c67647ec27e9e83 to your computer and use it in GitHub Desktop.
<?php
namespace Subscription\Service;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
class SubscriptionService implements SubscriptionInterface, ServiceLocatorAwareInterface
{
use ServiceLocatorAwareInterface;
protected $config;
protected $mapper;
public function generateKey($email)
{
$config = $this->getConfig();
if (!isset($config['hash_salt'])) {
throw new Exception('missing hash_salt in "subscription" config');
}
$salt = (string) $config['hash_salt'];
return md5($salt.$email.'pePpeR');
}
public function validateKey($email, $key)
{
if ($key === $this->generateKey($email)) {
return true;
}
return false;
}
public function unsubscribe($email, $typeEnum=0, $typeId=0)
{
$record = $this->getRecord($email, $typeEnum, $typeId, true);
$record->setUnsubscribeDate(time());
$this->getMapper()->persist($record);
}
public function resubscribe($email, $typeEnum=0, $typeId=0)
{
$record = $this->getRecord($email, $typeEnum, $typeId, true);
$record->setSubscribeDate(time());
$this->getMapper()->persist($record);
}
public function getRecord($email, $typeEnum=0, $typeId=0, $new=false)
{
$record = $this->getMapper()->getRecord($email, $typeEnum, $typeId);
if (!$record && $new) {
$record = $this->getMapper()->getEntityPrototype();
$record->setEmail($email);
$record->setTypeEnum($typeEnum);
$record->setTypeId($typeId);
}
return $record;
}
public function canEmail($email, $typeEnum=0, $typeId=0)
{
$record = $this->getMapper()->getRecord($email, $typeEnum, $typeId);
if ($record) {
//did they re-subscribe?
if ($record->getSubscribeDate() > $record->getUnsubscribeDate()) {
return true;
}
return false;
}
//maybe they are unsubscribed to the whole site/company
if ($typeEnum != 0 && $typeId != 0) {
$record = $this->getMapper()->getRecord($email, 0, 0);
if ($record) {
//did they re-subscribe?
if ($record->getSubscribeDate() > $record->getUnsubscribeDate()) {
return true;
}
return false;
}
}
return true;
}
public function getConfig()
{
if (null === $this->config) {
$config = $this->getServiceLocator()->get('Config');
if (!isset($config['subscription']) || !is_array($config['subscription'])) {
throw new \Exception("config required for subscription service");
}
$this->config = $config['subscription'];
}
return $this->config;
}
public function setConfig($config)
{
$this->config = $config;
return $this;
}
public function getMapper()
{
if (null === $this->mapper) {
$this->mapper = $this->getServiceLocator()
->get('Subscription\Mapper\SubscriptionMapper');
}
return $this->mapper;
}
public function setMapper($mapper)
{
$this->mapper = $mapper;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment