EventArchiveManager.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Muzich\CoreBundle\Managers;
  3. use Doctrine\ORM\EntityManager;
  4. use Muzich\CoreBundle\Entity\User;
  5. use Muzich\CoreBundle\Entity\EventArchive;
  6. /**
  7. *
  8. *
  9. * @author bux
  10. */
  11. class EventArchiveManager
  12. {
  13. /**
  14. *
  15. * @var EntityManager
  16. */
  17. private $em;
  18. /**
  19. *
  20. * @var EventArchive
  21. */
  22. private $archive;
  23. /**
  24. *
  25. * @var boolean
  26. */
  27. private $new;
  28. public function __construct(EntityManager $em)
  29. {
  30. $this->em = $em;
  31. }
  32. private function determineArchive(User $user, $type)
  33. {
  34. try
  35. {
  36. $this->archive = $this->em->createQuery(
  37. 'SELECT a FROM MuzichCoreBundle:EventArchive a
  38. WHERE a.user = :uid AND a.type = :type'
  39. )->setParameters(array(
  40. 'uid' => $user->getId(),
  41. 'type' => $type
  42. ))->getSingleResult()
  43. ;
  44. $this->new = false;
  45. }
  46. catch (\Doctrine\ORM\NoResultException $e)
  47. {
  48. $this->archive = new EventArchive();
  49. $this->new = true;
  50. }
  51. }
  52. private function initArchive(User $user, $type)
  53. {
  54. $this->archive->setUser($user);
  55. $this->archive->setCount(1);
  56. $this->archive->setType($type);
  57. }
  58. private function incrementArchive()
  59. {
  60. $this->archive->setCount($this->archive->getCount()+1);
  61. }
  62. public function add(User $user, $type)
  63. {
  64. $this->determineArchive($user, $type);
  65. if ($this->new)
  66. {
  67. $this->initArchive($user, $type);
  68. }
  69. else
  70. {
  71. $this->incrementArchive();
  72. }
  73. $this->em->persist($this->archive);
  74. }
  75. }