Passed
Push — master ( 751812...ad3fb0 )
by Kevin
02:24
created

TaskConfigurationSubscriber::addTask()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 2.8
c 0
b 0
f 0
eloc 25
ccs 26
cts 26
cp 1
cc 12
nc 2048
nop 2
crap 12

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ProcessTask;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class TaskConfigurationSubscriber 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 [BuildScheduleEvent::class => 'configureTasks'];
30
    }
31
32 5
    public function configureTasks(BuildScheduleEvent $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['timezone']) {
50 1
            $task->timezone($config['timezone']);
51
        }
52
53 5
        if ($config['without_overlapping']['enabled']) {
54 3
            $task->withoutOverlapping($config['without_overlapping']['ttl']);
55
        }
56
57 5
        if ($config['between']['enabled']) {
58 1
            $task->between($config['between']['start'], $config['between']['end']);
59
        }
60
61 5
        if ($config['unless_between']['enabled']) {
62 1
            $task->unlessBetween($config['unless_between']['start'], $config['unless_between']['end']);
63
        }
64
65 5
        if ($config['ping_before']['enabled']) {
66 1
            $task->pingBefore($config['ping_before']['url'], $config['ping_before']['method'], $config['ping_before']['options']);
67
        }
68
69 5
        if ($config['ping_after']['enabled']) {
70 1
            $task->pingAfter($config['ping_after']['url'], $config['ping_after']['method'], $config['ping_after']['options']);
71
        }
72
73 5
        if ($config['ping_on_success']['enabled']) {
74 1
            $task->pingOnSuccess($config['ping_on_success']['url'], $config['ping_on_success']['method'], $config['ping_on_success']['options']);
75
        }
76
77 5
        if ($config['ping_on_failure']['enabled']) {
78 1
            $task->pingOnFailure($config['ping_on_failure']['url'], $config['ping_on_failure']['method'], $config['ping_on_failure']['options']);
79
        }
80
81 5
        if ($config['email_after']['enabled']) {
82 1
            $task->emailAfter($config['email_after']['to'], $config['email_after']['subject']);
83
        }
84
85 5
        if ($config['email_on_failure']['enabled']) {
86 1
            $task->emailOnFailure($config['email_on_failure']['to'], $config['email_on_failure']['subject']);
87
        }
88
89 5
        $schedule->add($task);
90 5
    }
91
92 5
    private function createTask(array $commands): Task
93
    {
94 5
        if (1 === \count($commands)) {
95 3
            return $this->createSingleTask(\array_values($commands)[0]);
96
        }
97
98 2
        $task = new CompoundTask();
99
100 2
        foreach ($commands as $description => $command) {
101 2
            $subTask = $this->createSingleTask($command);
102
103 2
            if (!\is_numeric($description)) {
104 1
                $subTask->description($description);
105
            }
106
107 2
            $task->add($subTask);
108
        }
109
110 2
        return $task;
111
    }
112
113 5
    private function createSingleTask(string $command): Task
114
    {
115 5
        if (0 !== \mb_strpos($command, self::PROCESS_TASK_PREFIX)) {
116 4
            return new CommandTask($command);
117
        }
118
119 3
        return new ProcessTask(\trim(\mb_substr($command, \mb_strlen(self::PROCESS_TASK_PREFIX))));
120
    }
121
}
122