AsyncJobDispatcher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
namespace Workana\AsyncJobs\Dispatcher;
3
4
use DateTime;
5
use Bernard\Envelope;
6
use Workana\AsyncJobs\Job;
7
use Workana\AsyncJobs\JobManager;
8
use Workana\AsyncJobs\JobDispatcher;
9
10
/**
11
 * Job dispatcher on async mode
12
 *
13
 * @author Carlos Frutos <[email protected]>
14
 */
15
class AsyncJobDispatcher implements JobDispatcher
16
{
17
    /**
18
     * @var JobManager
19
     */
20
    protected $jm;
21
22
    /**
23
     * @var QueueDecider
24
     */
25
    protected $queueDecider;
26
27
    /**
28
     * Creates a new instance
29
     *
30
     * @param JobManager $jm
31
     */
32
    public function __construct(JobManager $jm)
33
    {
34
        $this->jm = $jm;
35
        $this->queueDecider = new QueueDecider(
36
            $jm->getQueueFactory(),
37
            $jm->getConfiguration()->getDefaultQueueName(),
38
            $jm->getConfiguration()->getDispatchingRules()
39
        );
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function dispatch(Job $job)
46
    {
47
        $queue = $this->queueDecider->decide($job);
48
        
49
        $envelope = new Envelope($job, $job->getDelay());
50
51
        $queue->enqueue($envelope);
52
    }
53
}