ScriptManager   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTasks() 0 34 5
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Task;
9
10
use Deployer\Host\Host;
11
use function Deployer\Support\array_flatten;
12
13
class ScriptManager
14
{
15
    /**
16
     * @var TaskCollection
17
     */
18
    private $tasks;
19
20
    /**
21
     * @param TaskCollection $tasks
22
     */
23 22
    public function __construct(TaskCollection $tasks)
24
    {
25 22
        $this->tasks = $tasks;
26 22
    }
27
28
    /**
29
     * Return tasks to run
30
     *
31
     * @param string $name
32
     * @param Host[] $hosts
33
     * @param bool $hooksEnabled
34
     * @return Task[]
35
     */
36
    public function getTasks($name, array $hosts = [], $hooksEnabled = true)
37
    {
38 21
        $collect = function ($name) use (&$collect, $hosts, $hooksEnabled) {
39 21
            $task = $this->tasks->get($name);
40
41 19
            if (!$task->shouldBePerformed(...array_values($hosts))) {
42
                return [];
43
            }
44
45 19
            $relatedTasks = [];
46
47 19
            if ($hooksEnabled) {
48 19
                $relatedTasks = array_merge(array_map($collect, $task->getBefore()), $relatedTasks);
49
            }
50
51 19
            if ($task instanceof GroupTask) {
52 9
                $relatedTasks = array_merge($relatedTasks, array_map($collect, $task->getGroup()));
53
            } else {
54 19
                $relatedTasks = array_merge($relatedTasks, [$task->getName()]);
55
            }
56
57 19
            if ($hooksEnabled) {
58 19
                $relatedTasks = array_merge($relatedTasks, array_map($collect, $task->getAfter()));
59
            }
60
61 19
            return $relatedTasks;
62 21
        };
63
64 21
        $script = $collect($name);
65 19
        $tasks = array_flatten($script);
66
67
        // Convert names to real tasks
68 19
        return array_map([$this->tasks, 'get'], $tasks);
69
    }
70
}
71