Skip to content

Commit

Permalink
Skytells Events Pack
Browse files Browse the repository at this point in the history
  • Loading branch information
Skytells, Inc committed Dec 30, 2017
0 parents commit 13be9dc
Show file tree
Hide file tree
Showing 6 changed files with 845 additions and 0 deletions.
86 changes: 86 additions & 0 deletions CallQueuedHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Skytells\Events;

use Skytells\Contracts\Queue\Job;
use Skytells\Queue\InteractsWithQueue;
use Skytells\Contracts\Container\Container;

class CallQueuedHandler
{
/**
* The container instance.
*
* @var \Skytells\Contracts\Container\Container
*/
protected $container;

/**
* Create a new job instance.
*
* @param \Skytells\Contracts\Container\Container $container
* @return void
*/
public function __construct(Container $container)
{
$this->container = $container;
}

/**
* Handle the queued job.
*
* @param \Skytells\Contracts\Queue\Job $job
* @param array $data
* @return void
*/
public function call(Job $job, array $data)
{
$handler = $this->setJobInstanceIfNecessary(
$job, $this->container->make($data['class'])
);

call_user_func_array(
[$handler, $data['method']], unserialize($data['data'])
);

if (! $job->isDeletedOrReleased()) {
$job->delete();
}
}

/**
* Set the job instance of the given class if necessary.
*
* @param \Skytells\Contracts\Queue\Job $job
* @param mixed $instance
* @return mixed
*/
protected function setJobInstanceIfNecessary(Job $job, $instance)
{
if (in_array(InteractsWithQueue::class, class_uses_recursive(get_class($instance)))) {
$instance->setJob($job);
}

return $instance;
}

/**
* Call the failed method on the job instance.
*
* The event instance and the exception will be passed.
*
* @param array $data
* @param \Exception $e
* @return void
*/
public function failed(array $data, $e)
{
$handler = $this->container->make($data['class']);

$parameters = array_merge(unserialize($data['data']), [$e]);

if (method_exists($handler, 'failed')) {
call_user_func_array([$handler, 'failed'], $parameters);
}
}
}
141 changes: 141 additions & 0 deletions CallQueuedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Skytells\Events;

use Skytells\Container\Container;
use Skytells\Contracts\Queue\Job;
use Skytells\Queue\InteractsWithQueue;
use Skytells\Contracts\Queue\ShouldQueue;

class CallQueuedListener implements ShouldQueue
{
use InteractsWithQueue;

/**
* The listener class name.
*
* @var string
*/
public $class;

/**
* The listener method.
*
* @var string
*/
public $method;

/**
* The data to be passed to the listener.
*
* @var array
*/
public $data;

/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries;

/**
* The number of seconds the job can run before timing out.
*
* @var int
*/
public $timeout;

/**
* Create a new job instance.
*
* @param string $class
* @param string $method
* @param array $data
* @return void
*/
public function __construct($class, $method, $data)
{
$this->data = $data;
$this->class = $class;
$this->method = $method;
}

/**
* Handle the queued job.
*
* @param \Skytells\Container\Container $container
* @return void
*/
public function handle(Container $container)
{
$this->prepareData();

$handler = $this->setJobInstanceIfNecessary(
$this->job, $container->make($this->class)
);

call_user_func_array(
[$handler, $this->method], $this->data
);
}

/**
* Set the job instance of the given class if necessary.
*
* @param \Skytells\Contracts\Queue\Job $job
* @param mixed $instance
* @return mixed
*/
protected function setJobInstanceIfNecessary(Job $job, $instance)
{
if (in_array(InteractsWithQueue::class, class_uses_recursive(get_class($instance)))) {
$instance->setJob($job);
}

return $instance;
}

/**
* Call the failed method on the job instance.
*
* The event instance and the exception will be passed.
*
* @param \Exception $e
* @return void
*/
public function failed($e)
{
$this->prepareData();

$handler = Container::getInstance()->make($this->class);

$parameters = array_merge($this->data, [$e]);

if (method_exists($handler, 'failed')) {
call_user_func_array([$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;
}
}

0 comments on commit 13be9dc

Please sign in to comment.