Completed
Push — master ( 1d826a...bce6a5 )
by Alexey
05:15
created

Init::process()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 28
nc 13
nop 0
dl 0
loc 38
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Mode Init
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Exchange1c\Mode;
13
14
class Init extends \Exchange1c\Mode {
15
16
  public function process() {
17
    echo "zip=no\n";
18
    echo 'file_limit=' . \Tools::toBytes(ini_get('post_max_size'));
19
    $this->end();
20
21
    //clean files
22
    if (!empty(\App::$cur->exchange1c->config['maxSaveFilesInterval'])) {
23
      $query = \App::$cur->db->newQuery();
24
      $query->operation = 'select';
25
      $query->table = \Exchange1c\Exchange\File::table();
26
      $query->cols = \Exchange1c\Exchange\File::colPrefix() . 'id';
27
      $queryArr = $query->buildQuery();
28
      $queryArr['query'].=' where `' . \Exchange1c\Exchange\File::colPrefix() . 'deleted` = 0 AND  `' . \Exchange1c\Exchange\File::colPrefix() . 'date_create` < NOW() - INTERVAL ' . \App::$cur->exchange1c->config['maxSaveFilesInterval'];
29
      try {
30
        $ids = array_keys($query->query($queryArr)->getArray(\Exchange1c\Exchange\File::colPrefix().'id'));
31
      } catch (\PDOException $exc) {
32
        if ($exc->getCode() == '42S02') {
33
          \Exchange1c\Exchange\File::createTable();
34
        } elseif ($exc->getCode() == '42S22') {
35
          $cols = \Exchange1c\Exchange\File::cols();
36
          foreach (\Exchange1c\Exchange\File::$cols as $colName => $params) {
37
            if (!isset($cols[\Exchange1c\Exchange\File::colPrefix() . $colName])) {
38
              \Exchange1c\Exchange\File::createCol($colName);
39
            }
40
          }
41
        }
42
        $ids = array_keys($query->query($queryArr)->getArray(\Exchange1c\Exchange\File::colPrefix().'id'));
43
      }
44
      foreach (array_chunk($ids, 500) as $idGroup) {
45
        $dfiles = \Exchange1c\Exchange\File::getList(['where' => ['id', $idGroup, 'IN']]);
46
        foreach ($dfiles as $dfile) {
47
          $dfile->deleteFile();
48
          unset($dfile);
49
        }
50
        unset($dfiles);
51
      }
52
    }
53
  }
54
55
}
56