Passed
Pull Request — master (#19)
by Samuel
02:03
created

MultiauthActions   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 64
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
B multiauthActingAs() 0 34 4
1
<?php
2
3
namespace SMartins\PassportMultiauth\Testing;
4
5
use Laravel\Passport\Client;
6
7
trait MultiauthActions
8
{
9
    /**
10
     * The route to generate the access token. The default value is the standard
11
     * route from Laravel\Passport.
12
     *
13
     * @var string
14
     */
15
    protected $oauthTokenRoute = 'oauth/token';
16
17
    public function setUp()
18
    {
19
        parent::setUp();
20
21
        $this->artisan('passport:install');
0 ignored issues
show
Bug introduced by
It seems like artisan() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22
    }
23
24
    /**
25
     * Set the the Authorization header with an access token created using
26
     * Laravel Passport.
27
     *
28
     * @todo Change way to issue token from $this->json() to creating accessing
29
     *       AccessTokenController@issueToken directly.
30
     * @todo Pass this method to PassportMultiauth::actingAs().
31
     *
32
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
33
     * @param  string $scope
34
     * @return $this
35
     */
36
    public function multiauthActingAs($user, $scope = '')
37
    {
38
        $client = Client::where('personal_access_client', false)
39
                        ->where('revoked', false)
40
                        ->first();
41
42
        $provider = '';
43
        foreach (config('auth.providers') as $p => $config) {
44
            if ($user instanceof $config['model']) {
45
                $provider = $p;
46
            }
47
        }
48
49
        $params = [
50
            'grant_type' => 'password',
51
            'client_id' => $client->id,
52
            'client_secret' => $client->secret,
53
            'username' => $user->email,
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
54
            'password' => 'secret',
55
            'scope' => $scope,
56
        ];
57
58
        // If model to be authenticated don't has the default provider
59
        if (config('auth.guards.api.provider') !== $provider) {
60
            $params = array_merge($params, ['provider' => $provider]);
61
        }
62
63
        $response = $this->json('POST', $this->oauthTokenRoute, $params);
0 ignored issues
show
Bug introduced by
It seems like json() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
        $accessToken = json_decode($response->original)->access_token;
65
66
        $this->withHeader('Authorization', 'Bearer '.$accessToken);
0 ignored issues
show
Bug introduced by
It seems like withHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
68
        return $this;
69
    }
70
}
71