PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ServerGrove/SGLiveChatBundle/Document/VisitorRepository.php

https://github.com/casoetan/ServerGroveLiveChat
PHP | 52 lines | 35 code | 9 blank | 8 comment | 2 complexity | af4a3b7f108a7383a90c05e7747e0bec MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. namespace ServerGrove\SGLiveChatBundle\Document;
  3. use Doctrine\ODM\MongoDB\DocumentRepository;
  4. /**
  5. * Description of VisitRepository
  6. *
  7. * @author Ismael Ambrosi<ismael@servergrove.com>
  8. */
  9. class VisitorRepository extends DocumentRepository
  10. {
  11. const REPOSITORY_NAME = 'SGLiveChatBundle:Visitor';
  12. public function create(array $args)
  13. {
  14. $visitor = new Visitor();
  15. foreach ($args as $k => $v) {
  16. $methodName = 'set' . ucfirst($k);
  17. if (method_exists($visitor, $methodName)) {
  18. call_user_func(array(
  19. $visitor,
  20. $methodName), $v);
  21. }
  22. }
  23. $visitor->setKey(md5(time() . $visitor->getAgent() . rand(0, 100)));
  24. return $visitor;
  25. }
  26. public function persist(Visitor $visitor)
  27. {
  28. $this->getDocumentManager()->persist($visitor);
  29. $this->getDocumentManager()->flush();
  30. }
  31. /**
  32. * @return ServerGrove\SGLiveChatBundle\Document\Visitor
  33. */
  34. public function getByKey($key)
  35. {
  36. $visitor = null;
  37. if (!is_null($key)) {
  38. $visitor = $this->findOneBy(array(
  39. 'key' => $key));
  40. }
  41. return $visitor;
  42. }
  43. }