| Total Complexity | 6 |
| Total Lines | 43 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | class PassportMultiauth |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Set the current user for the application with the given scopes. |
||
| 16 | * |
||
| 17 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
| 18 | * @param array $scopes |
||
| 19 | * @return void |
||
| 20 | * @throws Exception |
||
| 21 | */ |
||
| 22 | 8 | public static function actingAs($user, $scopes = []) |
|
| 23 | { |
||
| 24 | 8 | $token = Mockery::mock(Token::class)->shouldIgnoreMissing(false); |
|
| 25 | |||
| 26 | 8 | foreach ($scopes as $scope) { |
|
| 27 | 1 | $token->shouldReceive('can')->with($scope)->andReturn(true); |
|
| 28 | } |
||
| 29 | |||
| 30 | 8 | $uses = array_flip(class_uses_recursive($user)); |
|
| 31 | |||
| 32 | 8 | if (! isset($uses[HasApiTokens::class])) { |
|
| 33 | 1 | throw new Exception('The model ['.get_class($user).'] must uses the trait '.HasApiTokens::class); |
|
| 34 | } |
||
| 35 | |||
| 36 | 7 | $user->withAccessToken($token); |
|
|
|
|||
| 37 | |||
| 38 | 7 | $guard = AuthConfigHelper::getUserGuard($user); |
|
| 39 | |||
| 40 | 7 | app('auth')->guard($guard)->setUser($user); |
|
| 41 | |||
| 42 | 7 | app('auth')->shouldUse($guard); |
|
| 43 | 7 | } |
|
| 44 | |||
| 45 | /** |
||
| 46 | * If running unit test and try authenticate an user with actingAs($user) |
||
| 47 | * check the guards on request to authenticate or not the user. |
||
| 48 | * |
||
| 49 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
| 50 | */ |
||
| 51 | 7 | public static function userActing() |
|
| 55 | } |
||
| 56 | 1 | } |
|
| 58 |