Completed
Push — develop ( 2f90e1...bffb25 )
by Kirill
07:59
created

User::addKarmaToUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 2
nc 2
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 63 and the first side effect is on line 31.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace App;
3
4
use Carbon\Carbon;
5
use Gitter\Models\User as GitterUser;
6
use Illuminate\Auth\Authenticatable;
7
use Illuminate\Auth\Passwords\CanResetPassword;
8
use Illuminate\Foundation\Auth\Access\Authorizable;
9
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
10
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
11
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
12
13
14
/**
15
 * Class User
16
 * @package App
17
 *
18
 * @property int $id
19
 * @property string $gitter_id
20
 * @property string $name
21
 * @property string $avatar
22
 * @property string $url
23
 * @property string $login
24
 * @property string $email
25
 * @property string $password
26
 * @property string $remember_token
27
 * @property Carbon $created_at
28
 * @property Carbon $updated_at
29
 *
30
 */
31
class User extends \Eloquent implements
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
32
    AuthenticatableContract,
33
    AuthorizableContract,
34
    CanResetPasswordContract
35
{
36
    use Authenticatable,
37
        Authorizable,
38
        CanResetPassword;
39
40
    /**
41
     * The database table used by the model.
42
     * @var string
43
     */
44
    protected $table = 'users';
45
46
    /**
47
     * The attributes that are mass assignable.
48
     * @var array
49
     */
50
    protected $fillable = ['gitter_id', 'url', 'login', 'name', 'avatar', 'email', 'password'];
51
52
    /**
53
     * The attributes excluded from the model's JSON form.
54
     * @var array
55
     */
56
    protected $hidden = ['password', 'remember_token'];
57
58
    /**
59
     * @param GitterUser $gitterUser
60
     * @param \Closure $new
61
     * @return User
62
     */
63
    public static function make(GitterUser $gitterUser, \Closure $new = null)
64
    {
65
        if ($new === null) { $new = function(){}; }
66
67
        $user = static::where('gitter_id', $gitterUser->id)->first();
68
        if (!$user) {
69
            $user = static::create([
70
                'gitter_id' => $gitterUser->id,
71
                'name'      => $gitterUser->displayName,
72
                'avatar'    => $gitterUser->avatarUrlMedium,
73
                'url'       => $gitterUser->url,
74
                'login'     => $gitterUser->username,
75
                'email'     => null,
76
                'password'  => null,
77
            ]);
78
79
            $new($user);
80
        }
81
82
        return $user;
83
    }
84
85
    /**
86
     * @param $roomId
87
     * @return Carbon
88
     */
89
    public function getLastKarmaTimeForRoom($roomId)
90
    {
91
        $result = Karma::query()
92
            ->where('user_target_id', $this->gitter_id)
93
            ->where('room_id', $roomId)
94
            ->orderBy('created_at', 'desc')
95
            ->first();
96
97
        if ($result) {
98
            return $result->created_at;
99
        }
100
101
        return Carbon::createFromTimestamp(0);
102
    }
103
104
    public function addKarmaToUser(User $user, Carbon $time = null)
105
    {
106
        if ($time === null) { $time = Carbon::now(); }
0 ignored issues
show
Unused Code introduced by
$time is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
107
108
109
    }
110
}
111