LogEntryRepository.php 4.7 KB

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