Issues (1369)

classes/Core/Scheduler/Watchdog.php (2 issues)

1
<?php
2
/**
3
 * Created by Gorlum 15.06.2017 5:07
4
 */
5
6
namespace Core\Scheduler;
7
8
use Exception;
9
use classConfig;
10
use Core\GlobalContainer;
11
12
class Watchdog {
13
  /**
14
   * @var GlobalContainer $gc
15
   */
16
  protected $gc;
17
  /**
18
   * @var \classConfig $config
19
   */
20
  protected $config;
21
22
  /**
23
   * @var TaskConditional[] $taskList
24
   */
25
  protected $taskList = [];
26
27
  /**
28
   * Watchdog constructor.
29
   *
30
   * @param GlobalContainer $gc
31
   */
32
  public function __construct(GlobalContainer $gc) {
33
    $this->gc     = $gc;
34
    $this->config = $this->gc->config;
35
  }
36
37
  /**
38
   * @param TaskConditional $task
39
   * @param string          $name Optional. Task name
40
   */
41
  public function register(TaskConditional $task, $name = '') {
42
    if (empty($name)) {
43
      $this->taskList[] = $task;
44
    } else {
45
      $this->taskList[$name] = $task;
46
    }
47
  }
48
49
  /**
50
   * @param $name
51
   *
52
   * @return TaskConditional|null
53
   */
54
  public function getTask($name) {
55
    return !empty($this->taskList[$name]) ? $this->taskList[$name] : null;
56
  }
57
58
  public function execute() {
59
    foreach ($this->taskList as $task) {
60
      try {
61
        $task();
62
      } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
63
      }
64
    }
65
  }
66
67
  /**
68
   * @param string   $configName - config record name
69
   * @param int      $timeDiff   - interval from SN_TIME_NOW in seconds
70
   * @param callable $callable   - function to call when condition is met
71
   * @param int      $configType - type of config record - unixtime or Sql timestamp
72
   * @param bool     $forceLoad  - should config value be read from DB
73
   *
74
   * @deprecated
75
   */
76
  public function checkConfigTimeDiff($configName, $timeDiff, $callable, $configType = classConfig::DATE_TYPE_UNIX, $forceLoad = false) {
77
    $configValue = $forceLoad ? $this->config->db_loadItem($configName) : $this->config[$configName];
78
    $configType == classConfig::DATE_TYPE_SQL_STRING ? $configValue = strtotime($configValue, SN_TIME_NOW) : false;
0 ignored issues
show
It seems like $configValue can also be of type null; however, parameter $datetime of strtotime() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
    $configType == classConfig::DATE_TYPE_SQL_STRING ? $configValue = strtotime(/** @scrutinizer ignore-type */ $configValue, SN_TIME_NOW) : false;
Loading history...
79
80
    if (SN_TIME_NOW - $configValue > $timeDiff) {
81
      $callable();
82
    }
83
84
  }
85
86
}
87