Passed
Push — master ( 87e211...2aec27 )
by Samuel
03:04
created

MultiauthActions::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SMartins\PassportMultiauth\Testing;
4
5
use Laravel\Passport\Client;
6
use Illuminate\Support\Facades\App;
7
use Illuminate\Routing\Route;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
11
trait MultiauthActions
12
{
13
    /**
14
     * The route to generate the access token. The default value is the standard
15
     * route from Laravel\Passport.
16
     *
17
     * @var string
18
     */
19
    protected $passportOAuthTokenRoute = 'oauth/token';
20
21
    /**
22
     * @codeCoverageIgnore
23
     */
24
    public function setUp()
25
    {
26
        parent::setUp();
27
28
        $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...
29
    }
30
31
    /**
32
     * Set the the Authorization header with an access token created using
33
     * Laravel Passport. The method `ẁithHeader` of trait `MakesHttpRequests` is
34
     * avaiable after version 5.5 of Laravel Framework.
35
     *
36
     * @todo Change way to issue token from $this->json() to creating accessing
37
     *       AccessTokenController@issueToken directly.
38
     * @todo Pass this method to PassportMultiauth::actingAs().
39
     *
40
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
41
     * @param  string $scope
42
     * @return $this
43
     */
44 10
    public function multiauthActingAs(Authenticatable $user, $scope = '')
45
    {
46 10
        if ((float) App::version() < 5.5) {
47 1
            throw new \RuntimeException('The method is only available to Laravel >= 5.5. To older versions try use $this->multiauthAccessToken() to get access token.');
48
        }
49
50 9
        $this->withHeader('Authorization', $this->multiauthAccessToken($user, $scope));
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...
51
52 8
        return $this;
53
    }
54
55
    /**
56
     * Get multiauth header to be used on request to get access token.
57
     *
58
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
59
     * @param  string $scope
60
     * @return string
61
     */
62 9
    public function multiauthAccessToken(Authenticatable $user, $scope = '')
63
    {
64
        // @todo Change to specific repository
65 9
        $client = Client::where('personal_access_client', false)
66 9
                        ->where('revoked', false)
67 9
                        ->first();
68
69 9
        if (! $client) {
70 1
            throw new ModelNotFoundException('Laravel\Passport password grant not found. Please run `passport:install` to generate client.');
71
        }
72
73 8
        $provider = $this->getUserProvider($user);
74
75
        $params = [
76 8
            'grant_type' => 'password',
77 8
            'client_id' => $client->id,
78 8
            'client_secret' => $client->secret,
79 8
            '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...
80 8
            'password' => 'secret',
81 8
            'scope' => $scope,
82
        ];
83
84
        // If model to be authenticated don't is the default provider
85 8
        if (! $this->isDefaultProvider($provider)) {
86 4
            $params = array_merge($params, ['provider' => $provider]);
87
        }
88
89 8
        if (property_exists($this, 'oauthTokenRoute')) {
90
            $this->passportOAuthTokenRoute = $this->oauthTokenRoute;
0 ignored issues
show
Bug introduced by
The property oauthTokenRoute does not seem to exist. Did you mean passportOAuthTokenRoute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
91
        }
92
93 8
        $response = $this->json('POST', $this->passportOAuthTokenRoute, $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...
94
95 8
        $accessToken = json_decode($response->getContent())->access_token;
96
97 8
        return 'Bearer '.$accessToken;
98
    }
99
100
    /**
101
     * Get the user provider on configs.
102
     *
103
     * @todo Move to class specialized in check auth configs.
104
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
105
     * @return string
106
     */
107 8
    protected function getUserProvider(Authenticatable $user)
108
    {
109 8
        $provider = '';
110 8
        foreach (config('auth.providers') as $p => $config) {
111 8
            if ($user instanceof $config['model']) {
112 8
                $provider = $p;
113
            }
114
        }
115
116 8
        return $provider;
117
    }
118
119
    /**
120
     * Check if provider is the default provider used by Laravel\Passport.
121
     *
122
     * @todo Move to class specialized in check auth configs.
123
     * @param string $provider
124
     * @return bool
125
     */
126 8
    protected function isDefaultProvider($provider)
127
    {
128 8
        return config('auth.guards.api.provider') === $provider;
129
    }
130
}
131