Completed
Push — master ( a1f77e...f1bc3f )
by Matthew
04:51
created

CreateJobCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 41
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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