Completed
Push — 2.0 ( 090eab...b837a7 )
by Nicolas
14:41
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 namespace Modules\User\Repositories\Eloquent;
2
3
use Illuminate\Database\QueryException;
4
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
5
use Modules\User\Repositories\UserTokenRepository;
6
use Ramsey\Uuid\Uuid;
7
8
class EloquentUserTokenRepository extends EloquentBaseRepository implements UserTokenRepository
9
{
10
    /**
11
     * Get all tokens for the given user
12
     * @param int $userId
13
     * @return \Illuminate\Database\Eloquent\Collection
14
     */
15
    public function allForUser($userId)
16
    {
17
        return $this->model->where('user_id', $userId)->get();
18
    }
19
20
    /**
21
     * @param int $userId
22
     * @return \Modules\User\Entities\UserToken
23
     */
24
    public function generateFor($userId)
25
    {
26
        try {
27
            $uuid4 = Uuid::uuid4();
28
            $userToken = $this->model->create(['user_id' => $userId, 'access_token' => $uuid4]);
29
        } catch (QueryException $e) {
30
            $this->generateFor($userId);
31
        }
32
33
        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...
34
    }
35
}
36