Completed
Push — work-fleets ( 04acf9...8f8df9 )
by SuperNova.WS
07:02
created

RequestInfo::__construct()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 52
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 38
c 2
b 0
f 0
nc 18
nop 0
dl 0
loc 52
rs 7.2396
ccs 0
cts 45
cp 0
crap 56

How to fix   Long Method   

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
 * Created by Gorlum 29.08.2015 16:49
5
 */
6
7
/**
8
 * Подробности о запросе
9
 */
10
class RequestInfo {
11
  /**
12
   * Идентификационная строка устройства
13
   *
14
   * @var string
15
   */
16
  protected $device_cypher = '';
17
  /**
18
   * Идентификатор устройства
19
   *
20
   * @var string
21
   */
22
  public $device_id = 0;
23
24
  /**
25
   * Строка User-agent пользовательского браузера
26
   *
27
   * @var string
28
   */
29
  protected $user_agent = '';
30
  /**
31
   * Внутренний идентификатор строки браузера
32
   *
33
   * @var int
34
   */
35
  public $browser_id = 0;
36
37
  /**
38
   * Полный URL строки запроса
39
   *
40
   * @var string
41
   */
42
  protected $page_address = '';
43
  /**
44
   * ID запроса в таблице УРЛов
45
   *
46
   * @var int
47
   */
48
  public $page_address_id = 0;
49
50
  /**
51
   * Короткий УРЛ - без параметров
52
   *
53
   * @var string
54
   */
55
  protected $page_url = '';
56
  /**
57
   * ID короткого УРЛа в таблице УРЛов
58
   *
59
   * @var int
60
   */
61
  public $page_url_id = 0;
62
63
  /**
64
   * Адрес IPv4 в виде строки
65
   *
66
   * @var string
67
   */
68
  public $ip_v4_string = '';
69
  /**
70
   * Адрес IPv4 в виде целого
71
   *
72
   * @var int
73
   */
74
  public $ip_v4_int = 0;
75
  /**
76
   * Цепочка прокси IPv4
77
   *
78
   * @var string
79
   */
80
  public $ip_v4_proxy_chain = '';
81
82
  protected $write_full_url = false;
83
84
  public function __construct() {
85
    $this->write_full_url = !classSupernova::$config->security_write_full_url_disabled;
86
87
    // Инфа об устройстве и браузере - общая для всех
88
    sn_db_transaction_start();
89
    $this->device_cypher = $_COOKIE[SN_COOKIE_D];
90
    if($this->device_cypher) {
91
      $cypher_safe = db_escape($this->device_cypher);
92
      $device_id = classSupernova::$db->doSelectFetch("SELECT `device_id` FROM {{security_device}} WHERE `device_cypher` = '{$cypher_safe}' LIMIT 1 FOR UPDATE");
93
      if(!empty($device_id['device_id'])) {
94
        $this->device_id = $device_id['device_id'];
95
      }
96
    }
97
98
    if($this->device_id <= 0) {
99
      do {
100
        $cypher_safe = db_escape($this->device_cypher = sys_random_string());
101
        $row = classSupernova::$db->doSelectFetch("SELECT `device_id` FROM {{security_device}} WHERE `device_cypher` = '{$cypher_safe}' LIMIT 1 FOR UPDATE");
102
      } while (!empty($row));
103
      classSupernova::$db->doInsertSet(TABLE_SECURITY_DEVICE, array(
104
        'device_cypher' => $this->device_cypher,
105
      ));
106
      $this->device_id = classSupernova::$db->db_insert_id();
0 ignored issues
show
Documentation Bug introduced by
The property $device_id was declared of type string, but \classSupernova::$db->db_insert_id() is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
107
      sn_setcookie(SN_COOKIE_D, $this->device_cypher, PERIOD_FOREVER, SN_ROOT_RELATIVE);
108
    }
109
    sn_db_transaction_commit();
110
111
    sn_db_transaction_start();
112
    $this->user_agent = $_SERVER['HTTP_USER_AGENT'];
113
    $this->browser_id = db_get_set_unique_id_value($_SERVER['HTTP_USER_AGENT'], 'browser_id', 'security_browser', 'browser_user_agent');
114
    sn_db_transaction_commit();
115
116
    sn_db_transaction_start();
117
    $this->page_address = substr($_SERVER['PHP_SELF'], strlen(SN_ROOT_RELATIVE));
118
    $this->page_address_id = db_get_set_unique_id_value($this->page_address, 'url_id', 'security_url', 'url_string');
119
    sn_db_transaction_commit();
120
121
    if($this->write_full_url) {
122
      sn_db_transaction_start();
123
      $this->page_url = substr($_SERVER['REQUEST_URI'], strlen(SN_ROOT_RELATIVE));
124
      if(strpos($_SERVER['REQUEST_URI'], '/simulator.php') === 0) {
125
        $this->page_url = '/simulator.php';
126
      }
127
      $this->page_url_id = db_get_set_unique_id_value($this->page_url, 'url_id', 'security_url', 'url_string');
128
      sn_db_transaction_commit();
129
    }
130
131
    $ip = sec_player_ip();
132
    $this->ip_v4_string = $ip['ip'];
133
    $this->ip_v4_int = ip2longu($this->ip_v4_string);
0 ignored issues
show
Documentation Bug introduced by
The property $ip_v4_int was declared of type integer, but ip2longu($this->ip_v4_string) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
134
    $this->ip_v4_proxy_chain = $ip['proxy_chain'];
135
  }
136
137
  /**
138
   * Вставляет запись системы безопасности
139
   *
140
   * @param $user_id_unsafe
141
   *
142
   * @return array|bool|mysqli_result|null
143
   *
144
   */
145
  public function db_security_entry_insert($user_id_unsafe) {
146
    // TODO $user_id = !empty(self::$user['id']) ? self::$user['id'] : 'NULL';
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
147
    if(empty($user_id_unsafe)) {
148
      // self::flog('Нет ИД пользователя');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
149
      return true;
150
    }
151
152
    return classSupernova::$db->doInsertSet(TABLE_SECURITY_PLAYER_ENTRY, array(
153
      'player_id'  => empty($user_id_unsafe) ? null : $user_id_unsafe,
154
      'device_id'  => $this->device_id,
155
      'browser_id' => $this->browser_id,
156
      'user_ip'    => $this->ip_v4_int,
157
      'user_proxy' => $this->ip_v4_proxy_chain,
158
    ));
159
  }
160
161
  /**
162
   * Вставляет данные в счётчик
163
   *
164
   * @param $user_id_unsafe
165
   */
166
  public function db_counter_insert($user_id_unsafe) {
167
    global $sys_stop_log_hit;
168
169
    if($sys_stop_log_hit || !classSupernova::$config->game_counter) {
170
      return;
171
    }
172
173
    classSupernova::$db->isWatching = true;
174
    $values = array(
175
      'visit_time'  => SN_TIME_SQL,
176
      'user_id'     => $user_id_unsafe,
177
      'device_id'   => $this->device_id,
178
      'browser_id'  => $this->browser_id,
179
      'user_ip'     => $this->ip_v4_int,
180
      'user_proxy'  => $this->ip_v4_proxy_chain,
181
      'page_url_id' => $this->page_address_id,
182
    );
183
    $this->write_full_url ? $values['plain_url_id'] = $this->page_url_id : false;
184
    classSupernova::$db->doInsertSet(TABLE_COUNTER, $values);
185
186
    classSupernova::$db->isWatching = false;
187
  }
188
189
}
190