Completed
Pull Request — master (#47)
by
unknown
11:14
created

EloquentUserTokenRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 28
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A allForUser() 0 4 1
A generateFor() 0 11 2
1
<?php
2
3
namespace Modules\User\Repositories\Eloquent;
4
5
use Illuminate\Database\QueryException;
6
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
7
use Modules\User\Repositories\UserTokenRepository;
8
use Ramsey\Uuid\Uuid;
9
10
class EloquentUserTokenRepository extends EloquentBaseRepository implements UserTokenRepository
11
{
12
    /**
13
     * Get all tokens for the given user
14
     * @param int $userId
15
     * @return \Illuminate\Database\Eloquent\Collection
16
     */
17
    public function allForUser($userId)
18
    {
19
        return $this->model->where('user_id', $userId)->get();
20
    }
21
22
    /**
23
     * @param int $userId
24
     * @return \Modules\User\Entities\UserToken
25
     */
26
    public function generateFor($userId)
27
    {
28
        try {
29
            $uuid4 = Uuid::uuid4();
30
            $userToken = $this->model->create(['user_id' => $userId, 'access_token' => $uuid4]);
31
        } catch (QueryException $e) {
32
            $this->generateFor($userId);
33
        }
34
35
        return $userToken;
0 ignored issues
show
Bug introduced by
The variable $userToken does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
36
    }
37
}
38