1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tkotosz\CommandScheduler\Request\Processor; |
4
|
|
|
|
5
|
|
|
use Tkotosz\CommandScheduler\Api\CommandScheduleRepositoryInterface; |
6
|
|
|
use Tkotosz\CommandScheduler\Api\Data\CommandScheduleInterface; |
7
|
|
|
use Tkotosz\CommandScheduler\Model\CommandProvider; |
8
|
|
|
use Tkotosz\CommandScheduler\Request\CreateCommandScheduleRequest; |
9
|
|
|
|
10
|
|
|
class CreateCommandScheduleRequestProcessor |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var CommandScheduleRepositoryInterface |
14
|
|
|
*/ |
15
|
|
|
private $commandScheduleRepository; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var CommandProvider |
19
|
|
|
*/ |
20
|
|
|
private $commandProvider; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param CommandScheduleRepositoryInterface $commandScheduleRepository |
24
|
|
|
* @param CommandProvider $commandProvider |
25
|
|
|
*/ |
26
|
|
|
public function __construct( |
27
|
|
|
CommandScheduleRepositoryInterface $commandScheduleRepository, |
28
|
|
|
CommandProvider $commandProvider |
29
|
|
|
) { |
30
|
|
|
$this->commandScheduleRepository = $commandScheduleRepository; |
31
|
|
|
$this->commandProvider = $commandProvider; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function process(CreateCommandScheduleRequest $request): CommandScheduleInterface |
35
|
|
|
{ |
36
|
|
|
return $this->commandScheduleRepository->create($request->getCommandName(), $this->createCommandInput($request)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function createCommandInput(CreateCommandScheduleRequest $request): string |
40
|
|
|
{ |
41
|
|
|
$input = ''; |
42
|
|
|
|
43
|
|
|
$command = $this->commandProvider->getByName($request->getCommandName()); |
44
|
|
|
$commandParams = $request->getCommandParams(); |
45
|
|
|
|
46
|
|
|
// add options |
47
|
|
|
foreach ($command->getDefinition()->getOptions() as $option) { |
48
|
|
|
$paramName = sprintf('--%s', $option->getName()); |
49
|
|
|
if (isset($commandParams[$paramName])) { |
50
|
|
|
$input .= sprintf( |
51
|
|
|
' %s="%s"', |
52
|
|
|
$paramName, |
53
|
|
|
addcslashes($commandParams[$paramName], '"') |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// add arguments |
59
|
|
|
foreach ($command->getDefinition()->getArguments() as $argument) { |
60
|
|
|
$paramName = $argument->getName(); |
61
|
|
|
if (isset($commandParams[$paramName])) { |
62
|
|
|
$input .= sprintf( |
63
|
|
|
' "%s"', |
64
|
|
|
addcslashes($commandParams[$paramName], '"') |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return trim($input); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|