/**
  * Create a new Log instance
  *
  * @param string $action
  * @param object $object
  * @param LoggableAdapter $ea
  * @return \Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry|null
  */
 protected function createLogEntry($action, $object, LoggableAdapter $ea)
 {
     $om = $ea->getObjectManager();
     $wrapped = AbstractWrapper::wrap($object, $om);
     $meta = $wrapped->getMetadata();
     if ($config = $this->getConfiguration($om, $meta->name)) {
         $logEntryClass = $this->getLogEntryClass($ea, $meta->name);
         $logEntryMeta = $om->getClassMetadata($logEntryClass);
         /** @var \Gedmo\Loggable\Entity\LogEntry $logEntry */
         $logEntry = $logEntryMeta->newInstance();
         $logEntry->setAction($action);
         $logEntry->setUsername($this->username);
         $logEntry->setObjectClass($meta->name);
         $logEntry->setLoggedAt();
         // check for the availability of the primary key
         $uow = $om->getUnitOfWork();
         if ($action === self::ACTION_CREATE && $ea->isPostInsertGenerator($meta)) {
             $this->pendingLogEntryInserts[spl_object_hash($object)] = $logEntry;
         } else {
             $logEntry->setObjectId($wrapped->getIdentifier());
         }
         $newValues = array();
         if ($action !== self::ACTION_REMOVE && isset($config['versioned'])) {
             foreach ($ea->getObjectChangeSet($uow, $object) as $field => $changes) {
                 if (!in_array($field, $config['versioned'])) {
                     continue;
                 }
                 $value = $changes[1];
                 if ($meta->isSingleValuedAssociation($field) && $value) {
                     $oid = spl_object_hash($value);
                     $wrappedAssoc = AbstractWrapper::wrap($value, $om);
                     $value = $wrappedAssoc->getIdentifier(false);
                     if (!is_array($value) && !$value) {
                         $this->pendingRelatedObjects[$oid][] = array('log' => $logEntry, 'field' => $field);
                     }
                 }
                 $newValues[$field] = $value;
             }
             $logEntry->setData($newValues);
         }
         if ($action === self::ACTION_UPDATE && 0 === count($newValues)) {
             return null;
         }
         $version = 1;
         if ($action !== self::ACTION_CREATE) {
             $version = $ea->getNewVersion($logEntryMeta, $object);
             if (empty($version)) {
                 // was versioned later
                 $version = 1;
             }
         }
         $logEntry->setVersion($version);
         $this->prePersistLogEntry($logEntry, $object);
         $om->persist($logEntry);
         $uow->computeChangeSet($logEntryMeta, $logEntry);
         return $logEntry;
     }
     return null;
 }
 /**
  * Create a new Log instance
  *
  * @param string $action
  * @param object $object
  * @param LoggableAdapter $ea
  * @return void
  */
 private function createLogEntry($action, $object, LoggableAdapter $ea)
 {
     $om = $ea->getObjectManager();
     $meta = $om->getClassMetadata(get_class($object));
     if ($config = $this->getConfiguration($om, $meta->name)) {
         $logEntryClass = $this->getLogEntryClass($ea, $meta->name);
         $logEntry = new $logEntryClass();
         $logEntry->setAction($action);
         $logEntry->setUsername($this->username);
         $logEntry->setObjectClass($meta->name);
         $logEntry->setLoggedAt();
         // check for the availability of the primary key
         $identifierField = $ea->getSingleIdentifierFieldName($meta);
         $objectId = $meta->getReflectionProperty($identifierField)->getValue($object);
         if (!$objectId && $action === self::ACTION_CREATE) {
             $this->pendingLogEntryInserts[spl_object_hash($object)] = $logEntry;
         }
         $uow = $om->getUnitOfWork();
         $logEntry->setObjectId($objectId);
         if ($action !== self::ACTION_REMOVE && isset($config['versioned'])) {
             $newValues = array();
             foreach ($ea->getObjectChangeSet($uow, $object) as $field => $changes) {
                 if (!in_array($field, $config['versioned'])) {
                     continue;
                 }
                 $value = $changes[1];
                 if ($meta->isSingleValuedAssociation($field) && $value) {
                     $oid = spl_object_hash($value);
                     $value = $ea->extractIdentifier($om, $value, false);
                     if (!is_array($value)) {
                         $this->pendingRelatedObjects[$oid][] = array('log' => $logEntry, 'field' => $field);
                     }
                 }
                 $newValues[$field] = $value;
             }
             $logEntry->setData($newValues);
         }
         $version = 1;
         $logEntryMeta = $om->getClassMetadata($logEntryClass);
         if ($action !== self::ACTION_CREATE) {
             $version = $ea->getNewVersion($logEntryMeta, $object);
         }
         $logEntry->setVersion($version);
         $om->persist($logEntry);
         $uow->computeChangeSet($logEntryMeta, $logEntry);
     }
 }
 /**
  * Create a new Log instance
  *
  * @param string          $action
  * @param object          $object
  * @param LoggableAdapter $ea
  *
  * @return \Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry|null
  */
 protected function createLogEntry($action, $object, LoggableAdapter $ea)
 {
     $om = $ea->getObjectManager();
     $wrapped = AbstractWrapper::wrap($object, $om);
     $meta = $wrapped->getMetadata();
     // Filter embedded documents
     if (isset($meta->isEmbeddedDocument) && $meta->isEmbeddedDocument) {
         return;
     }
     // TODO replace with more generic interface
     if ($object instanceof BlockInterface && $object->isPublish()) {
         return;
     }
     if ($config = $this->getConfiguration($om, $meta->name)) {
         $logEntryClass = $this->getLogEntryClass($ea, $meta->name);
         $logEntryMeta = $om->getClassMetadata($logEntryClass);
         /** @var \Gedmo\Loggable\Entity\LogEntry $logEntry */
         $logEntry = $logEntryMeta->newInstance();
         $logEntry->setAction($action);
         $logEntry->setUsername($this->username);
         $logEntry->setObjectClass($meta->name);
         $logEntry->setLoggedAt();
         // check for the availability of the primary key
         /** @var UnitOfWork $uow */
         $uow = $om->getUnitOfWork();
         if ($action === self::ACTION_CREATE && $ea->isPostInsertGenerator($meta)) {
             $this->pendingLogEntryInserts[spl_object_hash($object)] = $logEntry;
         } else {
             $logEntry->setObjectId($wrapped->getIdentifier());
         }
         $newValues = array();
         if (isset($config['versioned'])) {
             //$action !== self::ACTION_REMOVE &&
             $newValues = $this->getObjectChangeSetData($ea, $object, $logEntry);
             $logEntry->setData($newValues);
             if (isset($this->changeSets[spl_object_hash($object)])) {
                 // Recalculate changeset data from scratch because we are overwriting
                 // and we are at risk to store only the latest change if we are flushing
                 // the object multiple times in the same request. This happens for example
                 // when we are sorting objects.
                 $prevValues = $this->changeSets[spl_object_hash($object)];
                 $newValues = array_merge($prevValues, $newValues);
             } else {
                 $this->changeSets[spl_object_hash($object)] = $newValues;
             }
         }
         if ($action === self::ACTION_UPDATE && 0 === count($newValues)) {
             return null;
         }
         $version = 1;
         if ($action !== self::ACTION_CREATE) {
             $version = $ea->getNewVersion($logEntryMeta, $object);
             if (empty($version)) {
                 // was versioned later
                 $version = 1;
             }
         }
         $logEntry->setVersion($version);
         $this->prePersistLogEntry($logEntry, $object);
         // TODO replace with more generic code
         if ($object instanceof BlockInterface) {
             // Check if this rootVersion has an Entry already, if so, update that instead
             $existingLogEntry = $om->getRepository($logEntryClass)->findOneBy(['rootVersion' => $this->rootVersion, 'objectId' => $object->getId()]);
             if ($existingLogEntry) {
                 unset($this->pendingLogEntryInserts[spl_object_hash($object)]);
                 $logEntry = $existingLogEntry;
                 $logEntry->setUsername($this->username);
                 $logEntry->setObjectClass($meta->name);
                 $logEntry->setLoggedAt();
                 $logEntry->setData($newValues);
             }
             if ($action !== self::ACTION_CREATE) {
                 $uow->clearEntityChangeSet(spl_object_hash($object));
                 //                    $uow->detach($object);
             }
             // Reset version number to that of owner Document, so we keep a tree
             // of blocks together on a single version #
             $logEntry->setRootVersion($this->rootVersion);
             $logEntry->setRootId($object instanceof BlockOwnerInterface ? $object->getId() : $object->getOwner()->getId());
             //                $uow->computeChangeSet($logEntryMeta, $logEntry);
         }
         $om->persist($logEntry);
         $uow->computeChangeSet($logEntryMeta, $logEntry);
         return $logEntry;
     }
     return null;
 }
 /**
  * Create a new Log instance
  *
  * @param string          $action
  * @param object          $object
  * @param LoggableAdapter $ea
  *
  * @return \Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry|null
  */
 protected function createLogEntry($action, $object, LoggableAdapter $ea)
 {
     $om = $ea->getObjectManager();
     $wrapped = AbstractWrapper::wrap($object, $om);
     $meta = $wrapped->getMetadata();
     // Filter embedded documents
     $ident = $meta->getIdentifier();
     if (empty($ident) || empty($ident[0])) {
         return;
     }
     if ($config = $this->getConfiguration($om, $meta->name)) {
         $logEntryClass = $this->getLogEntryClass($ea, $meta->name);
         $logEntryMeta = $om->getClassMetadata($logEntryClass);
         /** @var \Gedmo\Loggable\Entity\LogEntry $logEntry */
         $logEntry = $logEntryMeta->newInstance();
         $logEntry->setAction($action);
         $logEntry->setUsername($this->username);
         $logEntry->setObjectClass($meta->name);
         $logEntry->setLoggedAt();
         // check for the availability of the primary key
         $uow = $om->getUnitOfWork();
         if ($action === self::ACTION_CREATE && $ea->isPostInsertGenerator($meta)) {
             $this->pendingLogEntryInserts[spl_object_hash($object)] = $logEntry;
         } else {
             $logEntry->setObjectId($wrapped->getIdentifier());
         }
         $newValues = array();
         if ($action !== self::ACTION_REMOVE && isset($config['versioned'])) {
             $newValues = $this->getObjectChangeSetData($ea, $object, $logEntry);
             $logEntry->setData($newValues);
         }
         if ($action === self::ACTION_UPDATE && 0 === count($newValues)) {
             return null;
         }
         $version = 1;
         if ($action !== self::ACTION_CREATE) {
             $version = $ea->getNewVersion($logEntryMeta, $object);
             if (empty($version)) {
                 // was versioned later
                 $version = 1;
             }
         }
         $logEntry->setVersion($version);
         $this->prePersistLogEntry($logEntry, $object);
         $om->persist($logEntry);
         $uow->computeChangeSet($logEntryMeta, $logEntry);
         return $logEntry;
     }
     return null;
 }
 /**
  * @param LoggableAdapter $ea
  * @param Log             $logEntry
  * @param                 $object
  * @param                 $message
  * @return LogParent
  */
 protected function addLogParent(LoggableAdapter $ea, Log $logEntry, $object, $message, $childObject, $fieldName)
 {
     $om = $ea->getObjectManager();
     $parentLogEntry = new LogParent();
     $parentLogEntry->setAction($message);
     $parentLogEntry->setUsername($this->username);
     $parentLogEntry->setSourceUsername($this->sourceUsername);
     $wrappedParent = AbstractWrapper::wrap($object, $om);
     $parentLogEntry->setObjectClass($wrappedParent->getMetadata()->name);
     $parentLogEntry->setObjectId($wrappedParent->getIdentifier());
     $parentLogEntry->setFieldName($fieldName);
     $logEntry->addParent($parentLogEntry);
     $this->pendingParents[spl_object_hash($object)][] = array('log' => $parentLogEntry, 'field' => 'objectId');
     $this->pendingParents[spl_object_hash($childObject)][] = array('log' => $parentLogEntry, 'field' => 'childId');
     return $parentLogEntry;
 }