Skip to content

Commit

Permalink
Register a new app user
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Oct 3, 2020
1 parent 2203844 commit 0aec2ac
Show file tree
Hide file tree
Showing 46 changed files with 377 additions and 77 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"App\\Entity\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/Entity/",
"App\\EventListener\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/EventListener/",
"App\\EventSubscriber\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/EventSubscriber/",
"App\\Fixture\\": "src/Monofony/Plugin/FixturesPlugin/Recipe/src/Fixture/",
"App\\Fixture\\": "src/Monofony/Pack/FixturesPack/Recipe/src/Fixture/",
"App\\Form\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/Form/",
"App\\Formatter\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/Formatter/",
"App\\Installer\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/Installer/",
"App\\Installer\\Checker\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/Installer/Checker/",
"App\\Message\\": "src/Monofony/Pack/ApiPack/Recipe/src/Message/",
"App\\MessageHandler\\": "src/Monofony/Pack/ApiPack/Recipe/src/MessageHandler/",
"App\\Migrations\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/Migrations/",
"App\\Repository\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/Repository/",
"App\\Validator\\": "src/Monofony/Bundle/CoreBundle/Recipe/src/Validator/",
Expand Down Expand Up @@ -48,7 +50,7 @@
"App\\Tests\\Behat\\Exception\\": "src/Monofony/Bundle/CoreBundle/Recipe/tests/Behat/Exception/",
"App\\Tests\\Behat\\Page\\Frontend\\": "src/Monofony/Bundle/FrontBundle/Recipe/tests/Behat/Page/Frontend/",
"App\\Tests\\Behat\\Service\\": "src/Monofony/Bundle/CoreBundle/Recipe/tests/Behat/Service/",
"App\\Tests\\Controller\\": "src/Monofony/Bundle/ApiBundle/Recipe/tests/Controller/"
"App\\Tests\\Controller\\": "src/Monofony/Pack/ApiPack/Recipe/tests/Controller/"
}
},
"require": {
Expand Down
2 changes: 0 additions & 2 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,4 @@
Monofony\Bundle\CoreBundle\MonofonyCoreBundle::class => ['all' => true],
Monofony\Bundle\AdminBundle\MonofonyAdminBundle::class => ['all' => true],
Monofony\Bundle\FrontBundle\MonofonyFrontBundle::class => ['all' => true],
Monofony\Bundle\ApiBundle\MonofonyApiBundle::class => ['all' => true],
Monofony\Plugin\FixturesPlugin\MonofonyFixturesPlugin::class => ['all' => true],
];
4 changes: 3 additions & 1 deletion config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
api_platform:
version: !php/const App\Kernel::VERSION
mapping:
paths: ['%kernel.project_dir%/src/Monofony/Bundle/CoreBundle/Recipe/src/Entity']
paths:
- '%kernel.project_dir%/src/Monofony/Bundle/CoreBundle/Recipe/src/Entity'
- '%kernel.project_dir%/src/Monofony/Pack/ApiPack/Recipe/config/api_platform/resources'
patch_formats:
json: ['application/merge-patch+json']
swagger:
Expand Down
6 changes: 5 additions & 1 deletion config/packages/framework.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
framework:
#esi: ~
translator: { fallbacks: ["%locale%"] }
translator:
fallbacks: ["%locale%"]
paths:
- '%kernel.project_dir%/src/Monofony/Bundle/CoreBundle/Recipe/translations'
secret: '%env(APP_SECRET)%'
form: ~
csrf_protection: ~
Expand All @@ -17,3 +20,4 @@ framework:
http_method_override: true
php_errors:
log: true

2 changes: 2 additions & 0 deletions config/packages/monofony_fixtures.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: "../../src/Monofony/Pack/FixturesPack/Recipe/config/packages/monofony_fixtures.yaml" }
2 changes: 0 additions & 2 deletions config/packages/monofony_fixtures_plugin.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ app_frontend:
resource: "../src/Monofony/Bundle/FrontBundle/Recipe/config/routes/frontend.yaml"

app_api:
resource: "../src/Monofony/Bundle/ApiBundle/Recipe/config/routes/api.yaml"
resource: "../src/Monofony/Pack/ApiPack/Recipe/config/routes/api.yaml"
4 changes: 2 additions & 2 deletions config/services/monofony_fixtures.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ services:

app_fixture:
namespace: App\
resource: '../../src/Monofony/Plugin/FixturesPlugin/Recipe/src/*'
exclude: '../../src/Monofony/Plugin/FixturesPlugin/Recipe/src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
resource: '../../src/Monofony/Pack/FixturesPack/Recipe/src/*'
exclude: '../../src/Monofony/Pack/FixturesPack/Recipe/src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
20 changes: 0 additions & 20 deletions src/Monofony/Bundle/ApiBundle/MonofonyApiBundle.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of mz_067_s_ccpa_thermotool.
*
* (c) Mobizel
*
* 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\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
final class UniqueAppUserEmail extends Constraint
{
/** @var string */
public $message = 'sylius.user.email.unique';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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.
*/

declare(strict_types=1);

namespace App\Validator\Constraints;

use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;

final class UniqueAppUserEmailValidator extends ConstraintValidator
{
private $canonicalizer;
private $appUserRepository;

public function __construct(CanonicalizerInterface $canonicalizer, UserRepositoryInterface $appUserRepository)
{
$this->canonicalizer = $canonicalizer;
$this->appUserRepository = $appUserRepository;
}

public function validate($value, Constraint $constraint): void
{
if (null === $value) {
return;
}

/* @var UniqueAppUserEmail $constraint */
Assert::isInstanceOf($constraint, UniqueAppUserEmail::class);

$emailCanonical = $this->canonicalizer->canonicalize($value);
$shopUser = $this->appUserRepository->findOneByEmail($emailCanonical);

if (null === $shopUser) {
return;
}

$this->context->addViolation($constraint->message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
user:
email:
unique: This email is already used.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
user:
email:
unique: Cet e-mail est déjà utilisé.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
App\Message\RegisterAppUser:
attributes:
messenger: true
output: false
validation_groups: ["Default", "sylius"]
itemOperations: []
collectionOperations:
post:
path: /register
openapi_context:
summary: Registers an app user
validation_groups: ["Default", "sylius"]
77 changes: 77 additions & 0 deletions src/Monofony/Pack/ApiPack/Recipe/src/Message/RegisterAppUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of mz_067_s_ccpa_thermotool.
*
* (c) Mobizel
*
* 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\Message;

use Symfony\Component\Validator\Constraints as Assert;
use App\Validator\Constraints as CustomConstraints;

final class RegisterAppUser
{
/**
* @var string
*
* @Assert\NotBlank(message="sylius.customer.first_name.not_blank")
*/
public $firstName;

/**
* @var string
*
* @Assert\NotBlank(message="sylius.customer.last_name.not_blank")
*/
public $lastName;

/**
* @var string
*
* @Assert\NotBlank(message="sylius.customer.email.not_blank")
* @Assert\Email(mode="strict", message="sylius.customer.email.invalid")
* @Assert\Length(
* max=254,
* maxMessage="sylius.customer.email.max"
* )
* @CustomConstraints\UniqueAppUserEmail()
*/
public $email;

/**
* @var string
*
* @Assert\NotBlank(message="sylius.user.plainPassword.not_blank")
* @Assert\Length(
* min=4,
* minMessage="sylius.user.password.min",
* max=254,
* maxMessage="sylius.user.password.max"
* )
*/
public $password;

/** @var string|null */
public $phoneNumber;

public function __construct(
string $firstName,
string $lastName,
string $email,
string $password,
?string $phoneNumber = null
) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->password = $password;
$this->phoneNumber = $phoneNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of mz_067_s_ccpa_thermotool.
*
* (c) Mobizel
*
* 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\MessageHandler;

use App\Message\RegisterAppUser;
use App\Provider\CustomerProviderInterface;
use Monofony\Component\Core\Model\User\AppUserInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class RegisterAppUserHandler implements MessageHandlerInterface
{
private $appUserFactory;
private $appUserRepository;
private $customerProvider;

public function __construct(
FactoryInterface $appUserFactory,
RepositoryInterface $appUserRepository,
CustomerProviderInterface $customerProvider
) {
$this->appUserFactory = $appUserFactory;
$this->appUserRepository = $appUserRepository;
$this->customerProvider = $customerProvider;
}

public function __invoke(RegisterAppUser $command): void
{
/** @var AppUserInterface $user */
$user = $this->appUserFactory->createNew();
$user->setPlainPassword($command->password);

$customer = $this->customerProvider->provide($command->email);
if ($customer->getUser() !== null) {
throw new \DomainException(sprintf('User with email "%s" is already registered.', $command->email));
}

$customer->setFirstName($command->firstName);
$customer->setLastName($command->lastName);
$customer->setPhoneNumber($command->phoneNumber);
$customer->setUser($user);

$this->appUserRepository->add($user);
}
}
Loading

0 comments on commit 0aec2ac

Please sign in to comment.