Passed
Pull Request — master (#84)
by Joe
08:26 queued 02:42
created

CreateJobCommand::getHelpMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 23
ccs 11
cts 11
cp 1
crap 1
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\QueueBundle\Command;
4
5
use Dtc\QueueBundle\Exception\WorkerNotRegisteredException;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class CreateJobCommand extends ContainerAwareCommand
13
{
14
    protected static $defaultName = 'dtc:queue:create_job';
15
16 1
    protected function configure()
17
    {
18
        $this
19 1
            ->addOption(
20 1
                'json-args',
21 1
                'j',
22 1
                InputOption::VALUE_NONE,
23 1
                'Support json arguments (using the old arguments as strings is deprecated)'
24
            )
25 1
            ->addArgument(
26 1
                'worker_name',
27 1
                InputArgument::REQUIRED,
28 1
                'Name of worker',
29 1
                null
30
            )
31 1
            ->addArgument(
32 1
                'method',
33 1
                InputArgument::REQUIRED,
34 1
                'Method of worker to invoke',
35 1
                null
36
            )
37 1
            ->addArgument(
38 1
                'args',
39 1
                InputArgument::IS_ARRAY,
40 1
                'Json encoded argument(s) for invoking worker method'
41
            )
42 1
            ->setDescription('Create a job - for expert users')
43 1
            ->setHelp($this->getHelpMessage())
44
        ;
45 1
    }
46
47 1
    private function getHelpMessage()
48
    {
49 1
        $helpMessage = sprintf(
50 1
            "%s --json-args %s %s '%s'".PHP_EOL,
51 1
            $this->getName(), // command
52 1
            'my-worker-name', // worker_name
53 1
            'myMethod', // method
54 1
            json_encode([ // args
55 1
                "first parameter", // argv[0] (string)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
                null, // argv[1] (null)
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
                3, // argv[2] (int)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
                [ // argv[3] (array)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
                    "fourth",
60
                    "param",
61
                    "is",
62
                    "an",
63
                    "array",
64
                ]
65
            ])
66
        );
67 1
        $helpMessage .= PHP_EOL;
68 1
        $helpMessage .= "";
69 1
        return $helpMessage;
70
    }
71
72 1
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74 1
        $container = $this->getContainer();
75 1
        $jobManager = $container->get('dtc_queue.manager.job');
76 1
        $workerManager = $container->get('dtc_queue.manager.worker');
77
78 1
        $jsonArgs = $input->getOption('json-args');
79 1
        $workerName = $input->getArgument('worker_name');
80 1
        $methodName = $input->getArgument('method');
81 1
        $args = $input->getArgument('args');
82 1
        if ($jsonArgs) {
83 1
            if (1 !== count($args)) {
84
                throw new \InvalidArgumentException('args should be a single string when using --json-args');
85
            }
86 1
            $args = json_decode($args[0], true);
87
        } else {
88
            trigger_error(
89
                'Not Using --json-args is deprecated as of 4.7.2 and will become the default in 5.x',
90
                E_USER_DEPRECATED
91
            );
92
        }
93
94 1
        $worker = $workerManager->getWorker($workerName);
95
96 1
        if (!$worker) {
97 1
            throw new WorkerNotRegisteredException("Worker `{$workerName}` is not registered.");
98
        }
99
100 1
        $when = \Dtc\QueueBundle\Util\Util::getMicrotimeDateTime();
101 1
        $batch = true;
102 1
        $priority = 1;
103
104 1
        $jobClass = $worker->getJobManager()->getJobClass();
105 1
        $job = new $jobClass($worker, $batch, $priority, $when);
106 1
        $job->setMethod($methodName);
107 1
        $job->setArgs($args);
108
109 1
        $jobManager->save($job);
110 1
    }
111
}
112