Skip to content

Commit

Permalink
[Core] Fix some issue in new OAuth provider implementation
Browse files Browse the repository at this point in the history
Remove useless dependencies on EntityManager, add new UserOAuthInterface, extend UserInterface
  • Loading branch information
stloyd committed May 9, 2014
1 parent 3ab85f1 commit 416776a
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 36 deletions.
38 changes: 35 additions & 3 deletions Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,44 @@ public function setEmailCanonical($emailCanonical)
}

/**
* Get connect OAuth accounts.
*
* @return Collection|UserOAuth[]
* {@inheritdoc}
*/
public function getOAuthAccounts()
{
return $this->oauthAccounts;
}

/**
* {@inheritdoc}
*/
public function getOAuthAccount($provider)
{
if ($this->oauthAccounts->isEmpty()) {
return null;
}

$filtered = $this->oauthAccounts->filter(function ($oauth) use ($provider) {
/** @var $oauth UserOAuthInterface */
return $provider === $oauth->getProvider();
});

if ($filtered->isEmpty()) {
return null;
}

return $filtered->current();
}

/**
* {@inheritdoc}
*/
public function addOAuthAccount(UserOAuthInterface $oauth)
{
if (!$this->oauthAccounts->contains($oauth)) {
$this->oauthAccounts->add($oauth);
$oauth->setUser($this);
}

return $this;
}
}
25 changes: 25 additions & 0 deletions Model/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,29 @@ public function removeAddress(AddressInterface $address);
* @return Boolean
*/
public function hasAddress(AddressInterface $address);

/**
* Get connected OAuth accounts.
*
* @return Collection|UserOAuthInterface[]
*/
public function getOAuthAccounts();

/**
* Get connected OAuth account.
*
* @param string $provider
*
* @return null|UserOAuthInterface
*/
public function getOAuthAccount($provider);

/**
* Connect OAuth account.
*
* @param UserOAuthInterface $oauth
*
* @return self
*/
public function addOAuthAccount(UserOAuthInterface $oauth);
}
85 changes: 85 additions & 0 deletions Model/UserOAuthInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Component\Core\Model;

/**
* User OAuth account interface.
*
* @author Sergio Marchesini
* @author Joseph Bielawski <stloyd@gmail.com>
*/
interface UserOAuthInterface
{
/**
* Get OAuth provider name.
*
* @return string
*/
public function getProvider();

/**
* Set OAuth provider name.
*
* @param string $provider
*
* @return self
*/
public function setProvider($provider);

/**
* Get OAuth identifier.
*
* @return string
*/
public function getIdentifier();

/**
* Set OAuth identifier.
*
* @param string $identifier
*
* @return self
*/
public function setIdentifier($identifier);

/**
* Get OAuth access token.
*
* @return string
*/
public function getAccessToken();

/**
* Set OAuth access token.
*
* @param string $accessToken
*
* @return self
*/
public function setAccessToken($accessToken);

/**
* Get user
*
* @return UserInterface
*/
public function getUser();

/**
* Set user.
*
* @param UserInterface $user
*
* @return self
*/
public function setUser(UserInterface $user);
}
43 changes: 10 additions & 33 deletions Model/UserOauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* User OAuth model.
*
* @author Sergio Marchesini
* @author Joseph Bielawski <stloyd@gmail.com>
*/
class UserOAuth
class UserOAuth implements UserOAuthInterface
{
protected $id;
protected $provider;
Expand All @@ -39,21 +40,15 @@ public function getId()
}

/**
* Get OAuth provider name.
*
* @return string
* {@inheritdoc}
*/
public function getProvider()
{
return $this->provider;
}

/**
* Set OAuth provider name.
*
* @param string $provider
*
* @return self
* {@inheritdoc}
*/
public function setProvider($provider)
{
Expand All @@ -63,21 +58,15 @@ public function setProvider($provider)
}

/**
* Get OAuth identifier.
*
* @return string
* {@inheritdoc}
*/
public function getIdentifier()
{
return $this->identifier;
}

/**
* Set OAuth identifier.
*
* @param string $identifier
*
* @return self
* {@inheritdoc}
*/
public function setIdentifier($identifier)
{
Expand All @@ -87,21 +76,15 @@ public function setIdentifier($identifier)
}

/**
* Get OAuth access token.
*
* @return string
* {@inheritdoc}
*/
public function getAccessToken()
{
return $this->accessToken;
}

/**
* Set OAuth access token.
*
* @param string $accessToken
*
* @return self
* {@inheritdoc}
*/
public function setAccessToken($accessToken)
{
Expand All @@ -111,21 +94,15 @@ public function setAccessToken($accessToken)
}

/**
* Get user
*
* @return UserInterface
* {@inheritdoc}
*/
public function getUser()
{
return $this->user;
}

/**
* Set user.
*
* @param UserInterface $user
*
* @return self
* {@inheritdoc}
*/
public function setUser(UserInterface $user)
{
Expand Down

0 comments on commit 416776a

Please sign in to comment.