/**
  * Runs method od Entity with name $methodName and writes down result into the Store.
  *
  * If you don't wont to save result in the Store the method $methodName must return null.
  *
  * The method $methodName can receive not greater then two mixed parameters: $param1 and $param2.
  *
  * @param $methodName
  * @param mixed|null $param1
  * @param mixed|null $params2
  * @return string
  * @throws $this::EXCEPTION_CLASS
  */
 protected function runTransaction($methodName, $param1 = null, $params2 = null)
 {
     try {
         $errorMsg = "Can't start transaction for the method \"{$methodName}\"";
         $this->store->beginTransaction();
         //is row with this index exist?
         $errorMsg = "Can't execute the method \"readAndLock\" for {$this->id}";
         $data = $this->getStoredData($this->id, true);
         $errorMsg = "Cannot execute the method \"{$methodName}\". Entity does not exist.";
         if (is_null($data)) {
             throw new \Exception("Entity does not exist in the Store.");
         }
         $entityClass = $this->getClass();
         $entity = new $entityClass($data);
         $errorMsg = "Cannot execute the method \"{$methodName}\". Class: {$entityClass}";
         $dataReturned = call_user_func([$entity, $methodName], $param1, $params2);
         if (!is_null($dataReturned)) {
             $errorMsg = "Cannot execute the method \"store->update\".";
             $id = $dataReturned[StoreAbstract::ID];
             unset($dataReturned[StoreAbstract::ID]);
             //or update
             $number = $this->store->update($dataReturned, [StoreAbstract::ID => $id]);
             if (!$number) {
                 //or create a new one if absent
                 $dataReturned[StoreAbstract::ID] = $id;
                 $this->store->insert($dataReturned);
             }
         } else {
             $dataReturned = $data;
             $id = $this->id;
         }
         $this->store->commit();
         return $id;
     } catch (\Exception $e) {
         $this->store->rollback();
         $exceptionClass = $this::EXCEPTION_CLASS;
         throw new $exceptionClass($errorMsg . ' Id: ' . $this->id, 0, $e);
     }
 }
Exemple #2
0
 public function __construct($table, AdapterInterface $adapter, Message\Store $messagesStore, Promise\Store $promisesStore)
 {
     parent::__construct($table, $adapter);
     $this->messagesStore = $messagesStore;
     $this->promisesStore = $promisesStore;
 }