Passed
Branch master (7fff21)
by refat
05:12
created

UpdateTrait::update()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 8
nop 0
dl 0
loc 29
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Admin\User\Traits;
4
5
trait UpdateTrait
6
{
7
  public function update()
8
  {
9
    $id = userId();
10
11
    $posts = $this->request->posts();
12
    $name = array_keys($posts)[0];
13
    $columns = $this->getUserConfigColumns();
0 ignored issues
show
Bug introduced by
It seems like getUserConfigColumns() 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

13
    /** @scrutinizer ignore-call */ 
14
    $columns = $this->getUserConfigColumns();
Loading history...
14
    $table = $columns->$name->table;
15
    $column = $columns->$name;
16
    $filters = $columns->$name->filters;
17
    $value = ($posts[$name] == '') ? null : isset($filters->date) ? date('Y-m-d', strtotime($posts[$name])) : $posts[$name];
18
    $user_id_table_name = $column->user_id_table_name;
19
20
    $methods = $this->updateMethodsToCheckBeforeContinue([
21
      'id' => $id,
22
      'name' => $name,
23
      'table' => $table,
24
      'user_id_table_name' => $user_id_table_name,
25
      'value' => $value,
26
      'filters' => $filters,
27
    ]);
28
29
    $error = $this->checkForErrorsInUpdateMethods($methods);
30
31
    if ($error) {
32
      return json_encode($error);
33
    }
34
    $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...eTrait::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...
35
    return json_encode($msg);
36
  }
37
38
  private function checkForErrorsInUpdateMethods($methods)
39
  {
40
    foreach ($methods as $method => $options) {
41
      if (call_user_func_array(array($this, $method), $options[0]) == false) {
42
        return $this->updateErrorMsg($options);
43
      }
44
    }
45
    return false;
46
  }
47
48
  private function updateErrorMsg($options)
49
  {
50
    $msg = null;
51
52
    if (array_keys($options[1])[0] === 'msg') {
53
      $msg = array_values($options[1]);
54
    } else {
55
      if (array_keys($options[1])[0] === 'error') {
56
        $msg['error'] = $this->validator->getErrors();
57
      } else {
58
        $msg[array_keys($options[1])[0]] = array_values($options[1]);
59
      }
60
    }
61
    return $msg;
62
  }
63
64
  private function updateMethodsToCheckBeforeContinue($args)
65
  {
66
    extract($args);
67
    return [
68
      'isUserFound' => [
69
        [$id],
70
        ['msg' => 'reload'],
71
      ],
72
      'isValueChanged' => [
73
        [$name, $table, $user_id_table_name, $id, $value],
74
        ['same' => $value ? strtolower($value) : ''],
75
      ],
76
      'validatorPasses' => [
77
        [$filters, $name],
78
        ['error' => ''],
79
      ],
80
      'updateUser' => [
81
        [$name, $value, $user_id_table_name, $id, $table],
82
        ['msg' => 'reload'],
83
      ],
84
    ];
85
  }
86
87
  private function userUpdateMsg($name, $value, $filters)
88
  {
89
    $msg = null;
90
91
    if ($name === 'country') {
92
      $msg['country'] = [
93
        $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

93
        $value => $this->/** @scrutinizer ignore-call */ countries($value),
Loading history...
94
      ];
95
    } else {
96
      $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

96
      $msg['text'] = isset($filters->date) ? $this->/** @scrutinizer ignore-call */ changeFormatDate($value, ['Y-m-d', 'd M Y']) : _e($value);
Loading history...
97
    }
98
    return $msg;
99
  }
100
101
  private function updateUser($name, $value, $user_id_table_name, $id, $table)
102
  {
103
    return $this->db->data($name, $value)->where($user_id_table_name . ' = ?', $id)->update($table);
104
  }
105
106
  private function isUserFound($id)
107
  {
108
    return $this->load->model('User')->get($id);
109
  }
110
111
  private function isValueChanged($name, $table, $user_id_table_name, $id, $value)
112
  {
113
    $current_value = $this->db->select($name)->from($table)->where($user_id_table_name . ' = ?', [$id])->fetch()->$name;
114
    if (($current_value === strtolower($value)) || ($value == null && $current_value == null)) {
115
      return false;
116
    }
117
    return true;
118
  }
119
}
120