Example #1
0
 /**
  * Return object from object pool by the given type and ID; if object is not found, return NULL.
  *
  * @param  string                   $type
  * @param  int                      $id
  * @param  bool                     $use_cache
  * @return object
  * @throws InvalidArgumentException
  */
 public function &getById($type, $id, $use_cache = true)
 {
     $registered_type = $this->requireRegisteredType($type);
     $id = (int) $id;
     if ($id < 1) {
         throw new InvalidArgumentException('ID is expected to be a number larger than 0');
     }
     if (isset($this->objects_pool[$registered_type][$id]) && $use_cache) {
         return $this->objects_pool[$registered_type][$id];
     } else {
         $type_fields = $this->getTypeFields($registered_type);
         if ($row = $this->connection->executeFirstRow($this->getSelectOneByType($registered_type), [$id])) {
             $object_class = in_array('type', $type_fields) ? $row['type'] : $type;
             /** @var object|EntityInterface $object */
             $object = new $object_class($this->connection, $this, $this->log);
             if ($object instanceof ContainerAccessInterface && $this->hasContainer()) {
                 $object->setContainer($this->getContainer());
             }
             $object->loadFromRow($row);
             return $this->addToObjectPool($registered_type, $id, $object);
         } else {
             $object = null;
             return $this->addToObjectPool($registered_type, $id, $object);
         }
     }
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function restoreFailedJobById($job_id, array $update_data = null)
 {
     $job = null;
     if ($row = $this->connection->executeFirstRow('SELECT `type`, `channel`, `data` FROM `' . self::FAILED_JOBS_TABLE_NAME . '` WHERE `id` = ?', $job_id)) {
         $this->connection->transact(function () use(&$job, $job_id, $update_data, $row) {
             $job_type = $row['type'];
             if (!class_exists($job_type)) {
                 throw new RuntimeException("Can't restore a job. Type '{$job_type}' not found");
             }
             $channel = $row['channel'];
             if (empty($channel)) {
                 $channel = QueueInterface::MAIN_CHANNEL;
             }
             if ($row['data']) {
                 if (mb_substr($row['data'], 0, 1) == '{') {
                     $data = $this->jsonDecode($row['data']);
                 } else {
                     $data = unserialize($row['data']);
                 }
             }
             if (empty($data)) {
                 $data = [];
             }
             if ($update_data && is_array($update_data) && count($update_data)) {
                 $data = array_merge($data, $update_data);
             }
             $job = new $job_type($data);
             $this->enqueue($job, $channel);
             $this->connection->execute('DELETE FROM `' . self::FAILED_JOBS_TABLE_NAME . '` WHERE `id` = ?', $job_id);
         });
     } else {
         throw new RuntimeException("Failed job #{$job_id} not found");
     }
     return $job;
 }