Completed
Pull Request — master (#19)
by Akihito
05:17 queued 03:40
created

Task::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Ackintosh\Snidel\Task;
3
4
use Ackintosh\Snidel\Result\Result;
5
use Bernard\Message\AbstractMessage;
6
7
class Task extends AbstractMessage implements TaskInterface
8
{
9
    /** @var callable */
10
    private $callable;
11
12
    /** @var array */
13
    private $args;
14
15
    /** @var string */
16
    private $tag;
17
18
    /**
19
     * @param   callable    $callable
20
     * @param   array       $args
21
     * @param   string      $tag
22
     */
23
    public function __construct($callable, $args, $tag)
24
    {
25
        $this->callable     = $callable;
26
        $this->args         = $args;
27
        $this->tag          = $tag;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getName()
34
    {
35
        return 'Task';
36
    }
37
38
    /**
39
     * @return  callable
40
     */
41
    public function getCallable()
42
    {
43
        return $this->callable;
44
    }
45
46
    /**
47
     * @return  mixed
48
     */
49
    public function getArgs()
50
    {
51
        return $this->args;
52
    }
53
54
    /**
55
     * @return  string|null
56
     */
57
    public function getTag()
58
    {
59
        return $this->tag;
60
    }
61
62
    /**
63
     * @return  \Ackintosh\Snidel\Result\Result
64
     */
65
    public function execute()
66
    {
67
        ob_start();
68
        $result = new Result();
69
70
        try {
71
            $result->setReturn(
72
                call_user_func_array(
73
                    $this->getCallable(),
74
                    (is_array($args = $this->getArgs())) ? $args : [$args]
75
                )
76
            );
77
        } catch (\RuntimeException $e) {
78
            ob_get_clean();
79
            throw $e;
80
        }
81
82
        $result->setOutput(ob_get_clean());
83
        $result->setTask($this);
84
85
        return $result;
86
    }
87
}
88