Skip to content

Commit

Permalink
Put IdentityStore behind interface
Browse files Browse the repository at this point in the history
To allow end user to swap
  • Loading branch information
adamwathan committed Jun 10, 2015
1 parent cc303df commit b0f115a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
28 changes: 28 additions & 0 deletions src/EloquentIdentityStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php namespace AdamWathan\EloquentOAuth;

class EloquentIdentityStore implements IdentityStore
{
public function getByProvider($provider, $providerUser)
{
return OAuthIdentity::where('provider', $provider)
->where('provider_user_id', $providerUser->id)
->first();
}

public function flush($user, $provider)
{
OAuthIdentity::where('user_id', $user->getKey())
->where('provider', $provider)
->delete();
}

public function store($identity)
{
$identity->save();
}

public function userExists($provider, $providerUser)
{
return (bool) $this->getByProvider($provider, $providerUser);
}
}
29 changes: 5 additions & 24 deletions src/IdentityStore.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
<?php namespace AdamWathan\EloquentOAuth;

class IdentityStore
interface IdentityStore
{
public function getByProvider($provider, $providerUser)
{
return OAuthIdentity::where('provider', $provider)
->where('provider_user_id', $providerUser->id)
->first();
}

public function flush($user, $provider)
{
OAuthIdentity::where('user_id', $user->getKey())
->where('provider', $provider)
->delete();
}

public function store($identity)
{
$identity->save();
}

public function userExists($provider, $providerUser)
{
return (bool) $this->getByProvider($provider, $providerUser);
}
public function getByProvider($provider, $providerUser);
public function flush($user, $provider);
public function store($identity);
public function userExists($provider, $providerUser);
}
14 changes: 7 additions & 7 deletions tests/IdentityStoreTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use AdamWathan\EloquentOAuth\OAuthIdentity;
use AdamWathan\EloquentOAuth\IdentityStore;
use AdamWathan\EloquentOAuth\EloquentIdentityStore;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Mockery as M;
use SocialNorm\User as UserDetails;
Expand Down Expand Up @@ -36,7 +36,7 @@ public function test_get_by_provider()
'email' => 'john.doe@example.com',
'avatar' => 'http://example.com/photos/john_doe.jpg',
));
$identities = new IdentityStore;
$identities = new EloquentIdentityStore;
$identity = $identities->getByProvider('facebook', $details);
$this->assertEquals(2, $identity->user_id);
$this->assertEquals('facebook', $identity->provider);
Expand Down Expand Up @@ -66,7 +66,7 @@ public function test_get_by_provider_when_no_match()
'email' => 'john.doe@example.com',
'avatar' => 'http://example.com/photos/john_doe.jpg',
));
$identities = new IdentityStore;
$identities = new EloquentIdentityStore;
$identity = $identities->getByProvider('facebook', $details);
$this->assertNull($identity);
}
Expand All @@ -88,7 +88,7 @@ public function test_flush()

$this->assertEquals(1, OAuthIdentity::where('provider', 'facebook')->where('user_id', 2)->count());

$identities = new IdentityStore;
$identities = new EloquentIdentityStore;
$user = M::mock();
$user->shouldReceive('getKey')->andReturn(2);
$identities->flush($user, 'facebook');
Expand All @@ -107,7 +107,7 @@ public function test_store()

$this->assertEquals(0, OAuthIdentity::count());

$identities = new IdentityStore;
$identities = new EloquentIdentityStore;
$identities->store($identity);

$this->assertEquals(1, OAuthIdentity::count());
Expand All @@ -129,7 +129,7 @@ public function test_user_exists_returns_true_when_user_exists()
'email' => 'john.doe@example.com',
'avatar' => 'http://example.com/photos/john_doe.jpg',
));
$identities = new IdentityStore;
$identities = new EloquentIdentityStore;
$this->assertTrue($identities->userExists('facebook', $details));
}

Expand All @@ -149,7 +149,7 @@ public function test_user_exists_returns_false_when_user_doesnt_exist()
'email' => 'john.doe@example.com',
'avatar' => 'http://example.com/photos/john_doe.jpg',
));
$identities = new IdentityStore;
$identities = new EloquentIdentityStore;
$this->assertFalse($identities->userExists('facebook', $details));
}
}

0 comments on commit b0f115a

Please sign in to comment.