Skip to content

Commit

Permalink
Merge pull request #61 from BFoucher/feature/support-legal-user-creation
Browse files Browse the repository at this point in the history
support of UserLegal
  • Loading branch information
lenybernard authored Nov 23, 2017
2 parents 18437df + dd79ddf commit e8ee65e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
51 changes: 41 additions & 10 deletions Helper/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Troopers\MangopayBundle\Helper;

use Doctrine\ORM\EntityManager;
use MangoPay\UserLegal;
use MangoPay\UserNatural;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Troopers\MangopayBundle\Entity\UserInterface;
Expand All @@ -25,7 +26,13 @@ public function __construct(MangopayHelper $mangopayHelper, EntityManager $entit
$this->dispatcher = $dispatcher;
}

public function findOrCreateMangoUser(UserInterface $user)
/**
* @param UserInterface $user
* @param string $userType | NATURAL, BUSINESS, ORGANIZATION
*
* @return UserLegal|UserNatural
*/
public function findOrCreateMangoUser(UserInterface $user, $userType = 'NATURAL')
{
if ($mangoUserId = $user->getMangoUserId()) {
$mangoUser = $this->mangopayHelper->Users->get($mangoUserId);
Expand All @@ -36,24 +43,48 @@ public function findOrCreateMangoUser(UserInterface $user)
return $mangoUser;
}

public function createMangoUser(UserInterface $user)
/**
* @param UserInterface $user
* @param string $userType | NATURAL, BUSINESS, ORGANIZATION
*
* @return UserLegal|UserNatural
*/
public function createMangoUser(UserInterface $user, $userType = 'NATURAL')
{
$mangoUser = new UserNatural();
$mangoUser->Email = $user->getEmail();
$mangoUser->FirstName = $user->getFirstname();
$mangoUser->LastName = $user->getLastname();
$mangoUser->Birthday = $user->getBirthDate();
$mangoUser->Nationality = $user->getNationality();
$mangoUser->CountryOfResidence = $user->getCountry();
$mangoUser->Tag = $user->getId();
if (in_array($userType, ['BUSINESS', 'ORGANIZATION']) ) {
$mangoUser = new UserLegal();
$mangoUser->LegalPersonType = $userType;
$mangoUser->Name = $user->getLastname().' '.$user->getFirstname();
$mangoUser->Email = $user->getEmail();
$mangoUser->LegalRepresentativeFirstName = $user->getFirstname();
$mangoUser->LegalRepresentativeLastName = $user->getLastname();
$mangoUser->LegalRepresentativeBirthday = $user->getBirthDate();
$mangoUser->LegalRepresentativeNationality = $user->getNationality();
$mangoUser->LegalRepresentativeCountryOfResidence = $user->getCountry();
$mangoUser->Tag = $user->getId();
} elseif ($userType === 'NATURAL') {
$mangoUser = new UserNatural();
$mangoUser->Email = $user->getEmail();
$mangoUser->FirstName = $user->getFirstname();
$mangoUser->LastName = $user->getLastname();
$mangoUser->Birthday = $user->getBirthDate();
$mangoUser->Nationality = $user->getNationality();
$mangoUser->CountryOfResidence = $user->getCountry();
$mangoUser->Tag = $user->getId();
} else {
throw new \InvalidArgumentException(sprintf('Invalid argument, userType must be equal to NATURAL, BUSINESS or ORGANIZATION, %s given', $userType));
}


$mangoUser = $this->mangopayHelper->Users->Create($mangoUser);

$event = new UserEvent($user, $mangoUser);
$this->dispatcher->dispatch(TroopersMangopayEvents::NEW_USER, $event);

//@TODO: remove this or update Interface, setMangoUserId() is not implemented in UserInterface
$user->setMangoUserId($mangoUser->Id);

//@TODO: remove this, it's not bundle responsibility
$this->entityManager->persist($user);
$this->entityManager->flush();

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ It can create a new user in mangopay as the User object implements the UserInter
```php
$user = new User();
$this->get('troopers_mangopay.user_helper')->createMangoUser($user);

// UserLegal
$user = new User();
$this->get('troopers_mangopay.user_helper')->createMangoUser($user, 'BUSINESS');
// or
$this->get('troopers_mangopay.user_helper')->createMangoUser($user, 'ORGANIZATION');
```


WalletHelper
---
Expand Down

0 comments on commit e8ee65e

Please sign in to comment.