1
|
|
|
<?php |
2
|
|
|
namespace Samurai\Module\Planner; |
3
|
|
|
|
4
|
|
|
use Samurai\Task\ITask; |
5
|
|
|
use Samurai\Task\Planner; |
6
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PlannerAdapter |
13
|
|
|
* @package Samurai\Module\Planner |
14
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class PlannerAdapter implements ITask |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Planner |
20
|
|
|
*/ |
21
|
|
|
private $planner; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var IPlannerBuilder |
25
|
|
|
*/ |
26
|
|
|
private $plannerBuilder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var QuestionHelper |
30
|
|
|
*/ |
31
|
|
|
private $questionHelper; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param IPlannerBuilder $plannerBuilder |
35
|
|
|
* @param QuestionHelper $questionHelper |
36
|
|
|
*/ |
37
|
4 |
|
public function __construct(IPlannerBuilder $plannerBuilder, QuestionHelper $questionHelper) |
38
|
|
|
{ |
39
|
4 |
|
$this->plannerBuilder = $plannerBuilder; |
40
|
4 |
|
$this->questionHelper = $questionHelper; |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param InputInterface $input |
45
|
|
|
* @param OutputInterface $output |
46
|
|
|
* @return int|null |
47
|
|
|
*/ |
48
|
4 |
|
public function execute(InputInterface $input, OutputInterface $output) |
49
|
2 |
|
{ |
50
|
4 |
|
if(!$this->questionHelper->ask($input, $output, $this->buildQuestion())){ |
51
|
2 |
|
return ITask::NO_ERROR_CODE; |
52
|
|
|
} |
53
|
3 |
|
if(!$this->plannerBuilder->count()){ |
54
|
|
|
$output->writeln('<error>Nothing to run</error>'); |
55
|
|
|
return ITask::NON_BLOCKING_ERROR_CODE; |
56
|
|
|
} |
57
|
3 |
|
return $this->getPlanner()->execute($input, $output); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Getter of $planner |
62
|
|
|
* |
63
|
|
|
* @return Planner |
64
|
|
|
*/ |
65
|
3 |
|
private function getPlanner() |
66
|
|
|
{ |
67
|
3 |
|
if($this->planner == null){ |
68
|
3 |
|
$this->setPlanner($this->plannerBuilder->create()); |
69
|
3 |
|
} |
70
|
3 |
|
return $this->planner; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Setter of $planner |
75
|
|
|
* |
76
|
|
|
* @param Planner $planner |
77
|
|
|
*/ |
78
|
3 |
|
private function setPlanner(Planner $planner) |
79
|
|
|
{ |
80
|
3 |
|
$this->planner = $planner; |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return ConfirmationQuestion |
85
|
|
|
*/ |
86
|
4 |
|
private function buildQuestion() |
87
|
|
|
{ |
88
|
4 |
|
if($this->plannerBuilder->getName()){ |
89
|
2 |
|
return new ConfirmationQuestion('<question>Do you want to execute the module "' . $this->plannerBuilder->getName() . '"?[y]</question>'); |
|
|
|
|
90
|
|
|
} |
91
|
3 |
|
return new ConfirmationQuestion('<question>Do you want to execute the modules?[y]</question>'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.