Passed
Pull Request — master (#408)
by
unknown
03:22
created

CronTask   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 3 1
A getPreviousRunDate() 0 3 1
A getNextRunDate() 0 3 1
A getExpression() 0 3 1
A getName() 0 3 1
A shouldRun() 0 3 1
A __construct() 0 9 2
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 3.0.0
13
 */
14
15
namespace Quantum\Libraries\Cron;
16
17
use Quantum\Libraries\Cron\Contracts\CronTaskInterface;
18
use Quantum\Libraries\Cron\Exceptions\CronException;
19
use Cron\CronExpression;
20
21
/**
22
 * Class CronTask
23
 * @package Quantum\Libraries\Cron
24
 */
25
class CronTask implements CronTaskInterface
26
{
27
    /**
28
     * Cron expression instance
29
     * @var CronExpression
30
     */
31
    private $cronExpression;
32
33
    /**
34
     * Task name
35
     * @var string
36
     */
37
    private $name;
38
39
    /**
40
     * Task callback
41
     * @var callable
42
     */
43
    private $callback;
44
45
    /**
46
     * CronTask constructor
47
     * @param string $name
48
     * @param string $expression
49
     * @param callable $callback
50
     * @throws CronException
51
     */
52
    public function __construct(string $name, string $expression, callable $callback)
53
    {
54
        $this->name = $name;
55
        $this->callback = $callback;
56
57
        try {
58
            $this->cronExpression = new CronExpression($expression);
59
        } catch (\Exception $e) {
60
            throw CronException::invalidExpression($expression);
61
        }
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function getExpression(): string
68
    {
69
        return $this->cronExpression->getExpression();
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function getName(): string
76
    {
77
        return $this->name;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function shouldRun(): bool
84
    {
85
        return $this->cronExpression->isDue();
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function handle(): void
92
    {
93
        call_user_func($this->callback);
94
    }
95
96
    /**
97
     * Get the next run date
98
     * @return \DateTime
99
     */
100
    public function getNextRunDate(): \DateTime
101
    {
102
        return $this->cronExpression->getNextRunDate();
103
    }
104
105
    /**
106
     * Get the previous run date
107
     * @return \DateTime
108
     */
109
    public function getPreviousRunDate(): \DateTime
110
    {
111
        return $this->cronExpression->getPreviousRunDate();
112
    }
113
}
114