Completed
Pull Request — master (#1)
by Jim
08:57
created

Processor::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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