LogEntryRepository.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Gedmo\Loggable\Document\Repository;
  3. use Gedmo\Tool\Wrapper\MongoDocumentWrapper;
  4. use Gedmo\Loggable\LoggableListener;
  5. use Doctrine\ODM\MongoDB\DocumentRepository,
  6. Doctrine\ODM\MongoDB\Cursor;
  7. /**
  8. * The LogEntryRepository has some useful functions
  9. * to interact with log entries.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo\Loggable\Document\Repository
  13. * @subpackage LogEntryRepository
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class LogEntryRepository extends DocumentRepository
  18. {
  19. /**
  20. * Currently used loggable listener
  21. *
  22. * @var LoggableListener
  23. */
  24. private $listener;
  25. /**
  26. * Loads all log entries for the
  27. * given $document
  28. *
  29. * @param object $document
  30. * @return array
  31. */
  32. public function getLogEntries($document)
  33. {
  34. $wrapped = new MongoDocumentWrapper($document, $this->dm);
  35. $objectId = $wrapped->getIdentifier();
  36. $qb = $this->createQueryBuilder();
  37. $qb->field('objectId')->equals($objectId);
  38. $qb->field('objectClass')->equals($wrapped->getMetadata()->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. $wrapped = new MongoDocumentWrapper($document, $this->dm);
  61. $objectMeta = $wrapped->getMetadata();
  62. $meta = $this->getClassMetadata();
  63. $objectId = $wrapped->getIdentifier();
  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'], $value) : null;
  85. }
  86. $wrapped->setPropertyValue($field, $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. }