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

IdleSleepStrategy::onIdle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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\Strategy;
12
13
use Core\Queue\MongoQueue;
14
use SlmQueue\Strategy\AbstractStrategy;
15
use SlmQueue\Worker\Event\AbstractWorkerEvent;
16
use SlmQueue\Worker\Event\ProcessIdleEvent;
17
use Zend\EventManager\EventManagerInterface;
18
19
/**
20
 * ${CARET}
21
 * 
22
 * @author Mathias Gelhausen <[email protected]>
23
 * @todo write test 
24
 */
25
class IdleSleepStrategy extends AbstractStrategy
26
{
27
28
    public $duration = 1;
29
30 1
    public function attach(EventManagerInterface $events, $priority = 1)
31
    {
32 1
        $this->listeners[] = $events->attach(
33 1
            AbstractWorkerEvent::EVENT_PROCESS_IDLE,
34 1
            [$this, 'onIdle'],
35 1
            1
36
        );
37 1
    }
38
39
    /**
40
     * @param mixed $duration
41
     *
42
     * @return self
43
     */
44 1
    public function setDuration($duration)
45
    {
46 1
        $this->duration = $duration;
47
48 1
        return $this;
49
    }
50
51
    
52 2
    public function onIdle(ProcessIdleEvent $event)
53
    {
54 2
        $queue = $event->getQueue();
55
56 2
        if ($queue instanceof MongoQueue) {
57 1
            sleep($this->duration);
58
        }
59 2
    }
60
}
61