|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jarobe\TaskRunnerBundle\Processor; |
|
4
|
|
|
|
|
5
|
|
|
use Jarobe\TaskRunnerBundle\Exception\TaskException; |
|
6
|
|
|
use Jarobe\TaskRunnerBundle\Model\TaskResult; |
|
7
|
|
|
use Jarobe\TaskRunnerBundle\Driver\Factory\DriverFactory; |
|
8
|
|
|
use Jarobe\TaskRunnerBundle\TaskType\TaskTypeInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Processor implements ProcessorInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var DriverFactory |
|
14
|
|
|
*/ |
|
15
|
|
|
private $driverFactory; |
|
16
|
|
|
|
|
17
|
5 |
|
public function __construct(DriverFactory $driverFactory) |
|
18
|
|
|
{ |
|
19
|
5 |
|
$this->driverFactory = $driverFactory; |
|
20
|
5 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param TaskTypeInterface $task |
|
24
|
|
|
* @return TaskResult |
|
25
|
|
|
*/ |
|
26
|
5 |
|
public function process(TaskTypeInterface $task) |
|
27
|
|
|
{ |
|
28
|
5 |
|
$taskResult = new TaskResult(); |
|
29
|
|
|
|
|
30
|
|
|
try { |
|
31
|
5 |
|
$driver = $this->driverFactory->getDriverForTask($task); |
|
32
|
1 |
|
} catch (TaskException $e) { |
|
33
|
1 |
|
$exceptionMessage = sprintf("TaskException: %s", $e->getMessage()); |
|
34
|
1 |
|
$taskResult->setSuccess(false) |
|
35
|
1 |
|
->setErrors([$exceptionMessage]) |
|
36
|
|
|
; |
|
37
|
1 |
|
return $taskResult; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
//See if there's any issues with the tasks before running. |
|
42
|
4 |
|
$validationErrors = $driver->canRun($task); |
|
43
|
4 |
|
if ($validationErrors !== null && count($validationErrors) > 0) { |
|
44
|
2 |
|
$taskResult->setSuccess(false) |
|
45
|
2 |
|
->setErrors($validationErrors) |
|
46
|
|
|
; |
|
47
|
2 |
|
return $taskResult; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
//Run the task, and then check for errors |
|
51
|
2 |
|
$errors = $driver->run($task); |
|
52
|
2 |
|
if ($errors !== null && count($errors) > 0) { |
|
53
|
1 |
|
$taskResult->setSuccess(false) |
|
54
|
1 |
|
->setErrors($errors) |
|
55
|
|
|
; |
|
56
|
1 |
|
return $taskResult; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$taskResult->setSuccess(true); |
|
60
|
1 |
|
return $taskResult; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|