Ejemplo n.º 1
0
 /**
  * Restore trashed item
  * @return boolean
  * @throws Exception
  * @Ignored
  */
 public function restore()
 {
     if (!$this instanceof TrashInterface) {
         // When trying to restore normal document instead of trash item
         throw new Exception(sprintf('Restore can be performed only on `%s` instance', TrashInterface::class));
     }
     $em = new EntityManager($this->data);
     // Set scenario to `restore` for model, which is just about to be restored
     ScenarioManager::setScenario($this->data, TrashInterface::ScenarioRestore);
     if (!Event::valid($this->data, TrashInterface::EventBeforeRestore)) {
         return false;
     }
     $saved = $em->save();
     if (!$saved) {
         return false;
     }
     $finder = new Finder($this->data);
     $model = $finder->find(PkManager::prepareFromModel($this->data));
     if (!$model) {
         return false;
     }
     $eventAfter = new RestoreEvent();
     $eventAfter->setTrashed($this->data);
     $eventAfter->setTrash($this);
     if (!Event::valid($model, TrashInterface::EventAfterRestore, $eventAfter)) {
         return false;
     }
     $trashEm = new EntityManager($this);
     $this->data = null;
     // Use deleteOne, to avoid beforeDelete event,
     // which should be raised only when really removing document:
     // when emtying trash
     return $trashEm->deleteOne(PkManager::prepareFromModel($this));
 }