Completed
Pull Request — master (#8)
by Karl
18:46 queued 16:09
created

UserRepository::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php namespace JobApis\JobsToMail\Repositories;
2
3
use Carbon\Carbon;
4
use JobApis\JobsToMail\Models\Token;
5
use JobApis\JobsToMail\Models\User;
6
use JobApis\JobsToMail\Notifications\TokenGenerated;
7
8
class UserRepository implements Contracts\UserRepositoryInterface
9
{
10
    /**
11
     * @var Token model
12
     */
13
    public $tokens;
14
15
    /**
16
     * @var User model
17
     */
18
    public $users;
19
20
    /**
21
     * UserRepository constructor.
22
     *
23
     * @param User $model
0 ignored issues
show
Bug introduced by
There is no parameter named $model. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
24
     */
25 7
    public function __construct(User $users, Token $tokens)
26
    {
27 7
        $this->users = $users;
28 7
        $this->tokens = $tokens;
29 7
    }
30
31
    /**
32
     * Confirms a user's email and activates their account.
33
     *
34
     * @param $token string
35
     *
36
     * @return boolean
37
     */
38 2
    public function confirm($token = null)
39
    {
40 2
        $tokenObject = $this->getUnexpiredConfirmationToken($token);
41 2
        if ($tokenObject) {
42 1
            if ($this->update($tokenObject->user_id, ['confirmed_at' => Carbon::now()])) {
43 1
                return true;
44
            }
45
        }
46 1
        return false;
47
    }
48
49
    /**
50
     * Creates a single new user if data is valid
51
     *
52
     * @param $data array
53
     *
54
     * @return \JobApis\JobsToMail\Models\User
55
     */
56 1
    public function create($data = [])
57
    {
58
        // Create the user
59 1
        if ($user = $this->users->create($data)) {
60
            // Create a token
61 1
            $token = $this->generateToken($user->id, config('tokens.types.confirm'));
62
            // Email the token in link to the User
63 1
            $user->notify(new TokenGenerated($token));
64
        }
65 1
        return $user;
66
    }
67
68
    /**
69
     * Retrieves a single record by ID
70
     *
71
     * @param $id string
72
     * @param $options array
73
     *
74
     * @return \JobApis\JobsToMail\Models\User
75
     */
76 1
    public function getById($id = null, $options = [])
77
    {
78 1
        return $this->users->where('id', $id)->first();
79
    }
80
81
    /**
82
     * Retrieves all active user accounts
83
     *
84
     * @return \Illuminate\Database\Eloquent\Collection
85
     */
86 1
    public function getConfirmed()
87
    {
88 1
        return $this->users->confirmed()->get();
89
    }
90
91
    /**
92
     * Update a single user
93
     *
94
     * @param $id string
95
     * @param $data array
96
     *
97
     * @return boolean
98
     */
99 1
    public function update($id = null, $data = [])
100
    {
101 1
        return $this->users->where('id', $id)->update($data);
102
    }
103
104
    /**
105
     * Deletes a user.
106
     *
107
     * @param $id string
108
     *
109
     * @return boolean
110
     */
111 1
    public function unsubscribe($id = null)
112
    {
113 1
        return $this->users->where('id', $id)->delete();
114
    }
115
116
    /**
117
     * Get Confirmation Token by token id if not expired
118
     *
119
     * @param string $token
120
     *
121
     * @return mixed
122
     */
123 2
    private function getUnexpiredConfirmationToken($token = null, $daysToExpire = 30)
124
    {
125 2
        return $this->tokens
126 2
            ->where('token', $token)
127 2
            ->where('type', config('tokens.types.confirm'))
128 2
            ->where('created_at', '>', Carbon::now()->subDays($daysToExpire))
129 2
            ->first();
130
    }
131
132
    /**
133
     * Generates and returns a token for a specific User Id
134
     *
135
     * @param null $user_id
136
     * @param string $type
137
     *
138
     * @return Token
139
     */
140 1
    private function generateToken($user_id = null, $type = 'confirm')
141
    {
142 1
        return $this->tokens->create([
143 1
            'user_id' => $user_id,
144 1
            'type' => $type,
145
        ]);
146
    }
147
}
148