Passed
Push — master ( 18af30...0e6a78 )
by Mathias
45:31 queued 28:39
created

MongoWorker::processJob()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.1158

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 22
ccs 10
cts 12
cp 0.8333
rs 9.5222
c 0
b 0
f 0
cc 5
nc 7
nop 2
crap 5.1158
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2019 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Queue\Worker;
12
13
use Core\Queue\Exception\FatalJobException;
14
use Core\Queue\Exception\RecoverableJobException;
15
use Core\Queue\Job\ExceptionJobResult;
16
use Core\Queue\Job\JobResult;
17
use Core\Queue\Job\ResultProviderInterface;
18
use Core\Queue\LoggerAwareJobTrait;
19
use Core\Queue\MongoQueue;
20
use SlmQueue\Job\JobInterface;
21
use SlmQueue\Queue\QueueAwareInterface;
22
use SlmQueue\Queue\QueueInterface;
23
use SlmQueue\Worker\AbstractWorker;
24
use SlmQueue\Worker\Event\ProcessJobEvent;
25
use Zend\Log\LoggerAwareInterface;
26
27
/**
28
 * Queue worker for the mongo queue.
29
 *
30
 * @author Mathias Gelhausen <[email protected]>
31
 */
32
class MongoWorker extends AbstractWorker implements LoggerAwareInterface
33
{
34
    use LoggerAwareJobTrait;
35
36
    /**
37
     * Process job handler.
38
     *
39
     * @param JobInterface   $job
40
     * @param QueueInterface $queue
41
     *
42
     * @return int|void
43
     */
44 4
    public function processJob(JobInterface $job, QueueInterface $queue)
45
    {
46 4
        if (!$queue instanceof MongoQueue) {
47 1
            return;
48
        }
49
50 3
        if ($job instanceOf QueueAwareInterface) {
51
            $job->setQueue($queue);
52
        }
53
54
        try {
55 3
            return $job->execute();
56 2
        } catch (\Exception $exception) {
57 2
            $this->getLogger()->err('Job execution thrown exception: ' . get_class($exception));
58
59 2
            if ($job instanceOf ResultProviderInterface) {
60
                $job->setResult(JobResult::failure($exception->getMessage(), [$exception->getTraceAsString()]));
61
            } else {
62 2
                $this->getLogger()->err($exception->getMessage(), [$exception->getTraceAsString()]);
63
            }
64
65 2
            return ProcessJobEvent::JOB_STATUS_FAILURE;
66
        }
67
    }
68
}
69