Completed
Push — master ( 0b2176...a454e0 )
by Matthew
05:42
created

CreateJobCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 41
ccs 0
cts 31
cp 0
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
    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