Passed
Branch master (75bbf9)
by refat
08:47 queued 04:24
created

Update::checkPostParametersUpdate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Controllers\Admin\User\Traits;
4
5
trait Update
6
{
7
  public function update()
8
  {
9
    $id = userId();
10
11
    $posts = $this->request->posts();
12
    $name = array_keys($posts)[0];
13
    $allows = $this->file->call('config/admin/users/pages/update.php');
14
15
    $columns = $this->file->fileContent('config/admin/users/columns.json');
16
    $columns = json_decode($columns);
17
    $table = $columns->$name->table;
18
    $column = $columns->$name;
19
    $filters = $columns->$name->filters;
20
    $value = ($posts[$name] == '') ? null : isset($filters->date) ? date('Y-m-d', strtotime($posts[$name])) : $posts[$name];
21
    $user_id_table_name = $column->user_id_table_name;
22
23
    $methods = $this->updateMethodsToCheckBeforeContinue([
24
      'id' => $id,
25
      'name' => $name,
26
      'allows' => $allows,
27
      'table' => $table,
28
      'user_id_table_name' => $user_id_table_name,
29
      'value' => $value,
30
      'filters' => $filters,
31
    ]);
32
33
    $error = $this->checkForErrorsInUpdateMethods($methods);
34
35
    if ($error) {
36
      return json_encode($error);
37
    }
38
    $msg = $this->userUpdateMsg($name, $value, $filters);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $msg is correct as $this->userUpdateMsg($name, $value, $filters) targeting App\Controllers\Admin\Us...Update::userUpdateMsg() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
39
    return json_encode($msg);
40
  }
41
42
  private function checkForErrorsInUpdateMethods($methods)
43
  {
44
    foreach ($methods as $method => $options) {
45
      if (call_user_func_array(array($this, $method), $options[0]) == false) {
46
        return $this->updateErrorMsg($options);
47
      }
48
    }
49
    return false;
50
  }
51
52
  private function updateErrorMsg($options)
53
  {
54
    $msg = null;
55
56
    if (array_keys($options[1])[0] === 'msg') {
57
      $msg = array_values($options[1]);
58
    } else {
59
      if (array_keys($options[1])[0] === 'error') {
60
        $msg['error'] = $this->validator->getErrors();
61
      } else {
62
        $msg[array_keys($options[1])[0]] = array_values($options[1]);
63
      }
64
    }
65
    return $msg;
66
  }
67
68
  private function updateMethodsToCheckBeforeContinue($args)
69
  {
70
    extract($args);
71
    return [
72
      'isUserFound' => [
73
        [$id],
74
        ['msg' => 'reload'],
75
      ],
76
      'checkPostParametersUpdate' => [
77
        [$name, $allows],
78
        ['msg' => 'reload'],
79
      ],
80
      'isValueChanged' => [
81
        [$name, $table, $user_id_table_name, $id, $value],
82
        ['same' => $value ? strtolower($value) : ''],
83
      ],
84
      'validatorPasses' => [
85
        [$filters, $name],
86
        ['error' => ''],
87
      ],
88
      'updateUser' => [
89
        [$name, $value, $user_id_table_name, $id, $table],
90
        ['msg' => 'reload'],
91
      ],
92
    ];
93
  }
94
95
  private function userUpdateMsg($name, $value, $filters)
96
  {
97
    $msg = null;
98
99
    if ($name === 'country') {
100
      $msg['country'] = [
101
        $value => $this->countries($value),
0 ignored issues
show
Bug introduced by
It seems like countries() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        $value => $this->/** @scrutinizer ignore-call */ countries($value),
Loading history...
102
      ];
103
    } else {
104
      $msg['text'] = isset($filters->date) ? $this->changeFormatDate($value, ['Y-m-d', 'd M Y']) : _e($value);
0 ignored issues
show
Bug introduced by
It seems like changeFormatDate() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
      $msg['text'] = isset($filters->date) ? $this->/** @scrutinizer ignore-call */ changeFormatDate($value, ['Y-m-d', 'd M Y']) : _e($value);
Loading history...
105
    }
106
    return $msg;
107
  }
108
109
  private function updateUser($name, $value, $user_id_table_name, $id, $table)
110
  {
111
    return $this->db->data($name, $value)->where($user_id_table_name . ' = ?', $id)->update($table);
112
  }
113
114
  private function isUserFound($id)
115
  {
116
    return $this->load->model('User')->get($id);
117
  }
118
119
  private function isValueChanged($name, $table, $user_id_table_name, $id, $value)
120
  {
121
    $current_value = $this->db->select($name)->from($table)->where($user_id_table_name . ' = ?', [$id])->fetch()->$name;
122
    if (($current_value === strtolower($value)) || ($value == null && $current_value == null)) {
123
      return false;
124
    }
125
    return true;
126
  }
127
128
  private function checkPostParametersUpdate($name, $allows)
129
  {
130
    if (!in_array($name, $allows)) {
131
      return false;
132
    }
133
    return true;
134
  }
135
}
136