Completed
Pull Request — master (#30)
by Matthew
16:57
created

CreateJobCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Dtc\QueueBundle\Command;
4
5
use Dtc\QueueBundle\Documents\Job;
6
use Dtc\QueueBundle\Exception\WorkerNotRegisteredException;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class CreateJobCommand extends ContainerAwareCommand
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('dtc:queue:create_job')
18
            ->addArgument('worker_name', InputArgument::REQUIRED, 'Name of worker', null)
19
            ->addArgument('method', InputArgument::REQUIRED, 'Method of worker to invoke', null)
20
            ->addArgument('args', InputArgument::IS_ARRAY, 'Argument(s) for invoking worker method')
21
            ->setDescription('Create a job - for expert users');
22
    }
23
24
    protected function execute(InputInterface $input, OutputInterface $output)
25
    {
26
        $container = $this->getContainer();
27
        $jobManager = $container->get('dtc_queue.job_manager');
28
        $workerManager = $container->get('dtc_queue.worker_manager');
29
30
        $workerName = $input->getArgument('worker_name');
31
        $methodName = $input->getArgument('method');
32
        $args = $input->getArgument('args');
33
34
        $worker = $workerManager->getWorker($workerName);
35
36
        if (!$worker) {
37
            throw new WorkerNotRegisteredException("Worker `{$workerName}` is not registered.");
38
        }
39
40
        $when = new \DateTime();
41
        $batch = true;
42
        $priority = 1;
43
44
        $jobClass = $worker->getJobManager()->getJobClass();
45
        $job = new $jobClass($worker, $batch, $priority, $when);
46
        $job->setMethod($methodName);
47
        $job->setArgs($args);
48
49
        $jobManager->save($job);
50
    }
51
}
52