Completed
Push — master ( b618f7...e48405 )
by Karl
11s
created

UserRepository::unsubscribe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 8
    public function __construct(User $users, Token $tokens)
26
    {
27 8
        $this->users = $users;
28 8
        $this->tokens = $tokens;
29 8
    }
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, generate a token and notify the user.
51
     *
52
     * @param $data array
53
     *
54
     * @return \JobApis\JobsToMail\Models\User
55
     */
56 2
    public function create($data = [])
57
    {
58 2
        $user = $this->users->create($data);
59
60 2
        $this->sendConfirmationToken($user);
61
62 2
        return $user;
63
    }
64
65
    /**
66
     * Deletes a user.
67
     *
68
     * @param $id string
69
     *
70
     * @return boolean
71
     */
72 1
    public function delete($id = null)
73
    {
74 1
        return $this->users->where('id', $id)->delete();
75
    }
76
77
    /**
78
     * Creates a single new user or returns an existing one by email
79
     *
80
     * @param $data array
81
     *
82
     * @return \JobApis\JobsToMail\Models\User
83
     */
84 3
    public function firstOrCreate($data = [])
85
    {
86 3
        if ($user = $this->users->where('email', $data['email'])->first()) {
87
            // Resend the user a confirmation token if they haven't confirmed
88 2
            if (!$user->confirmed_at) {
89 1
                $this->sendConfirmationToken($user);
90
            }
91 2
            $user->existed = true;
92 2
            return $user;
93
        }
94 1
        return $this->create($data);
95
    }
96
97
    /**
98
     * Retrieves a single record by ID
99
     *
100
     * @param $id string
101
     * @param $options array
102
     *
103
     * @return \JobApis\JobsToMail\Models\User
104
     */
105 1
    public function getById($id = null, $options = [])
106
    {
107 1
        return $this->users->where('id', $id)->first();
108
    }
109
110
    /**
111
     * Update a single user
112
     *
113
     * @param $id string
114
     * @param $data array
115
     *
116
     * @return boolean
117
     */
118 1
    public function update($id = null, $data = [])
119
    {
120 1
        return $this->users->where('id', $id)->update($data);
121
    }
122
123
    /**
124
     * Get Confirmation Token by token id if not expired
125
     *
126
     * @param string $token
127
     *
128
     * @return mixed
129
     */
130 2
    private function getUnexpiredConfirmationToken($token = null, $daysToExpire = 30)
131
    {
132 2
        return $this->tokens
133 2
            ->where('token', $token)
134 2
            ->where('type', config('tokens.types.confirm'))
135 2
            ->where('created_at', '>', Carbon::now()->subDays($daysToExpire))
136 2
            ->first();
137
    }
138
139
    /**
140
     * Generates and returns a token for a specific User Id
141
     *
142
     * @param null $user_id
143
     * @param string $type
144
     *
145
     * @return Token
146
     */
147 3
    private function generateToken($user_id = null, $type = 'confirm')
148
    {
149 3
        return $this->tokens->create([
150 3
            'user_id' => $user_id,
151 3
            'type' => $type,
152
        ]);
153
    }
154
155
    /**
156
     * Generates a new confirmation token and sends it to the user
157
     *
158
     * @param User $user
159
     *
160
     * @return Token
161
     */
162 3
    private function sendConfirmationToken(User $user)
163
    {
164
        // Create a token
165 3
        $token = $this->generateToken($user->id, config('tokens.types.confirm'));
166
        // Email the token in link to the User
167 3
        $user->notify(new TokenGenerated($token));
168
169 3
        return $token;
170
    }
171
}
172