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

AddTrait::insertPersonalInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 4
dl 0
loc 16
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Admin\User\Traits;
4
5
use RandomLib\Factory as Factory;
6
7
trait AddTrait
8
{
9
  public function new()
10
  {
11
    $countries = $this->countries('all', 'name');
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

11
    /** @scrutinizer ignore-call */ 
12
    $countries = $this->countries('all', 'name');
Loading history...
12
13
    $context = [
14
      'countries' => $countries,
15
    ];
16
    return $this->view->render('admin/pages/users/new', $context);
17
  }
18
19
  public function add()
20
  {
21
    $msg = null;
22
    $posts = $this->request->posts();
23
    $names = array_keys($posts);
24
25
    if (!$this->checkAddValidator($names)) {
26
      $msg = $this->validator->getErrors();
27
      return json_encode($msg);
28
    }
29
    extract($this->generateUserIdCode());
30
31
    $table = $this->load->model('User')->getTable();
32
33
    $insertPersonalInfo = $this->insertPersonalInfo($posts, $table, $user_id, $code);
34
    $insertInAddress = $this->insertUserAddress($posts, $user_id);
35
    $insertUserActivities = $this->insertUserActivities($user_id);
36
37
    if (!$insertPersonalInfo || !$insertInAddress || !$insertUserActivities) {
38
      $msg = 'reload';
39
      return json_encode($msg);
40
    }
41
    $msg['success'] = $user_id;
42
    return json_encode($msg);
43
  }
44
45
  private function checkAddValidator($names)
46
  {
47
    $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

47
    /** @scrutinizer ignore-call */ 
48
    $columns = $this->getUserConfigColumns();
Loading history...
48
49
    foreach ($names as $name) {
50
      $filters = $columns->$name->filters;
51
      $this->validatorPasses($filters, $name);
0 ignored issues
show
Bug introduced by
It seems like validatorPasses() 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

51
      $this->/** @scrutinizer ignore-call */ 
52
             validatorPasses($filters, $name);
Loading history...
52
    }
53
    if ($this->validator->fails()) {
54
      return false;
55
    }
56
    return true;
57
  }
58
59
  private function insertPersonalInfo($posts, $table, $user_id, $code)
60
  {
61
    $birthday = date('Y-m-d', strtotime($posts['birthday']));
62
    $registration = $this->changeFormatDate(microtime(true), ['U.u', 'Y-m-d H:i:s']);
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

62
    /** @scrutinizer ignore-call */ 
63
    $registration = $this->changeFormatDate(microtime(true), ['U.u', 'Y-m-d H:i:s']);
Loading history...
63
64
    return $this->db->data([
65
      'id' => $user_id,
66
      'code' => $code,
67
      'username' => $posts['username'],
68
      'fname' => $posts['fname'],
69
      'lname' => $posts['lname'],
70
      'gender' => $posts['gender'],
71
      'birthday' => $birthday,
72
      'email' => $posts['email'],
73
      'registration' => $registration,
74
    ])->insert($table);
75
  }
76
77
  private function setNullToUserAddressWhenEmpty($posts)
78
  {
79
    $array = [
80
      'country' => $posts['country'],
81
      'state' => $posts['state'],
82
      'city' => $posts['city'],
83
      'zip' => $posts['zip'],
84
      'street' => $posts['street'],
85
      'house_number' => $posts['house_number'],
86
      'additional' => $posts['additional'],
87
    ];
88
    foreach($array as $key => $value) {
89
      if (!$value) {
90
        $value = null;
91
      }
92
      $array[$key] = $value;
93
    }
94
    return $array;
95
  }
96
97
  private function insertUserAddress($posts, $user_id)
98
  {
99
    $posts = $this->setNullToUserAddressWhenEmpty($posts);
100
101
    return $this->db->data([
102
      'user_id' => $user_id,
103
      'country' => $posts['country'],
104
      'state' => $posts['state'],
105
      'city' => $posts['city'],
106
      'zip' => $posts['zip'],
107
      'street' => $posts['street'],
108
      'house_number' => $posts['house_number'],
109
      'additional' => $posts['additional'],
110
    ])->insert('address');
111
  }
112
113
  private function insertUserActivities($user_id)
114
  {
115
    return $this->db->data([
116
      'user_id' => $user_id,
117
      'is_login' => 0,
118
    ])->insert('activity');
119
  }
120
121
  private function generateUserIdCode()
122
  {
123
    $factory = new Factory;
124
    $user_id = $factory->getMediumStrengthGenerator()->generateString(8, '0123456789');
125
    $code = $factory->getMediumStrengthGenerator()->generateString(20, '0123456789abcdefghijklmnopqrstuvwxyz');
126
127
    return [
128
      'user_id' => $user_id,
129
      'code' => $code,
130
    ];
131
  }
132
}
133