data = $data; $this->class = $class; $this->method = $method; } /** * Handle the queued job. * * @param \Illuminate\Container\Container $container * @return void */ public function handle(Container $container) { $this->prepareData(); $handler = $this->setJobInstanceIfNecessary( $this->job, $container->make($this->class) ); $handler->{$this->method}(...array_values($this->data)); } /** * Set the job instance of the given class if necessary. * * @param \Illuminate\Contracts\Queue\Job $job * @param object $instance * @return object */ protected function setJobInstanceIfNecessary(Job $job, $instance) { if (in_array(InteractsWithQueue::class, class_uses_recursive($instance))) { $instance->setJob($job); } return $instance; } /** * Call the failed method on the job instance. * * The event instance and the exception will be passed. * * @param \Throwable $e * @return void */ public function failed($e) { $this->prepareData(); $handler = Container::getInstance()->make($this->class); $parameters = array_merge(array_values($this->data), [$e]); if (method_exists($handler, 'failed')) { $handler->failed(...$parameters); } } /** * Unserialize the data if needed. * * @return void */ protected function prepareData() { if (is_string($this->data)) { $this->data = unserialize($this->data); } } /** * Get the display name for the queued job. * * @return string */ public function displayName() { return $this->class; } /** * Prepare the instance for cloning. * * @return void */ public function __clone() { $this->data = array_map(function ($data) { return is_object($data) ? clone $data : $data; }, $this->data); } }