Completed
Push — master ( 807794...9b102f )
by Mihail
02:58
created

CronManager::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Ffcms\Core\Managers;
4
5
6
use Ffcms\Core\App;
7
use Ffcms\Core\Helper\Type\Obj;
8
9
10
/**
11
 * Class CronManager. Class to control cron manager on website. Register and run tasks over time.
12
 * @package Ffcms\Core\Managers
13
 */
14
class CronManager
15
{
16
    /** @var array $configs */
17
    private $configs = [];
18
19
    /**
20
     * CronManager constructor. Initialize configurations.
21
     */
22
    public function __construct()
23
    {
24
        /** @var array $configs */
25
        $configs = App::$Properties->getAll('Cron');
26
        if (Obj::isArray($configs)) {
27
            $this->configs = $configs;
28
        }
29
    }
30
31
    /**
32
     * Run cron task. Attention - this method is too 'fat' to run from any app's
33
     * @return array|null
34
     */
35
    public function run()
36
    {
37
        // check if cron instances is defined
38
        if (!isset($this->configs['instances']) || !Obj::isArray($this->configs['instances'])) {
39
            return null;
40
        }
41
        // get timestamp
42
        $time = time();
43
        $log = [];
44
45
        // each every one instance
46
        foreach ($this->configs['instances'] as $callback => $delay) {
47
            if (((int)$this->configs['log'][$callback] + $delay) <= $time) {
48
                // prepare cron initializing
49
                list($class, $method) = explode('::', $callback);
50
                if (class_exists($class) && method_exists($class, $method)) {
51
                    // make static callback
52
                    forward_static_call([$class, $method]);
53
                    $log[] = $callback;
54
                }
55
                // update log information
56
                $this->configs['log'][$callback] = $time + $delay;
57
            }
58
        }
59
60
        // write updated configs
61
        App::$Properties->writeConfig('Cron', $this->configs);
62
        return $log;
63
    }
64
65
    /**
66
     * Register cron action callback to $class::$method() every $delay seconds
67
     * @param string $class
68
     * @param string $method
69
     * @param int $delay
70
     * @return bool
71
     */
72
    public function register($class, $method, $delay = 60)
73
    {
74
        // check if declared callback is exist over autoload
75
        if (!class_exists($class) || !method_exists($class, $method)) {
76
            return false;
77
        }
78
79
        $callback = (string)$class . '::' . (string)$method;
80
81
        // add instance to cron task manager
82
        if (!isset($this->configs['instances'][$callback])) {
83
            $this->configs['instances'][$callback] = $delay;
84
            App::$Properties->writeConfig('Cron', $this->configs);
85
        }
86
87
        return true;
88
    }
89
90
    /**
91
     * Remove registered cron task from configs
92
     * @param string $class
93
     * @param string $method
94
     */
95
    public function remove($class, $method)
96
    {
97
        $callback = $class . '::' . $method;
98
        if (isset($this->configs['instances'][$callback])) {
99
            unset($this->configs['instances'][$callback], $this->configs['log'][$callback]);
100
            App::$Properties->writeConfig('Cron', $this->configs);
101
        }
102
    }
103
}