Completed
Branch hotfix/5.4.1 (9732a4)
by Schlaefer
02:32
created

CronJob::getDue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Cron\Lib;
14
15
use Cake\Core\Configure;
16
use Cake\Log\Log;
17
use Stopwatch\Lib\Stopwatch;
18
19
class CronJob
20
{
21
22
    /**
23
     * @var string
24
     */
25
    private $uid;
26
27
    /**
28
     * @var int
29
     */
30
    private $due;
31
32
    /**
33
     * @var callable
34
     */
35
    private $func;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param string $uid unique ID for cron job
41
     * @param string $due due intervall
42
     * @param callable $func cron job
43
     */
44
    public function __construct(string $uid, string $due, callable $func)
45
    {
46
        $this->uid = $uid;
47
        $this->due = strtotime($due);
48
        if (!$this->due) {
49
            throw new \InvalidArgumentException(
50
                sprintf('Cannot convert "%s" into a timestamp.', $due),
51
                1571567221
52
            );
53
        }
54
        $this->func = $func;
55
    }
56
57
    /**
58
     * Get the value of uid
59
     *
60
     * @return  string
61
     */
62
    public function getUid(): string
63
    {
64
        return $this->uid;
65
    }
66
67
    /**
68
     * When should the job be next executed
69
     *
70
     * @return int UNIX-timestamp
71
     */
72
    public function getDue(): int
73
    {
74
        return $this->due;
75
    }
76
77
    /**
78
     * Execute cron job
79
     *
80
     * @return void
81
     */
82
    public function execute(): void
83
    {
84
        $msg = 'Cron.CronJob::execute ' . $this->getUid();
85
        if (Configure::read('Saito.debug.logInfo')) {
86
            Log::write('info', $msg, ['scope' => ['saito.info']]);
87
        }
88
89
        Stopwatch::start($msg);
90
        call_user_func($this->func);
91
        Stopwatch::stop($msg);
92
    }
93
}
94