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

HelpersTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A formatUsers() 0 12 2
A getUserConfigColumns() 0 4 1
A validatorPasses() 0 14 5
A isUserNew() 0 27 5
1
<?php
2
3
namespace App\Controllers\Admin\User\Traits;
4
5
trait HelpersTrait
6
{
7
  private function formatUsers($users)
8
  {
9
    $users_for_list = [];
10
11
    foreach ($users as $user) {
12
      $user->new = $this->isUserNew($user->registration);
13
      $user->country_icon = $this->countries($user->country);
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

13
      /** @scrutinizer ignore-call */ 
14
      $user->country_icon = $this->countries($user->country);
Loading history...
14
      $user->registration = $this->changeFormatDate($user->registration);
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

14
      /** @scrutinizer ignore-call */ 
15
      $user->registration = $this->changeFormatDate($user->registration);
Loading history...
15
      $user->last_login = $this->changeFormatDate($user->last_login);
16
      $users_for_list[] = $user;
17
    }
18
    return $users_for_list;
19
  }
20
21
  private function isUserNew($date)
22
  {
23
    if (!$date) {
24
      return;
25
    }
26
    $register_year = $this->changeFormatDate($date, ['Y-m-d H:i:s', 'Y']);
27
    $register_month = $this->changeFormatDate($date, ['Y-m-d H:i:s', 'm']);
28
    $register_day = $this->changeFormatDate($date, ['Y-m-d H:i:s', 'd']);
29
30
    $year = date('Y');
31
    $month = date('m');
32
    $day = date('d');
33
34
    $years = $year - $register_year;
35
36
    if ($years === 0) {
37
      $months = $month - $register_month;
38
39
      if ($months === 0) {
40
        $days = $day - $register_day;
41
42
        if ($days < 1) {
43
          return 1;
44
        }
45
      }
46
    }
47
    return 0;
48
  }
49
50
  private function validatorPasses($filters, $name)
51
  {
52
    foreach ($filters as $func => $arg) {
53
      if (method_exists($this->validator, $func) == 1) {
54
        if (gettype($arg) === 'boolean') {
55
          if ($arg) {
56
            $this->validator->input($name)->$func();
57
          }
58
        } else {
59
          $this->validator->input($name)->$func($arg);
60
        }
61
      }
62
    }
63
    return $this->validator->passes();
64
  }
65
66
  private function getUserConfigColumns()
67
  {
68
    $columns = $this->file->fileContent('config/admin/users/columns.json');
69
    return json_decode($columns);
70
  }
71
}
72