CreateCommandScheduleRequestProcessor::process()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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