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

Confirmation::make_password_reset_code()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
/**
4
 * Created by Gorlum 17.09.2015 14:11
5
 */
6
class Confirmation {
7
8
  /**
9
   * @var db_mysql
10
   */
11
  protected $db = null;
12
13
  public function __construct($db) {
14
    $this->db = $db;
15
  }
16
17
  // TODO - НЕ ОБЯЗАТЕЛЬНО ОТПРАВЛЯТЬ ЧЕРЕЗ ЕМЕЙЛ! ЕСЛИ ЭТО ФЕЙСБУЧЕК ИЛИ ВКШЕЧКА - МОЖНО ЧЕРЕЗ ЛС ПИСАТЬ!!
18
  // TODO - OK 4.6
19
  public function db_confirmation_get_latest_by_type_and_email($confirmation_type_safe, $email_unsafe) {
20
    $email_safe = $this->db->db_escape($email_unsafe);
21
22
    return $this->db->doSelectFetch(
23
      "SELECT * FROM {{confirmations}} WHERE
24
          `type` = {$confirmation_type_safe} AND `email` = '{$email_safe}' ORDER BY create_time DESC LIMIT 1;"
25
    );
26
  }
27
  // TODO - OK 4.6
28
  public function db_confirmation_delete_by_type_and_email($confirmation_type_safe, $email_unsafe) {
29
    $email_safe = $this->db->db_escape($email_unsafe);
30
31
    return $this->db->doDelete("DELETE FROM {{confirmations}} WHERE `type` = {$confirmation_type_safe} AND `email` = '{$email_safe}'");
32
  }
33
  // TODO - OK 4.6
34 View Code Duplication
  public function db_confirmation_get_unique_code_by_type_and_email($confirmation_type_safe, $email_unsafe) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    $email_safe = $this->db->db_escape($email_unsafe);
36
37
    do {
38
      // Ну, если у нас > 999.999 подтверждений - тут нас ждут проблемы...
39
      $confirm_code_safe = $this->db->db_escape($confirm_code_unsafe = $this->make_password_reset_code());
40
      // $query = static::$db->doquery("SELECT `id` FROM {{confirmations}} WHERE `code` = '{$confirm_code_safe}' AND `type` = {$confirmation_type_safe} FOR UPDATE", true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
41
      // Тип не нужен для проверки - код подтверждения должен быть уникален от слова "совсем"
42
      $query = $this->db->doSelectFetch("SELECT `id` FROM {{confirmations}} WHERE `code` = '{$confirm_code_safe}' FOR UPDATE");
43
    } while($query);
0 ignored issues
show
Bug Best Practice introduced by
The expression $query of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
44
45
    $this->db->doReplace(
46
      "REPLACE INTO {{confirmations}}
47
        SET `type` = {$confirmation_type_safe}, `code` = '{$confirm_code_safe}', `email` = '{$email_safe}';");
48
49
    return $confirm_code_unsafe;
50
  }
51
  // TODO - OK 4.6
52
  public function db_confirmation_get_by_type_and_code($confirmation_type_safe, $confirmation_code_unsafe) {
53
    $confirmation_code_safe = $this->db->db_escape($confirmation_code_unsafe);
54
55
    return $this->db->doSelectFetch(
56
      "SELECT * 
57
      FROM {{confirmations}} 
58
      WHERE
59
        `type` = {$confirmation_type_safe} 
60
        AND 
61
        `code` = '{$confirmation_code_safe}' 
62
      ORDER BY create_time 
63
      DESC LIMIT 1 
64
      FOR UPDATE"
65
    );
66
  }
67
68
  protected function make_password_reset_code() {
69
    return sys_random_string(LOGIN_PASSWORD_RESET_CONFIRMATION_LENGTH, SN_SYS_SEC_CHARS_CONFIRMATION);
70
  }
71
72
}
73