LogEntryRepository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Gedmo\Loggable\Document\Repository;
  3. use Doctrine\ODM\MongoDB\DocumentRepository,
  4. Doctrine\ODM\MongoDB\Cursor;
  5. /**
  6. * The LogEntryRepository has some useful functions
  7. * to interact with log entries.
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo\Loggable\Document\Repository
  11. * @subpackage LogEntryRepository
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class LogEntryRepository extends DocumentRepository
  16. {
  17. /**
  18. * Loads all log entries for the
  19. * given $document
  20. *
  21. * @param object $document
  22. * @return array
  23. */
  24. public function getLogEntries($document)
  25. {
  26. $objectClass = get_class($document);
  27. $objectMeta = $this->dm->getClassMetadata($objectClass);
  28. $objectId = $objectMeta->getReflectionProperty($objectMeta->identifier)->getValue($document);
  29. $qb = $this->createQueryBuilder();
  30. $qb->field('objectId')->equals($objectId);
  31. $qb->field('objectClass')->equals($objectMeta->name);
  32. $qb->sort('version', 'DESC');
  33. $q = $qb->getQuery();
  34. $result = $q->execute();
  35. if ($result instanceof Cursor) {
  36. $result = $result->toArray();
  37. }
  38. return $result;
  39. }
  40. /**
  41. * Reverts given $document to $revision by
  42. * restoring all fields from that $revision.
  43. * After this operation you will need to
  44. * persist and flush the $document.
  45. *
  46. * @param object $document
  47. * @param integer $version
  48. * @throws \Gedmo\Exception\UnexpectedValueException
  49. * @return void
  50. */
  51. public function revert($document, $version = 1)
  52. {
  53. $objectClass = get_class($document);
  54. $objectMeta = $this->dm->getClassMetadata($objectClass);
  55. $meta = $this->getClassMetadata();
  56. $objectId = $objectMeta->getReflectionProperty($objectMeta->identifier)->getValue($document);
  57. $qb = $this->createQueryBuilder();
  58. $qb->field('objectId')->equals($objectId);
  59. $qb->field('objectClass')->equals($objectMeta->name);
  60. $qb->field('version')->lte($version);
  61. $qb->sort('version', 'ASC');
  62. $q = $qb->getQuery();
  63. $logs = $q->execute();
  64. if ($logs instanceof Cursor) {
  65. $logs = $logs->toArray();
  66. }
  67. if ($logs) {
  68. $fields = array();
  69. foreach ($objectMeta->fieldMappings as $mapping) {
  70. if ($objectMeta->identifier !== $mapping['fieldName'] &&
  71. !$objectMeta->isCollectionValuedAssociation($mapping['fieldName'])
  72. ) {
  73. $fields[] = $mapping['fieldName'];
  74. }
  75. }
  76. $filled = false;
  77. while (($log = array_pop($logs)) && !$filled) {
  78. if ($data = $log->getData()) {
  79. foreach ($data as $field => $value) {
  80. if (in_array($field, $fields)) {
  81. if ($objectMeta->isSingleValuedAssociation($field)) {
  82. $mapping = $objectMeta->getFieldMapping($field);
  83. $value = $this->dm->getReference($mapping['targetDocument'], current($value));
  84. }
  85. $objectMeta->getReflectionProperty($field)->setValue($document, $value);
  86. unset($fields[array_search($field, $fields)]);
  87. }
  88. }
  89. }
  90. $filled = count($fields) === 0;
  91. }
  92. if (count($fields)) {
  93. throw new \Gedmo\Exception\UnexpectedValueException('Cound not fully revert the document to version: '.$version);
  94. }
  95. } else {
  96. throw new \Gedmo\Exception\UnexpectedValueException('Count not find any log entries under version: '.$version);
  97. }
  98. }
  99. }