Completed
Push — work-fleets ( ec9dc8...d7065d )
by SuperNova.WS
06:07
created

init_functions.php ➔ sn_sys_load_php_files()   C

Complexity

Conditions 11
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 17
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 28
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// ------------------------------------------------------------------------------------------------------------------------------
4
function sn_sys_load_php_files($dir_name, $load_extension = '.php', $modules = false) {
5
  if(!file_exists($dir_name) || !is_dir($dir_name)) {
6
    return;
7
  }
8
9
  $dir = opendir($dir_name);
10
  while (($file = readdir($dir)) !== false) {
11
    if ($file == '..' || $file == '.') {
12
      continue;
13
    }
14
15
    $full_filename = $dir_name . $file;
16
    if ($modules && is_dir($full_filename)) {
17
      if (file_exists($full_filename = "{$full_filename}/{$file}{$load_extension}")) {
18
        require_once($full_filename);
19
        // Registering module
20
        if (class_exists($file)) {
21
          new $file($full_filename);
22
        }
23
      }
24
    } else {
25
      $extension = substr($full_filename, -strlen($load_extension));
26
      if ($extension == $load_extension) {
27
        require_once($full_filename);
28
      }
29
    }
30
  }
31
}
32
33
function sys_refresh_tablelist() {
34
  classSupernova::$cache->tables = classSupernova::$db->db_get_table_list();
35
}
36
37
/**
38
 *
39
 */
40
function init_update() {
41
  $update_file = SN_ROOT_PHYSICAL . "includes/update" . DOT_PHP_EX;
42
  if(file_exists($update_file)) {
43
    if(filemtime($update_file) > classSupernova::$config->db_loadItem('var_db_update') || classSupernova::$config->db_loadItem('db_version') < DB_VERSION) {
44
      if(defined('IN_ADMIN')) {
45
        sn_db_transaction_start(); // Для защиты от двойного запуска апдейта - начинаем транзакцию. Так запись в базе будет блокирована
46
        if(SN_TIME_NOW >= classSupernova::$config->db_loadItem('var_db_update_end')) {
47
          classSupernova::$config->db_saveItem('var_db_update_end', SN_TIME_NOW + (classSupernova::$config->upd_lock_time ? classSupernova::$config->upd_lock_time : 300));
48
          sn_db_transaction_commit();
49
50
          require_once($update_file);
51
          sys_refresh_tablelist();
52
53
          $current_time = time();
54
          classSupernova::$config->db_saveItem('var_db_update', $current_time);
55
          classSupernova::$config->db_saveItem('var_db_update_end', $current_time);
56
        } elseif(filemtime($update_file) > classSupernova::$config->var_db_update) {
57
          $timeout = classSupernova::$config->var_db_update_end - SN_TIME_NOW;
58
          die(
59
            "Обновляется база данных. Рассчетное время окончания - {$timeout} секунд (время обновления может увеличиваться). Пожалуйста, подождите...<br />
60
            Obnovljaetsja baza dannyh. Rasschetnoe vremya okonchanija - {$timeout} secund. Pozhalujsta, podozhdute...<br />
61
            Database update in progress. Estimated update time {$timeout} seconds (can increase depending on update process). Please wait..."
62
          );
63
        }
64
        sn_db_transaction_rollback();
65
      } else {
66
        die(
67
          'Происходит обновление сервера - пожалуйста, подождите...<br />
68
          Proishodit obnovlenie servera - pozhalujsta, podozhdute...<br />
69
          Server upgrading now - please wait...<br />
70
          <a href="admin/overview.php">Admin link</a>'
71
        );
72
      }
73
    }
74
  }
75
}
76