Skip to content

Commit

Permalink
Update procrastinator. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell authored Nov 8, 2019
1 parent 61723d2 commit d6638fe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 54 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
},
"require": {
"getdkan/procrastinator": "^3.0.0",
"getdkan/procrastinator": "^4.0.0",
"ext-curl": "*"
}
}
71 changes: 19 additions & 52 deletions src/FileFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use FileFetcher\Processor\Remote;
use FileFetcher\Processor\LastResort;
use Procrastinator\Job\AbstractPersistentJob;
use Procrastinator\Result;

/**
* @details
Expand All @@ -18,7 +17,8 @@
*/
class FileFetcher extends AbstractPersistentJob
{
private $processors = [];

private $customProcessorClasses = [];

/**
* Constructor.
Expand All @@ -44,7 +44,7 @@ protected function __construct(string $identifier, $storage, array $config = nul

// [State]

foreach ($this->processors as $processor) {
foreach ($this->getProcessors() as $processor) {
if ($processor->isServerCompatible($state)) {
$state['processor'] = get_class($processor);
$state = $processor->setupState($state);
Expand All @@ -63,51 +63,25 @@ public function setTimeLimit(int $seconds): bool
return parent::setTimeLimit($seconds);
}

public function jsonSerialize()
{
$object = parent::jsonSerialize();

$object->processor = get_class($this->getProcessor());

return $object;
}

public static function hydrate(string $json, $instance = null)
{
$data = json_decode($json);
$object = $instance;

$reflector = new \ReflectionClass(self::class);

if (!$instance) {
$object = $reflector->newInstanceWithoutConstructor();
}

$reflector = new \ReflectionClass($object);

$p = $reflector->getParentClass()->getParentClass()->getProperty('timeLimit');
$p->setAccessible(true);
$p->setValue($object, $data->timeLimit);

$p = $reflector->getParentClass()->getParentClass()->getProperty('result');
$p->setAccessible(true);
$p->setValue($object, Result::hydrate(json_encode($data->result)));

$p = $reflector->getProperty("processors");
$p->setAccessible(true);
$p->setValue($object, self::getProcessors());

return $object;
}

protected function runIt()
{
$info = $this->getProcessor()->copy($this->getState(), $this->getResult(), $this->getTimeLimit());
$this->setState($info['state']);
return $info['result'];
}

private static function getProcessors()
private function getProcessors()
{
$processors = self::getDefaultProcessors();
foreach ($this->customProcessorClasses as $processorClass) {
if ($processor = $this->getCustomProcessorInstance($processorClass)) {
$processors = array_merge([$processor], $processors);
}
}
return $processors;
}

private static function getDefaultProcessors()
{
$processors = [];
$processors[Local::class] = new Local();
Expand All @@ -118,7 +92,7 @@ private static function getProcessors()

private function getProcessor(): ProcessorInterface
{
return $this->processors[$this->getStateProperty('processor')];
return $this->getProcessors()[$this->getStateProperty('processor')];
}

private function validateConfig($config): array
Expand All @@ -137,8 +111,6 @@ private function validateConfig($config): array

private function setProcessors($config)
{
$this->processors = self::getProcessors();

if (!isset($config['processors'])) {
return;
}
Expand All @@ -147,14 +119,10 @@ private function setProcessors($config)
return;
}

foreach ($config['processors'] as $processorClass) {
$this->setProcessor($processorClass);
}

$this->processors = array_merge($this->processors, self::getProcessors());
$this->customProcessorClasses = $config['processors'];
}

private function setProcessor($processorClass)
private function getCustomProcessorInstance($processorClass)
{
if (!class_exists($processorClass)) {
return;
Expand All @@ -165,7 +133,6 @@ private function setProcessor($processorClass)
return;
}

$instance = new $processorClass();
$this->processors = array_merge([$processorClass => $instance], $this->processors);
return new $processorClass();
}
}
1 change: 0 additions & 1 deletion src/Processor/ProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* FileFetcher::FileFetcher comes in an array: $state. The array looks like this:
*
* @snippet{lineno} src/FileFetcher.php State
*
*/
interface ProcessorInterface
{
Expand Down

0 comments on commit d6638fe

Please sign in to comment.