Skip to content

Commit

Permalink
Merge pull request FriendsOfSymfony#196 from Remixjobs/fix-calculate-…
Browse files Browse the repository at this point in the history
…scire-on-user-update

Fix calculate scire on user update
  • Loading branch information
jeremymarc committed Sep 20, 2012
2 parents b97ac27 + 906d777 commit ab5ded5
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 59 deletions.
6 changes: 0 additions & 6 deletions app/config/routing.yml
Expand Up @@ -46,12 +46,6 @@ fos_oauth_server_authorize:
rj_client_cv:
resource: "@RjCvClientBundle/Resources/config/routing.yml"

#dont understand why it's not working without that ?
rj_cv_client_score:
pattern: /account/cv/score
defaults:
_controller: RjCvClientBundle:Score:get

_imagine:
resource: .
type: imagine
Expand Down
2 changes: 1 addition & 1 deletion src/Rj/CoreBundle/Entity/User.php
Expand Up @@ -156,7 +156,7 @@ class User extends BaseUser implements PublicIdInterface
* @Assert\Max(limit = "180")
* @Assert\Type(type="integer")
*
* @ORM\Column(name="profile_completed_score", type="smallint", nullable=false)
* @ORM\Column(name="profile_completed_score", type="float", nullable=false)
*/
protected $profileCompletedScore;

Expand Down
Expand Up @@ -3,13 +3,14 @@

use Doctrine\ORM\Events;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;

use Rj\CoreBundle\DomainCommand\CalculateProfileCompletedScoreCommand;
use Rj\CoreBundle\Entity\User;
use Rj\CoreBundle\Entity\Skill;
use Rj\CoreBundle\Entity\Study;
use Rj\CoreBundle\Entity\UserSocialProfile;
use Rj\CoreBundle\Entity\Tag;

/**
* @author Kotlyar Maksim <kotlyar.maksim@gmail.com>
Expand All @@ -22,8 +23,6 @@ class CalculateProfileCompletedScoreListener implements EventSubscriber
*/
protected $calculateProfileCompletedScoreCommand;

protected $logger;

/**
* @var array
*/
Expand All @@ -37,20 +36,13 @@ public function __construct(CalculateProfileCompletedScoreCommand $calculateProf
$this->calculateProfileCompletedScoreCommand = $calculateProfileCompletedScoreCommand;
}

public function setLogger($logger)
{
$this->logger = $logger;
}

/**
* @param \Doctrine\ORM\Event\LifecycleEventArgs $event
*/
public function preFlush(PreFlushEventArgs $event)
public function onFlush(OnFlushEventArgs $event)
{
/**
* @var $uow \Doctrine\ORM\UnitOfWork
*/
$uow = $event->getEntityManager()->getUnitOfWork();
$om = $event->getEntityManager();
$uow = $om->getUnitOfWork();

$scheduledEntities = array_merge(
$uow->getScheduledEntityDeletions(),
Expand All @@ -60,28 +52,54 @@ public function preFlush(PreFlushEventArgs $event)

$this->calculatedUsers = array();
foreach ($scheduledEntities as $scheduledEntity) {
$user = null;

if ($scheduledEntity instanceof Skill || $scheduledEntity instanceof Study) {
$user = $scheduledEntity->getOwner();
}
if ($scheduledEntity instanceof UserSocialProfile) {
$user = $scheduledEntity->getUser();
if (false == $user = $this->guessUserByScheduledEntity($scheduledEntity)) {
continue;
}
if ($scheduledEntity instanceof User) {
$user = $scheduledEntity;
if (array_key_exists(spl_object_hash($user), $this->calculatedUsers)) {
continue;
}

//tag

if ($user && false == isset($this->calculatedUsers[spl_object_hash($user)])) {
$user->setProfileCompletedScore(
$this->calculateProfileCompletedScoreCommand->calculate($user)

$score = $this->calculateProfileCompletedScoreCommand->calculate($user);

$user->setProfileCompletedScore($score);

if (in_array($user, $scheduledEntities)) {
$uow->recomputeSingleEntityChangeSet(
$om->getClassMetadata(get_class($user)),
$user
);
} else {
$uow->computeChangeSet(
$om->getClassMetadata(get_class($user)),
$user
);

$this->calculatedUsers[spl_object_hash($user)] = $user;
}

$this->calculatedUsers[spl_object_hash($user)] = $user;
}
}

/**
* @param mixed $scheduledEntity
*
* @return null|\Rj\CoreBundle\Entity\User
*/
protected function guessUserByScheduledEntity($scheduledEntity)
{
$user = null;
if ($scheduledEntity instanceof Skill) {
$user = $scheduledEntity->getOwner();
} else if ($scheduledEntity instanceof Study) {
$user = $scheduledEntity->getOwner();
} else if ($scheduledEntity instanceof UserSocialProfile) {
$user = $scheduledEntity->getUser();
} else if ($scheduledEntity instanceof User) {
$user = $scheduledEntity;
} else if ($scheduledEntity instanceof Tag) {
$user = $scheduledEntity->getCreatedBy();
}

return $user;
}

/**
Expand All @@ -90,7 +108,7 @@ public function preFlush(PreFlushEventArgs $event)
public function getSubscribedEvents()
{
return array(
Events::preFlush,
Events::onFlush,
);
}
}
2 changes: 0 additions & 2 deletions src/Rj/CoreBundle/Resources/config/listeners.yml
Expand Up @@ -10,7 +10,5 @@ services:
class: Rj\CoreBundle\EventListener\Doctrine\CalculateProfileCompletedScoreListener
arguments:
- @domain_command.calculate_profile_completed_score
calls:
- [setLogger, [@logger]]
tags:
- { name: doctrine.event_subscriber }
Expand Up @@ -2,13 +2,15 @@
namespace Rj\CoreBundle\Tests\EventListener\Doctrine;

use Doctrine\ORM\Events;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;

use Rj\CoreBundle\EventListener\Doctrine\CalculateProfileCompletedScoreListener;
use Rj\CoreBundle\Entity\User;
use Rj\CoreBundle\Entity\Skill;
use Rj\CoreBundle\Entity\Study;
use Rj\CoreBundle\Entity\UserSocialProfile;
use Rj\CoreBundle\Entity\Tag;

/**
* @author Kotlyar Maksim <kotlyar.maksim@gmail.com>
Expand Down Expand Up @@ -41,7 +43,7 @@ public function shouldSubscribeOnPreFlush()
{
$listener = new CalculateProfileCompletedScoreListener($this->createCalculateProfileCompletedScoreCommandMock());

$this->assertContains(Events::preFlush, $listener->getSubscribedEvents());
$this->assertContains(Events::onFlush, $listener->getSubscribedEvents());
}

/**
Expand All @@ -66,7 +68,7 @@ public function shouldNotPassNoUserEntity()

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -103,7 +105,7 @@ public function shouldCalculateScoreIfUserScheduledForUpdateAndSetItToUser()

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -140,7 +142,7 @@ public function shouldCalculateScoreIfUserScheduledForInsertAndSetItToUser()

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -180,7 +182,7 @@ public function shouldCalculateScoreIfSkillScheduledForUpdateAndSetItToSkillUser

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -220,7 +222,7 @@ public function shouldCalculateScoreIfSkillScheduledForInsertAndSetItToSkillUser

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}


Expand Down Expand Up @@ -261,7 +263,7 @@ public function shouldCalculateScoreIfStudyScheduledForUpdateAndSetItToStudyUser

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -301,7 +303,7 @@ public function shouldCalculateScoreIfStudyScheduledForInsertAndSetItToStudyUser

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -341,7 +343,7 @@ public function shouldCalculateScoreIfUserSocialProfileScheduledForInsertAndSetI

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -381,7 +383,7 @@ public function shouldCalculateScoreIfUserSocialProfileScheduledForUpdateAndSetI

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -421,7 +423,7 @@ public function shouldCalculateScoreIfUserSocialProfileScheduledForDeleteAndSetI

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -461,7 +463,7 @@ public function shouldNotCalculateScoreTwiceIfUserAndStudyScheduledAtTheSameTime

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -501,7 +503,47 @@ public function shouldNotCalculateScoreTwiceIfUserAndSkillScheduledAtTheSameTime

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->preFlush(new PreFlushEventArgs($entityManagerStub));
$listener->onFlush(new OnFlushEventArgs($entityManagerStub));
}

/**
* @test
*/
public function shouldCalculateScoreIfTagScheduledToUpdate()
{
$calculatedScore = 10;

$user = $this->createUserMock();
$user
->expects($this->once())
->method('setProfileCompletedScore')
->with($this->equalTo($calculatedScore))
;

$tag = new Tag();
$tag->setCreatedBy($user);

$unitOfWorkStub = $this->createUnitOfWorkStub(
$returnGetScheduledEntityUpdates = array($tag),
$returnGetScheduledEntityInsertions = array(),
$returnGetScheduledEntityDeletions = array()
);

$entityManagerStub = $this->createEntityManagerStub(
$returnGetUnitOfWork = $unitOfWorkStub
);

$commandMock = $this->createCalculateProfileCompletedScoreCommandMock();
$commandMock
->expects($this->once())
->method('calculate')
->with($this->equalTo($user))
->will($this->returnValue($calculatedScore))
;

$listener = new CalculateProfileCompletedScoreListener($commandMock);

$listener->onFlush(new onFlushEventArgs($entityManagerStub));
}

/**
Expand Down Expand Up @@ -532,7 +574,13 @@ protected function createEntityManagerStub($returnGetUnitOfWork = null)
->method('getUnitOfWork')
->will($this->returnValue($returnGetUnitOfWork))
;


$entityManagerMock
->expects($this->any())
->method('getClassMetadata')
->will($this->returnValue(new ClassMetadata('foo')))
;

return $entityManagerMock;
}

Expand Down
11 changes: 7 additions & 4 deletions src/Rj/CvBundle/Resources/config/routing.yml
Expand Up @@ -31,18 +31,16 @@ rj_cv_client_theme_view_current:
_format: html
theme: null
requirements:
userId: \d+
_format: html|pdf

rj_cv_client_theme_view_custom:
pattern: /cv/{theme}/{userPublicId}.{_format}
defaults:
_controller: RjCvClientBundle:CvTheme:view
_format: html
requirements:
userId: \d+
_format: html|pdf
theme: .+
theme: \w+

rj_cv_client_import_linkedin:
pattern: /account/import/linkedin
Expand All @@ -53,4 +51,9 @@ rj_cv_client_import_viadeo:
pattern: /account/import/viadeo
defaults:
_controller: RjCvClientBundle:Import:viadeo

rj_cv_client_score:
pattern: /account/cv/score
defaults:
_controller: RjCvClientBundle:Score:get
#end copy paste

0 comments on commit ab5ded5

Please sign in to comment.