Completed
Push — master ( 5eaeb0...7eb939 )
by Wachter
11:51 queued 02:03
created

CronTask   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 5
c 1
b 1
f 1
lcom 1
cbo 4
dl 0
loc 43
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A scheduleNext() 0 14 3
A getNextRunDateTime() 0 4 1
1
<?php
2
/*
3
 * This file is part of PHP-Task library.
4
 *
5
 * (c) php-task
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Task\FrequentTask;
12
13
use Cron\CronExpression;
14
use Task\SchedulerInterface;
15
use Task\TaskInterface;
16
17
/**
18
 * Uses cron-expression to schedule frequent tasks.
19
 *
20
 * @author @wachterjohannes <[email protected]>
21
 */
22
class CronTask extends FrequentTask
23
{
24
    /**
25
     * @var CronExpression
26
     */
27
    private $expression;
28
29 35
    public function __construct(CronExpression $expression,
30
        TaskInterface $task,
31
        \DateTime $start,
32
        \DateTime $end = null
33
    ) {
34 35
        parent::__construct($task, $start, $end);
35
36 35
        $this->expression = $expression;
37 35
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 30
    public function scheduleNext(SchedulerInterface $scheduler)
43
    {
44 30
        $executionDate = $this->getNextRunDateTime();
45
46 30
        if (null !== $this->end && $executionDate > $this->end) {
47 6
            return;
48
        }
49
50 24
        $scheduler->createTask($this->getTaskName(), $this->getWorkload())
51 24
            ->cron($this->expression->getExpression(), $this->start, $this->end)
52 24
            ->setKey($this->getKey())
53 24
            ->setExecutionDate($executionDate)
54 24
            ->schedule();
55 24
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 31
    public function getNextRunDateTime()
61
    {
62 31
        return $this->expression->getNextRunDate();
63
    }
64
}
65