Completed
Push — work-fleets ( 2bd11a...17dd3b )
by SuperNova.WS
06:36
created

PlayerToAccountTranslate::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 6
1
<?php
2
3
/**
4
 * Created by Gorlum 18.09.2015 11:36
5
 */
6
7
/**
8
 * Class PlayerToAccountTranslate
9
 */
10
class PlayerToAccountTranslate {
11
  /**
12
   * БД из которой читать данные
13
   *
14
   * @var db_mysql $db
15
   */
16
  protected static $db = null;
17
  protected static $is_init = false;
18
19
  protected static function init() {
20
    if(!empty(static::$db)) {
21
      return;
22
    }
23
    static::$db = classSupernova::$db;
24
  }
25
26
  /**
27
   * Регистрирует игрока на указанный аккаунт указанного провайдера
28
   *
29
   * @param $provider_id_safe
30
   * @param $provider_account_id_safe
31
   * @param $user_id_safe
32
   *
33
   * @return array|resource
34
   */
35
  // OK v4.7
36
  public static function db_translate_register_user($provider_id_unsafe, $provider_account_id_unsafe, $user_id_unsafe) {
37
    static::init();
38
39
    $provider_id_safe = static::$db->db_escape($provider_id_unsafe);
40
    $provider_account_id_safe = static::$db->db_escape($provider_account_id_unsafe);
41
    $user_id_safe = static::$db->db_escape($user_id_unsafe);
42
43
    return static::$db->doInsert(
44
      "INSERT INTO `{{account_translate}}` (`provider_id`, `provider_account_id`, `user_id`) VALUES
45
                  ({$provider_id_safe}, {$provider_account_id_safe}, {$user_id_safe});"
46
    );
47
  }
48
49
  /**
50
   * Возвращает из `account_translate` список пользователей, которые прилинкованы к списку аккаунтов на указанном провайдере
51
   *
52
   * @param int $provider_id_unsafe Идентификатор провайдера авторизации
53
   * @param int|int[] $account_list
54
   *
55
   * @return array
56
   */
57
  public static function db_translate_get_users_from_account_list($provider_id_unsafe, $account_list) {
58
    static::init();
59
60
    $account_translation = array();
61
62
    $provider_id_safe = intval($provider_id_unsafe);
63
    !is_array($account_list) ? $account_list = array($account_list) : false;
64
65
    foreach($account_list as $provider_account_id_unsafe) {
66
      $provider_account_id_safe = intval($provider_account_id_unsafe);
67
68
      // TODO - Здесь могут отсутствовать аккаунты - проверять провайдером
69
      $query = static::$db->doSelect(
70
        "SELECT `user_id` 
71
        FROM {{account_translate}} 
72
        WHERE `provider_id` = {$provider_id_safe} AND `provider_account_id` = {$provider_account_id_safe} 
73
        FOR UPDATE"
74
      );
75
      while($row = static::$db->db_fetch($query)) {
0 ignored issues
show
Bug introduced by
It seems like $query defined by static::$db->doSelect("S... \n FOR UPDATE") on line 69 can also be of type boolean; however, db_mysql::db_fetch() does only seem to accept object<mysqli_result>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76
        $account_translation[$row['user_id']][$provider_id_unsafe][$provider_account_id_unsafe] = true;
77
      }
78
    }
79
80
    return $account_translation;
81
  }
82
83
  public static function db_translate_get_account_by_user_id($user_id_unsafe, $provider_id_unsafe = 0) {
84
    static::init();
85
86
    $user_id_safe = static::$db->db_escape($user_id_unsafe);
87
    $provider_id_safe = static::$db->db_escape($provider_id_unsafe);
88
89
    $account_translation = array();
90
91
    $query = static::$db->doSelect(
92
      "SELECT * FROM {{account_translate}} WHERE `user_id` = {$user_id_safe} " .
93
      ($provider_id_unsafe ? "AND `provider_id` = {$provider_id_safe} " : '') .
94
      "ORDER BY `timestamp` FOR UPDATE");
95
    while($row = static::$db->db_fetch($query)) {
0 ignored issues
show
Bug introduced by
It seems like $query defined by static::$db->doSelect("S...timestamp` FOR UPDATE') on line 91 can also be of type boolean; however, db_mysql::db_fetch() does only seem to accept object<mysqli_result>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96
      $account_translation[$row['user_id']][$row['provider_id']][$row['provider_account_id']] = $row;
97
    }
98
99
    return $account_translation;
100
  }
101
102
  public static function db_translate_unregister_user($user_id_unsafe) {
103
    static::init();
104
105
    $user_id_safe = static::$db->db_escape($user_id_unsafe);
106
    return static::$db->doDelete("DELETE FROM `{{account_translate}}` WHERE `user_id` = {$user_id_safe});");
107
  }
108
109
}
110