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

CreateJobCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 40
ccs 0
cts 30
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 27 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