Skip to content

Commit 372403f

Browse files
committed
Список задач к выполнению формируется заранее
1 parent 06ab52d commit 372403f

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

src/Crontask/TaskList.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Crontask;
34

45
use Crontask\Interfaces\TaskInterface;
@@ -33,29 +34,51 @@ public function addTasks($tasks)
3334
* @param TaskInterface $task
3435
* @return TaskList $this
3536
*/
36-
public function addTask(TaskInterface $task)
37+
public function addTask(TaskInterface $task): TaskList
3738
{
3839
$this->tasks[] = $task;
3940

4041
return $this;
4142
}
4243

44+
/**
45+
* Set tasks
46+
*
47+
* @param array $tasks
48+
*/
49+
public function setTasks($tasks)
50+
{
51+
$this->tasks = $tasks;
52+
}
53+
4354
/**
4455
* Get Tasks
4556
*
4657
* @return array
4758
*/
48-
public function getTasks()
59+
public function getTasks(): array
4960
{
5061
return $this->tasks;
5162
}
5263

64+
/**
65+
* Get required tasks
66+
*
67+
* @return array
68+
*/
69+
public function getTasksRequired(): array
70+
{
71+
return array_filter($this->tasks, function (TaskInterface $task) {
72+
return $task->isRequired();
73+
});
74+
}
75+
5376
/**
5477
* Get Output
5578
*
5679
* @return array
5780
*/
58-
public function getOutput()
81+
public function getOutput(): array
5982
{
6083
return $this->output;
6184
}
@@ -65,21 +88,17 @@ public function getOutput()
6588
*
6689
* @return array
6790
*/
68-
public function run()
91+
public function run(): array
6992
{
7093
$this->output = [];
7194

72-
foreach ($this->tasks as $task) {
73-
if ($task->isRequired()) {
74-
75-
$result = $task->run();
76-
77-
$this->output[] = [
78-
'task' => get_class($task),
79-
'output' => $task->getOutput(),
80-
'result' => $result,
81-
];
82-
}
95+
foreach ($this->getTasksRequired() as $task) {
96+
$result = $task->run();
97+
$this->output[] = [
98+
'task' => get_class($task),
99+
'output' => $task->getOutput(),
100+
'result' => $result,
101+
];
83102
}
84103

85104
return $this->output;

src/Crontask/Tasks/Shell.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Shell extends Task
2020
public function run()
2121
{
2222
$output = null;
23-
exec($this->getCommand().' '.implode(' ', $this->arguments), $output, $result);
23+
exec($this->getCommand() . ' ' . implode(' ', $this->arguments), $output, $result);
2424

2525
$this->setOutput($output);
2626

@@ -31,7 +31,7 @@ public function run()
3131
* @param string $command
3232
* @return $this
3333
*/
34-
public function setCommand($command)
34+
public function setCommand($command): self
3535
{
3636
$this->command = $command;
3737

@@ -41,7 +41,7 @@ public function setCommand($command)
4141
/**
4242
* @return string
4343
*/
44-
public function getCommand()
44+
public function getCommand(): string
4545
{
4646
return $this->command;
4747
}
@@ -50,7 +50,7 @@ public function getCommand()
5050
* @param mixed $argument
5151
* @return $this
5252
*/
53-
public function addArgument($argument)
53+
public function addArgument($argument): self
5454
{
5555
$this->arguments[] = $argument;
5656

@@ -60,7 +60,7 @@ public function addArgument($argument)
6060
/**
6161
* @return array
6262
*/
63-
public function getArguments()
63+
public function getArguments(): array
6464
{
6565
return $this->arguments;
6666
}

src/Crontask/Tasks/Task.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Crontask\Tasks;
34

45
use Cron\CronExpression;
@@ -23,10 +24,11 @@ abstract public function run();
2324

2425
/**
2526
* Sets a cron expression
27+
*
2628
* @param string $expression
2729
* @return Task $this
2830
*/
29-
public function setExpression($expression)
31+
public function setExpression($expression): Task
3032
{
3133
$this->expression = $expression;
3234

@@ -35,19 +37,21 @@ public function setExpression($expression)
3537

3638
/**
3739
* Gets the current cron expression
40+
*
3841
* @return string
3942
*/
40-
public function getExpression()
43+
public function getExpression(): string
4144
{
4245
return $this->expression;
4346
}
4447

4548
/**
4649
* Sets the output from the task
50+
*
4751
* @param null|string|array $output
4852
* @return Task $this
4953
*/
50-
public function setOutput($output)
54+
public function setOutput($output): Task
5155
{
5256
$this->output = $output;
5357

@@ -56,6 +60,7 @@ public function setOutput($output)
5660

5761
/**
5862
* Gets the output from the task
63+
*
5964
* @return null|string|array
6065
*/
6166
public function getOutput()
@@ -67,7 +72,7 @@ public function getOutput()
6772
* Checks whether the task is currently due
6873
* @return bool
6974
*/
70-
public function isRequired()
75+
public function isRequired(): bool
7176
{
7277
$expression = $this->getExpression();
7378

0 commit comments

Comments
 (0)