Skip to content

Commit

Permalink
Merge pull request #161 from loic425/features/improve-user-and-custom…
Browse files Browse the repository at this point in the history
…er-entities

Improve user and customer entities
  • Loading branch information
loic425 committed Mar 20, 2019
2 parents 4989f39 + 4f83ce5 commit 877e5ce
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 62 deletions.
50 changes: 50 additions & 0 deletions spec/App/Entity/AdminUserSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace spec\App\Entity;

use App\Entity\AdminUser;
use App\Entity\AdminUserInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\User\Model\User;
use Sylius\Component\User\Model\UserInterface;

class AdminUserSpec extends ObjectBehavior
{
function it_extends_a_base_user_model(): void
{
$this->shouldHaveType(User::class);
}

function it_implements_an_admin_user_interface(): void
{
$this->shouldImplement(AdminUserInterface::class);
}

function it_implements_a_user_interface(): void
{
$this->shouldImplement(UserInterface::class);
}

function it_has_no_first_name_by_default(): void
{
$this->getFirstName()->shouldReturn(null);
}

function its_first_name_is_mutable(): void
{
$this->setFirstName('John');
$this->getFirstName()->shouldReturn('John');
}

function it_has_no_last_name_by_default(): void
{
$this->getLastName()->shouldReturn(null);
}

function its_last_name_is_mutable(): void
{
$this->setLastName('Doe');
$this->getLastName()->shouldReturn('Doe');
}
}
7 changes: 6 additions & 1 deletion spec/App/Entity/AppUserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

namespace spec\App\Entity;

use App\Entity\AppUser;
use App\Entity\AppUserInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Customer\Model\CustomerInterface;
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Sylius\Component\User\Model\User as BaseUser;

class AppUserSpec extends ObjectBehavior
{
function it_implements_an_app_user_interface(): void
{
$this->shouldImplement(AppUserInterface::class);
}

function it_extends_a_user_model(): void
{
$this->shouldHaveType(BaseUser::class);
Expand Down
42 changes: 37 additions & 5 deletions spec/App/Entity/CustomerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,57 @@
namespace spec\App\Entity;

use App\Entity\AppUser;
use App\Entity\Customer;
use App\Entity\AppUserInterface;
use App\Entity\CustomerInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Customer\Model\Customer as BaseCustomer;
use Sylius\Component\User\Model\UserInterface;

class CustomerSpec extends ObjectBehavior
{
function it_is_initializable()
function it_implements_a_customer_interface(): void
{
$this->shouldHaveType(Customer::class);
$this->shouldImplement(CustomerInterface::class);
}

function it_extends_base_customer()
function it_extends_a_base_customer_model(): void
{
$this->shouldBeAnInstanceOf(BaseCustomer::class);
}

function its_user_is_mutable(AppUser $user)
function it_has_no_user_by_default(): void
{
$this->getUser()->shouldReturn(null);
}

function its_user_is_mutable(AppUserInterface $user): void
{
$user->setCustomer($this)->shouldBeCalled();

$this->setUser($user);
$this->getUser()->shouldReturn($user);
}

function it_throws_an_invalid_argument_exception_when_user_is_not_an_app_user_type(UserInterface $user)
{
$this->shouldThrow(\InvalidArgumentException::class)->during('setUser', [$user]);
}

function it_resets_customer_of_previous_user(AppUserInterface $previousUser, AppUserInterface $user)
{
$this->setUser($previousUser);

$previousUser->setCustomer(null)->shouldBeCalled();

$this->setUser($user);
}

function it_does_not_replace_user_if_it_is_already_set(AppUserInterface $user)
{
$user->setCustomer($this)->shouldBeCalledOnce();

$this->setUser($user);
$this->setUser($user);
}
}
28 changes: 9 additions & 19 deletions src/Entity/AdminUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
* @ORM\Entity
* @ORM\Table("sylius_admin_user")
*/
class AdminUser extends BaseUser
class AdminUser extends BaseUser implements AdminUserInterface
{
const DEFAULT_ADMIN_ROLE = 'ROLE_ADMIN';

/**
* @var string
*
Expand All @@ -47,42 +45,34 @@ public function __construct()
}

/**
* @return string
* {@inheritdoc}
*/
public function getLastName()
public function getLastName(): ?string
{
return $this->lastName;
}

/**
* @param string $lastName
*
* @return $this
* {@inheritdoc}
*/
public function setLastName($lastName)
public function setLastName(?string $lastName): void
{
$this->lastName = $lastName;

return $this;
}

/**
* @return string
* {@inheritdoc}
*/
public function getFirstName()
public function getFirstName(): ?string
{
return $this->firstName;
}

/**
* @param string $firstName
*
* @return $this
* {@inheritdoc}
*/
public function setFirstName($firstName)
public function setFirstName(?string $firstName): void
{
$this->firstName = $firstName;

return $this;
}
}
41 changes: 41 additions & 0 deletions src/Entity/AdminUserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of AppName.
*
* (c) Monofony
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Entity;

use Sylius\Component\User\Model\UserInterface as BaseUserInterface;

interface AdminUserInterface extends BaseUserInterface
{
public const DEFAULT_ADMIN_ROLE = 'ROLE_ADMIN';

/**
* @return string|null
*/
public function getFirstName(): ?string;

/**
* @param string|null $firstName
*/
public function setFirstName(?string $firstName): void;

/**
* @return string|null
*/
public function getLastName(): ?string;

/**
* @param string|null $lastName
*/
public function setLastName(?string $lastName): void;
}
26 changes: 6 additions & 20 deletions src/Entity/AppUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,30 @@
* @ORM\Entity
* @ORM\Table(name="sylius_app_user")
*/
class AppUser extends BaseUser
class AppUser extends BaseUser implements AppUserInterface
{
/**
* @var CustomerInterface
*
* @ORM\OneToOne(targetEntity="Sylius\Component\Customer\Model\CustomerInterface", inversedBy="user", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
protected $customer;
private $customer;

/**
* @return CustomerInterface
* {@inheritdoc}
*/
public function getCustomer()
public function getCustomer(): ?CustomerInterface
{
return $this->customer;
}

/**
* @param CustomerInterface $customer
*
* @return $this
* {@inheritdoc}
*/
public function setCustomer($customer)
public function setCustomer($customer): void
{
$this->customer = $customer;

return $this;
}

/**
Expand Down Expand Up @@ -97,14 +93,4 @@ public function setEmailCanonical(?string $emailCanonical): void

$this->customer->setEmailCanonical($emailCanonical);
}

/**
* @param Customer $customer
*/
protected function assignUser(Customer $customer = null)
{
if (null !== $customer) {
$customer->setUser($this);
}
}
}
21 changes: 21 additions & 0 deletions src/Entity/AppUserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of AppName.
*
* (c) Monofony
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Entity;

use Sylius\Component\Customer\Model\CustomerAwareInterface;
use Sylius\Component\User\Model\UserInterface;

interface AppUserInterface extends UserInterface, CustomerAwareInterface
{
}
31 changes: 14 additions & 17 deletions src/Entity/Customer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of Monofony.
* This file is part of AppName.
*
* (c) Monofony
*
Expand All @@ -13,15 +13,14 @@

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Customer\Model\Customer as BaseCustomer;
use Sylius\Component\User\Model\UserAwareInterface;
use Sylius\Component\User\Model\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
* @ORM\Table(name="sylius_customer")
*/
class Customer extends BaseCustomer implements UserAwareInterface
class Customer extends BaseCustomer implements CustomerInterface
{
/**
* @var AppUser
Expand All @@ -42,25 +41,23 @@ public function getUser(): ?UserInterface

/**
* @param UserInterface|null $user
*
* @return $this
*/
public function setUser(?UserInterface $user)
public function setUser(?UserInterface $user): void
{
if ($this->user !== $user) {
$this->user = $user;
$this->assignCustomer($user);
if ($this->user === $user) {
return;
}

return $this;
}
\Webmozart\Assert\Assert::nullOrIsInstanceOf($user, AppUserInterface::class);

/**
* @param AppUser|null $user
*/
protected function assignCustomer($user = null)
{
if (null !== $user) {
$previousUser = $this->user;
$this->user = $user;

if ($previousUser instanceof AppUserInterface) {
$previousUser->setCustomer(null);
}

if ($user instanceof AppUserInterface) {
$user->setCustomer($this);
}
}
Expand Down

0 comments on commit 877e5ce

Please sign in to comment.