Completed
Push — master ( 0b2176...a454e0 )
by Matthew
05:42
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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class CreateJobCommand extends ContainerAwareCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('dtc:queue:create_job')
17
            ->addArgument('worker_name', InputArgument::REQUIRED, 'Name of worker', null)
18
            ->addArgument('method', InputArgument::REQUIRED, 'Method of worker to invoke', null)
19
            ->addArgument('args', InputArgument::IS_ARRAY, 'Argument(s) for invoking worker method')
20
            ->setDescription('Create a job - for expert users');
21
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        $container = $this->getContainer();
26
        $jobManager = $container->get('dtc_queue.job_manager');
27
        $workerManager = $container->get('dtc_queue.worker_manager');
28
29
        $workerName = $input->getArgument('worker_name');
30
        $methodName = $input->getArgument('method');
31
        $args = $input->getArgument('args');
32
33
        $worker = $workerManager->getWorker($workerName);
34
35
        if (!$worker) {
36
            throw new \Exception("Worker `{$workerName}` is not registered.");
37
        }
38
39
        $when = new \DateTime();
40
        $batch = true;
41
        $priority = 1;
42
43
        $jobClass = $worker->getJobClass();
44
        $job = new $jobClass($worker, $batch, $priority, $when);
45
        $job->setMethod($methodName);
46
        $job->setArgs($args);
47
        $job->setLocked(null);
48
49
        $jobManager->save($job);
50
    }
51
}
52