CronManager   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 6 2
B run() 0 29 7
A register() 0 16 4
A __construct() 0 6 2
1
<?php
2
3
namespace Ffcms\Core\Managers;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Any;
7
use Ffcms\Core\Helper\Type\Obj;
8
9
/**
10
 * Class CronManager. Class to control cron manager on website. Register and run tasks over time.
11
 * @package Ffcms\Core\Managers
12
 */
13
class CronManager
14
{
15
    /** @var array $configs */
16
    private $configs = [];
17
18
    /**
19
     * CronManager constructor. Initialize configurations.
20
     */
21
    public function __construct()
22
    {
23
        /** @var array $configs */
24
        $configs = App::$Properties->getAll('Cron');
25
        if (Any::isArray($configs)) {
26
            $this->configs = $configs;
27
        }
28
    }
29
30
    /**
31
     * Run cron task. Attention - this method is too 'fat' to run from any app's
32
     * @return array|null
33
     */
34
    public function run()
35
    {
36
        // check if cron instances is defined
37
        if (!isset($this->configs['instances']) || !Any::isArray($this->configs['instances'])) {
38
            return null;
39
        }
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
}
104