Async/src/FuzeWorks/Async/TaskStorage/DummyTaskStorage.php

291 lines
8.1 KiB
PHP

<?php
/**
* FuzeWorks Async Library
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2020, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.0.0
*
* @version Version 1.0.0
*/
namespace FuzeWorks\Async\TaskStorage;
use FuzeWorks\Async\Events\TaskModifyEvent;
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Async\TaskStorage;
use FuzeWorks\Events;
use FuzeWorks\Logger;
/**
* Class DummyTaskStorage
*
* DummyTaskStorage is used when you don't want persistent task storage. Data is simply stored in an array and
* not saved anywhere. When the program terminates all contents are disposed off. Particularly useful for testing
* and development.
*
* @package FuzeWorks\Async\TaskStorage
*/
class DummyTaskStorage implements TaskStorage
{
/**
* Contains the entire task storage
*
* @var Task[]
*/
protected $tasks;
/**
* An array containing all task outputs
*
* @var array
*/
protected $taskOutput;
/**
* @inheritDoc
*/
public function __construct(array $parameters)
{
$this->tasks = array();
$this->taskOutput = array();
}
/**
* @inheritDoc
* @throws TasksException
*/
public function addTask(Task $task): bool
{
// Check if the already exists
$taskId = $task->getId();
foreach ($this->tasks as $t)
{
if ($t->getId() === $taskId)
throw new TasksException("Could not add Task to TaskStorage. Task '$taskId' already exists.");
}
$this->tasks[] = clone $task;
return true;
}
/**
* @inheritDoc
*/
public function readTasks(bool $noIncludeDone = false): array
{
if ($noIncludeDone === false)
return $this->tasks;
$tasks = [];
foreach ($this->tasks as $task)
{
if ($task->getStatus() !== Task::COMPLETED && $task->getStatus() !== Task::CANCELLED)
$tasks[] = $task;
}
return $tasks;
}
/**
* @inheritDoc
*/
public function getTaskById(string $identifier): Task
{
foreach ($this->tasks as $t)
if ($t->getId() === $identifier)
return $t;
throw new TasksException("Could not get task by id. Task not found.");
}
/**
* @inheritDoc
*/
public function modifyTask(Task $task): bool
{
// Fire the TaskModifyEvent
/** @var TaskModifyEvent $event */
$event = Events::fireEvent(new TaskModifyEvent(), $task);
if ($event->isCancelled())
{
Logger::log("Did not modify task. Cancelled by taskModifyEvent.");
return false;
}
// And finally replace Task with the event based one.
$task = $event->getTask();
$taskId = $task->getId();
for ($i=0;$i<count($this->tasks);$i++)
{
if ($this->tasks[$i]->getId() === $taskId)
{
$this->tasks[$i] = clone $task;
return true;
}
}
throw new TasksException("Could not modify task. Task '$taskId' doesn't exist.");
}
/**
* @inheritDoc
* @throws TasksException
*/
public function deleteTask(Task $task): bool
{
$taskId = $task->getId();
for ($i=0;$i<count($this->tasks);$i++)
{
if ($this->tasks[$i]->getId() === $taskId)
{
// Remove the task from the main storage
unset($this->tasks[$i]);
return true;
}
}
throw new TasksException("Could not delete task. Task '$taskId' doesn't exist.");
}
/**
* @inheritDoc
*/
public function writeTaskOutput(Task $task, string $output, string $errors, int $statusCode): bool
{
// First check if the task exists
$task = $this->getTaskById($task->getId());
// Set the attempt number
if (!isset($this->taskOutput[$task->getId()]['outAttempts']))
{
$this->taskOutput[$task->getId()]['outAttempts'] = 1;
$attempt = $this->taskOutput[$task->getId()]['outAttempts'];
}
else
$attempt = $this->taskOutput[$task->getId()]['outAttempts']++;
if (isset($this->taskOutput[$task->getId()]['task'][$attempt]))
throw new TasksException("Could not write task output. Output already written.");
$this->taskOutput[$task->getId()]['task'][$attempt] = [
'output' => $output,
'errors' => $errors,
'statusCode' => $statusCode
];
return true;
}
/**
* @inheritDoc
*/
public function writePostOutput(Task $task, string $output, string $errors, int $statusCode): bool
{
// First check if the task exists
$task = $this->getTaskById($task->getId());
// Set the attempt number
if (!isset($this->taskOutput[$task->getId()]['postAttempts']))
{
$this->taskOutput[$task->getId()]['postAttempts'] = 1;
$attempt = $this->taskOutput[$task->getId()]['postAttempts'];
}
else
$attempt = $this->taskOutput[$task->getId()]['postAttempts']++;
if (isset($this->taskOutput[$task->getId()]['post'][$attempt]))
throw new TasksException("Could not write task post output. Output already written.");
$this->taskOutput[$task->getId()]['post'][$attempt] = [
'output' => $output,
'errors' => $errors,
'statusCode' => $statusCode
];
return true;
}
/**
* @inheritDoc
*/
public function readTaskOutput(Task $task, int $attempt = 0): ?array
{
if (!isset($this->taskOutput[$task->getId()]['task']))
return null;
if ($attempt === 0)
$attempt = count($this->taskOutput[$task->getId()]['task']);
if ($attempt === -1)
return $this->taskOutput[$task->getId()]['task'];
else
{
if (isset($this->taskOutput[$task->getId()]['task'][$attempt]))
return $this->taskOutput[$task->getId()]['task'][$attempt];
}
return null;
}
/**
* @inheritDoc
*/
public function readPostOutput(Task $task, int $attempt = 0): ?array
{
if (!isset($this->taskOutput[$task->getId()]['post']))
return null;
if ($attempt === 0)
$attempt = count($this->taskOutput[$task->getId()]['post']);
if ($attempt === -1)
return $this->taskOutput[$task->getId()]['post'];
else
{
if (isset($this->taskOutput[$task->getId()]['post'][$attempt]))
return $this->taskOutput[$task->getId()]['post'][$attempt];
}
return null;
}
/**
* @inheritDoc
*/
public function reset(): bool
{
$this->tasks = [];
$this->taskOutput = [];
return true;
}
}