Skip to content

Instantly share code, notes, and snippets.

@linxlad
Created September 23, 2015 10:24
Show Gist options
  • Select an option

  • Save linxlad/551679b6fa643e4ba97b to your computer and use it in GitHub Desktop.

Select an option

Save linxlad/551679b6fa643e4ba97b to your computer and use it in GitHub Desktop.
Check if any of the referee properties can verify the referral.
<?php
namespace AppBundle\EventListener\Referral;
use AppBundle\Entity\Referral;
use SM\Event\TransitionEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class CanTransitionToVerifyListener
{
/**
* canTransition
*
* Check if any of the referee properties can verify the referral.
*
* @param TransitionEvent $event
* @param string $eventName
* @param EventDispatcherInterface $dispatcher
* @return $event
*/
public function canTransition(TransitionEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
$container = $dispatcher->getContainer();
$config = $event->getConfig();
// If we are not trying to apply the verified
// status then do not continue.
if ('verified' != $config['to']) {
return;
}
// Get the state machine and the associated object.
$stateMachine = $event->getStateMachine();
$object = $stateMachine->getObject();
// If the state machine object is not a referral one (i.e another
// statemachine could have a 'verified' state but we
// want to make sure it is only on a referral).
if (!$object instanceof Referral) {
return;
}
$entityManager = $container->get('doctrine')->getEntityManager();
$userRepository = $entityManager->getRepository('AppBundle:User');
// Attempt to get the referee user object.
if (!$refereeUser = $userRepository->findOneBy(array('email' => $object->getRefereeEmail()))) {
return;
}
$propertyRepository = $entityManager->getRepository('AppBundle:Property');
// Attempt to get an array of properties owned by the referee user.
$properties = $propertyRepository->findBy(array('user' => $refereeUser->getId(), 'listingType' => 'selling'));
// Initially reject the event as we want to
// strictly control who can be verified
$event->setRejected(true);
// Loop over the referee properties anc check
// if any of them can verify the referral.
foreach ($properties as $index => $property) {
if ($property->getGoLiveDate() != null) {
// If the property has a package type is listed as selling.
if ($package = $property->getPackage()) {
// Check property criteria to determine if the status should be progressed.
// Progress to "verified" rules:
// - Pay as you go: Signed up for 8 weeks/sold.
// - Pay on completion: After trial/sold.
if (
($package->getRecurringCost() && $property->getDaysSinceGoLive() > 56) || // PAYG
(!$package->getRecurringCost() && $property->getTrialPeriodDaysRemaining() == 0) // PAYLAT
) {
// This property can verify the referral so let the state
// machine know it can transition to the verified state.
$event->setRejected(false);
}
}
}
}
return $event;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment