CallableTask   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getState() 0 7 2
A __construct() 0 5 1
A run() 0 17 3
1
<?php
2
3
namespace Robo\Collection;
4
5
use Robo\Result;
6
use Robo\Contract\TaskInterface;
7
use Robo\State\StateAwareInterface;
8
use Robo\State\Data;
9
10
/**
11
 * Creates a task wrapper that converts any Callable into an
12
 * object that can be used directly with a task collection.
13
 *
14
 * It is not necessary to use this class directly; Collection will
15
 * automatically wrap Callables when they are added.
16
 */
17
class CallableTask implements TaskInterface
18
{
19
    /**
20
     * @var callable
21
     */
22
    protected $fn;
23
24
    /**
25
     * @var \Robo\Contract\TaskInterface
26
     */
27
    protected $reference;
28
29
    public function __construct(callable $fn, TaskInterface $reference)
30
    {
31
        $this->fn = $fn;
32
        $this->reference = $reference;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function run()
39
    {
40
        $result = call_user_func($this->fn, $this->getState());
41
        // If the function returns no result, then count it
42
        // as a success.
43
        if (!isset($result)) {
44
            $result = Result::success($this->reference);
45
        }
46
        // If the function returns a result, it must either return
47
        // a \Robo\Result or an exit code.  In the later case, we
48
        // convert it to a \Robo\Result.
49
        if (!$result instanceof Result) {
50
            $result = new Result($this->reference, $result);
51
        }
52
53
        return $result;
54
    }
55
56
    /**
57
     * @return \Robo\State\Data
58
     */
59
    public function getState()
60
    {
61
        if ($this->reference instanceof StateAwareInterface) {
62
            return $this->reference->getState();
63
        }
64
        return new Data();
65
    }
66
}
67