Completed
Pull Request — master (#10)
by Karl
04:24
created

UserRepository::getConfirmed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
cc 2
eloc 5
nc 2
nop 1
crap 2.032
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, or accounts for a single email if specified.
83
     *
84
     * @param $email null | string
85
     *
86
     * @return \Illuminate\Database\Eloquent\Collection
87
     */
88 1
    public function getConfirmed($email = null)
89
    {
90 1
        $query = $this->users->confirmed();
91 1
        if ($email) {
92
            $query = $query->where('email', $email);
93
        }
94 1
        return $query->get();
95
    }
96
97
    /**
98
     * Update a single user
99
     *
100
     * @param $id string
101
     * @param $data array
102
     *
103
     * @return boolean
104
     */
105 1
    public function update($id = null, $data = [])
106
    {
107 1
        return $this->users->where('id', $id)->update($data);
108
    }
109
110
    /**
111
     * Deletes a user.
112
     *
113
     * @param $id string
114
     *
115
     * @return boolean
116
     */
117 1
    public function unsubscribe($id = null)
118
    {
119 1
        return $this->users->where('id', $id)->delete();
120
    }
121
122
    /**
123
     * Get Confirmation Token by token id if not expired
124
     *
125
     * @param string $token
126
     *
127
     * @return mixed
128
     */
129 2
    private function getUnexpiredConfirmationToken($token = null, $daysToExpire = 30)
130
    {
131 2
        return $this->tokens
132 2
            ->where('token', $token)
133 2
            ->where('type', config('tokens.types.confirm'))
134 2
            ->where('created_at', '>', Carbon::now()->subDays($daysToExpire))
135 2
            ->first();
136
    }
137
138
    /**
139
     * Generates and returns a token for a specific User Id
140
     *
141
     * @param null $user_id
142
     * @param string $type
143
     *
144
     * @return Token
145
     */
146 1
    private function generateToken($user_id = null, $type = 'confirm')
147
    {
148 1
        return $this->tokens->create([
149 1
            'user_id' => $user_id,
150 1
            'type' => $type,
151
        ]);
152
    }
153
}
154