Passed
Pull Request — master (#2)
by Kevin
03:06 queued 01:22
created

ConfigureTasksSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\EventListener;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Zenstruck\ScheduleBundle\Event\ScheduleBuildEvent;
7
use Zenstruck\ScheduleBundle\Schedule;
8
use Zenstruck\ScheduleBundle\Schedule\Task;
9
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
10
use Zenstruck\ScheduleBundle\Schedule\Task\CompoundTask;
11
use Zenstruck\ScheduleBundle\Schedule\Task\ProcessTask;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class ConfigureTasksSubscriber implements EventSubscriberInterface
17
{
18
    private const PROCESS_TASK_PREFIX = 'bash:';
19
20
    private $config;
21
22 5
    public function __construct(array $config)
23
    {
24 5
        $this->config = $config;
25 5
    }
26
27 5
    public static function getSubscribedEvents(): array
28
    {
29 5
        return [ScheduleBuildEvent::class => 'configureTasks'];
30
    }
31
32 5
    public function configureTasks(ScheduleBuildEvent $event): void
33
    {
34 5
        foreach ($this->config as $taskConfig) {
35 5
            $this->addTask($event->getSchedule(), $taskConfig);
36
        }
37 5
    }
38
39 5
    private function addTask(Schedule $schedule, array $config): void
40
    {
41 5
        $task = $this->createTask($config['command']);
42
43 5
        $task->cron($config['frequency']);
44
45 5
        if ($config['description']) {
46 1
            $task->description($config['description']);
47
        }
48
49 5
        if ($config['without_overlapping']['enabled']) {
50 3
            $task->withoutOverlapping($config['without_overlapping']['ttl']);
51
        }
52
53 5
        if ($config['between']['enabled']) {
54 1
            $task->between($config['between']['start'], $config['between']['end']);
55
        }
56
57 5
        if ($config['unless_between']['enabled']) {
58 1
            $task->unlessBetween($config['unless_between']['start'], $config['unless_between']['end']);
59
        }
60
61 5
        if ($config['ping_before']['enabled']) {
62 1
            $task->pingBefore($config['ping_before']['url'], $config['ping_before']['method'], $config['ping_before']['options']);
63
        }
64
65 5
        if ($config['ping_after']['enabled']) {
66 1
            $task->pingAfter($config['ping_after']['url'], $config['ping_after']['method'], $config['ping_after']['options']);
67
        }
68
69 5
        if ($config['ping_on_success']['enabled']) {
70 1
            $task->pingOnSuccess($config['ping_on_success']['url'], $config['ping_on_success']['method'], $config['ping_on_success']['options']);
71
        }
72
73 5
        if ($config['ping_on_failure']['enabled']) {
74 1
            $task->pingOnFailure($config['ping_on_failure']['url'], $config['ping_on_failure']['method'], $config['ping_on_failure']['options']);
75
        }
76
77 5
        if ($config['email_after']['enabled']) {
78 1
            $task->emailAfter($config['email_after']['to'], $config['email_after']['subject']);
79
        }
80
81 5
        if ($config['email_on_failure']['enabled']) {
82 1
            $task->emailOnFailure($config['email_on_failure']['to'], $config['email_on_failure']['subject']);
83
        }
84
85 5
        $schedule->add($task);
86 5
    }
87
88 5
    private function createTask(array $commands): Task
89
    {
90 5
        if (1 === \count($commands)) {
91 3
            return $this->createSingleTask(\array_values($commands)[0]);
92
        }
93
94 2
        $task = new CompoundTask();
95
96 2
        foreach ($commands as $description => $command) {
97 2
            $subTask = $this->createSingleTask($command);
98
99 2
            if (!\is_numeric($description)) {
100 1
                $subTask->description($description);
101
            }
102
103 2
            $task->add($subTask);
104
        }
105
106 2
        return $task;
107
    }
108
109 5
    private function createSingleTask(string $command): Task
110
    {
111 5
        if (0 !== \mb_strpos($command, self::PROCESS_TASK_PREFIX)) {
112 4
            return new CommandTask($command);
113
        }
114
115 3
        return new ProcessTask(\trim(\mb_substr($command, \mb_strlen(self::PROCESS_TASK_PREFIX))));
116
    }
117
}
118