TaskConfigurationSubscriber   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 24
eloc 48
c 0
b 0
f 0
dl 0
loc 115
ccs 54
cts 54
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
F addTask() 0 51 12
A __construct() 0 3 1
A getSubscribedEvents() 0 3 1
A createTask() 0 19 4
A removePrefix() 0 3 1
A configureTasks() 0 4 2
A createSingleTask() 0 11 3
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\EventListener;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Zenstruck\ScheduleBundle\Event\BuildScheduleEvent;
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\PingTask;
12
use Zenstruck\ScheduleBundle\Schedule\Task\ProcessTask;
13
14
/**
15
 * @author Kevin Bond <[email protected]>
16
 */
17
final class TaskConfigurationSubscriber implements EventSubscriberInterface
18
{
19
    private const PROCESS_TASK_PREFIX = 'bash:';
20
    private const PING_TASK_PREFIX = 'ping:';
21
22
    /** @var array */
23
    private $config;
24 7
25
    public function __construct(array $config)
26 7
    {
27 7
        $this->config = $config;
28
    }
29 7
30
    public static function getSubscribedEvents(): array
31 7
    {
32
        return [BuildScheduleEvent::class => 'configureTasks'];
33
    }
34 7
35
    public function configureTasks(BuildScheduleEvent $event): void
36 7
    {
37 7
        foreach ($this->config as $taskConfig) {
38
            $this->addTask($event->getSchedule(), $taskConfig);
39 7
        }
40
    }
41 7
42
    private function addTask(Schedule $schedule, array $config): void
43 7
    {
44
        $task = $this->createTask($config['task']);
45 7
46
        $task->cron($config['frequency']);
47 7
48 2
        if ($config['description']) {
49
            $task->description($config['description']);
50
        }
51 7
52 1
        if ($config['timezone']) {
53
            $task->timezone($config['timezone']);
54
        }
55 7
56 3
        if ($config['without_overlapping']['enabled']) {
57
            $task->withoutOverlapping($config['without_overlapping']['ttl']);
58
        }
59 7
60 1
        if ($config['only_between']['enabled']) {
61
            $task->onlyBetween($config['only_between']['start'], $config['only_between']['end']);
62
        }
63 7
64 1
        if ($config['unless_between']['enabled']) {
65
            $task->unlessBetween($config['unless_between']['start'], $config['unless_between']['end']);
66
        }
67 7
68 1
        if ($config['ping_before']['enabled']) {
69
            $task->pingBefore($config['ping_before']['url'], $config['ping_before']['method'], $config['ping_before']['options']);
70
        }
71 7
72 1
        if ($config['ping_after']['enabled']) {
73
            $task->pingAfter($config['ping_after']['url'], $config['ping_after']['method'], $config['ping_after']['options']);
74
        }
75 7
76 1
        if ($config['ping_on_success']['enabled']) {
77
            $task->pingOnSuccess($config['ping_on_success']['url'], $config['ping_on_success']['method'], $config['ping_on_success']['options']);
78
        }
79 7
80 1
        if ($config['ping_on_failure']['enabled']) {
81
            $task->pingOnFailure($config['ping_on_failure']['url'], $config['ping_on_failure']['method'], $config['ping_on_failure']['options']);
82
        }
83 7
84 1
        if ($config['email_after']['enabled']) {
85
            $task->emailAfter($config['email_after']['to'], $config['email_after']['subject']);
86
        }
87 7
88 1
        if ($config['email_on_failure']['enabled']) {
89
            $task->emailOnFailure($config['email_on_failure']['to'], $config['email_on_failure']['subject']);
90
        }
91 7
92 7
        $schedule->add($task);
93
    }
94 7
95
    private function createTask(array $commands): Task
96 7
    {
97 5
        if (1 === \count($commands)) {
98
            return self::createSingleTask(\array_values($commands)[0]);
99
        }
100 2
101
        $task = new CompoundTask();
102 2
103 2
        foreach ($commands as $description => $command) {
104
            $subTask = self::createSingleTask($command);
105 2
106 1
            if (!\is_numeric($description)) {
107
                $subTask->description($description);
108
            }
109 2
110
            $task->add($subTask);
111
        }
112 2
113
        return $task;
114
    }
115 7
116
    private static function createSingleTask(string $command): Task
117 7
    {
118 3
        if (0 === \mb_strpos($command, self::PROCESS_TASK_PREFIX)) {
119
            return new ProcessTask(self::removePrefix($command, self::PROCESS_TASK_PREFIX));
120
        }
121 6
122 2
        if (0 === \mb_strpos($command, self::PING_TASK_PREFIX)) {
123
            return new PingTask(self::removePrefix($command, self::PING_TASK_PREFIX));
124
        }
125 5
126
        return new CommandTask($command);
127
    }
128 4
129
    private static function removePrefix(string $value, string $prefix): string
130 4
    {
131
        return \trim(\mb_substr($value, \mb_strlen($prefix)));
132
    }
133
}
134