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

Helpers   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 33
c 1
b 0
f 1
dl 0
loc 59
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isUserNew() 0 27 5
A formatUsers() 0 12 2
A validatorPasses() 0 14 5
1
<?php
2
3
namespace App\Controllers\Admin\User\Traits;
4
5
trait Helpers
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