Permalink
Browse files

Add DoctrineORM mapper and connecting bits

  • Loading branch information...
1 parent 8e1e3b5 commit 283a73c4c97334f77f34575a141ffa7cd044f00d @adamlundrigan adamlundrigan committed Oct 24, 2012
View
@@ -8,6 +8,7 @@
Zend\ModuleManager\Feature\ConfigProviderInterface,
Zend\ModuleManager\Feature\ServiceProviderInterface,
ZfcUser\Validator\NoRecordExists as ZfcUserUniqueEmailValidator;
+use Doctrine\ORM\Mapping\Driver\XmlDriver;
class Module implements
AutoloaderProviderInterface,
@@ -55,6 +56,18 @@ public function getServiceConfig()
$obj->setHydrator(new Mapper\EmailVerificationHydrator());
return $obj;
},
+ 'cdlitwostagesignup_ev_modelmapper_doctrineorm' => function($sm) {
+ // Add Entity driver
+ $chain = $sm->get('doctrine.driver.orm_default');
+ $chain->addDriver(new XmlDriver(__DIR__ . '/config/xml/cdlitwostagesignup-doctrineorm'), 'CdliTwoStageSignup\Entity');
+
+ // Create instance of Doctrine ORM mapper
+ $obj = new Mapper\EmailVerification\DoctrineORM(
+ $sm->get('zfcuser_doctrine_em'),
+ $sm->get('cdlitwostagesignup_module_options')
+ );
+ return $obj;
+ },
'cdlitwostagesignup_ev_tablegateway' => function($sm) {
$obj = new \Zend\Db\TableGateway\TableGateway(
'user_signup_email_verification',
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
+ http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
+
+ <entity name="CdliTwoStageSignup\Entity\EmailVerification" table="user_signup_email_verification">
+ <id name="request_key" type="string" unique="true" length="255" column="request_key">
+ <generator strategy="NONE" />
+ </id>
+ <field name="email_address" type="string" unique="true" length="255" column="email_address" />
+ <field name="request_time" type="datetime" column="request_time" />
+ </entity>
+
+</doctrine-mapping>
@@ -0,0 +1,67 @@
+<?php
+namespace CdliTwoStageSignup\Mapper\EmailVerification;
+
+use Doctrine\ORM\EntityManager;
+use CdliTwoStageSignup\Options\ModuleOptions;
+use CdliTwoStageSignup\Entity\EmailVerification as Model;
+use Zend\Stdlib\Hydrator\HydratorInterface;
+use ZfcBase\EventManager\EventProvider;
+
+class DoctrineORM extends EventProvider implements MapperInterface
+{
+ /**
+ * @var \Doctrine\ORM\EntityManager
+ */
+ protected $em;
+
+ /**
+ * @var \CdliTwoStageSignup\Options\ModuleOptions
+ */
+ protected $options;
+
+ public function __construct(EntityManager $em, ModuleOptions $options)
+ {
+ $this->em = $em;
+ $this->options = $options;
+ }
+
+ protected function getEntityRepository()
+ {
+ return $this->em->getRepository('CdliTwoStageSignup\Entity\EmailVerification');
+ }
+
+ public function findByEmail($email)
+ {
+ $entity = $this->getEntityRepository()->findOneBy(array('email_address' => $email));
+ $this->getEventManager()->trigger('find', $this, array('entity' => $entity));
+ return $entity;
+ }
+
+ public function findByRequestKey($key)
+ {
+ $entity = $this->getEntityRepository()->findOneBy(array('request_key' => $key));
+ $this->getEventManager()->trigger('find', $this, array('entity' => $entity));
+ return $entity;
+ }
+
+ public function cleanExpiredVerificationRequests($expiryTime=86400)
+ {
+ $now = new \DateTime((int)$expiryTime . ' seconds ago');
+ $dql = "DELETE CdliTwoStageSignup\Entity\EmailVerification ev WHERE ev.request_time <= '" . $now->format('Y-m-d H:i:s') . "'";
+ return $this->em->createQuery($dql)->getResult();
+ }
+
+ public function insert($entity, $tableName = null, HydratorInterface $hydrator = null)
+ {
+ $this->em->persist($entity);
+ $this->em->flush();
+ return $entity;
+ }
+
+ public function remove(Model $evrModel)
+ {
+ $this->em->remove($evrModel);
+ $this->em->flush();
+ return true;
+ }
+}

0 comments on commit 283a73c

Please sign in to comment.