-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from irontec/CDD-4-activate-administrators
Activate administrators
- Loading branch information
Showing
13 changed files
with
277 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Controller\Administrator; | ||
|
||
use Demo\Application\Service\Administrator\ActivateAdministrator; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class Activate | ||
{ | ||
public function __construct( | ||
private ActivateAdministrator $activateAdministrator | ||
) { | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
$administratorId = (int) $request->get('id'); | ||
|
||
$this->activateAdministrator->execute($administratorId); | ||
|
||
return new Response('Administrator activated', 200); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/App/Controller/Administrator/SendActivationEmail.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Controller\Administrator; | ||
|
||
use Demo\Domain\Model\Administrator\AdministratorRepository; | ||
use Demo\Domain\Service\Administrator\SendActivationEmailInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
class SendActivationEmail | ||
{ | ||
public function __construct( | ||
private SendActivationEmailInterface $sendActivationEmail, | ||
private AdministratorRepository $administratorRepository | ||
) { | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
$response = new Response(); | ||
|
||
$administratorId = (int) $request->attributes->get('id'); | ||
|
||
$administrator = $this->administratorRepository->find($administratorId); | ||
if ($administrator === null) { | ||
throw new \DomainException("Resource not found", 404); | ||
} | ||
|
||
$this->sendActivationEmail->execute($administrator); | ||
$response->setStatusCode(200); | ||
|
||
return $response; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/Demo/Application/Service/Administrator/ActivateAdministrator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Demo\Application\Service\Administrator; | ||
|
||
use Demo\Domain\Model\Administrator\AdministratorDto; | ||
use Demo\Domain\Model\Administrator\AdministratorRepository; | ||
use Ivoz\Core\Domain\Service\EntityTools; | ||
|
||
class ActivateAdministrator | ||
{ | ||
public function __construct( | ||
private AdministratorRepository $administratorRepository, | ||
private EntityTools $entityTools | ||
) { | ||
} | ||
|
||
public function execute(int $administratorId): void | ||
{ | ||
$administrator = $this->administratorRepository->find($administratorId); | ||
|
||
if ($administrator === null) { | ||
throw new \DomainException('Administrator not found.', 404); | ||
} | ||
|
||
/** @var AdministratorDto $administratorDto */ | ||
$administratorDto = $this->entityTools->entityToDto($administrator); | ||
$administratorDto->setActive(1); | ||
$this->entityTools->persistDto($administratorDto, $administrator); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/Demo/Domain/Service/Administrator/AdministratorLifecycleEventHandlerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Demo\Domain\Service\Administrator; | ||
|
||
use Demo\Domain\Model\Administrator\AdministratorInterface; | ||
use Ivoz\Core\Domain\Service\LifecycleEventHandlerInterface; | ||
|
||
interface AdministratorLifecycleEventHandlerInterface extends LifecycleEventHandlerInterface | ||
{ | ||
public function execute(AdministratorInterface $administrator): void; | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/Demo/Domain/Service/Administrator/AdministratorLifecycleServiceCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Demo\Domain\Service\Administrator; | ||
|
||
use Ivoz\Core\Domain\Assert\Assertion; | ||
use Ivoz\Core\Domain\Model\EntityInterface; | ||
use Ivoz\Core\Domain\Service\DomainEventSubscriberInterface; | ||
use Ivoz\Core\Domain\Service\LifecycleEventHandlerInterface; | ||
use Ivoz\Core\Domain\Service\LifecycleServiceCollectionInterface; | ||
use Ivoz\Core\Domain\Service\LifecycleServiceCollectionTrait; | ||
|
||
class AdministratorLifecycleServiceCollection implements LifecycleServiceCollectionInterface | ||
{ | ||
use LifecycleServiceCollectionTrait; | ||
|
||
/** @var array<array-key, array> $bindedBaseServices */ | ||
public static $bindedBaseServices = [ | ||
"on_commit" => | ||
[ | ||
\Demo\Domain\Service\Administrator\SendActivationEmail::class => 200, | ||
], | ||
]; | ||
|
||
protected function addService(string $event, LifecycleEventHandlerInterface|DomainEventSubscriberInterface $service): void | ||
{ | ||
Assertion::isInstanceof($service, AdministratorLifecycleEventHandlerInterface::class); | ||
$this->services[$event][] = $service; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/Demo/Domain/Service/Administrator/SendActivationEmail.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Demo\Domain\Service\Administrator; | ||
|
||
use Demo\Domain\Model\Administrator\AdministratorInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class SendActivationEmail implements AdministratorLifecycleEventHandlerInterface | ||
{ | ||
public const ON_COMMIT_PRIORITY = self::PRIORITY_NORMAL; | ||
|
||
public function __construct( | ||
private SendActivationEmailInterface $sendActivationEmail, | ||
private LoggerInterface $logger | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<string, int> | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
self::EVENT_ON_COMMIT => self::ON_COMMIT_PRIORITY | ||
]; | ||
} | ||
|
||
/** | ||
* @throws \DomainException | ||
*/ | ||
public function execute(AdministratorInterface $administrator): void | ||
{ | ||
$isNew = $administrator->isNew(); | ||
if (!$isNew) { | ||
return; | ||
} | ||
|
||
$isActive = $administrator->getActive(); | ||
if ($isActive) { | ||
return; | ||
} | ||
|
||
$this->sendActivationEmail->execute($administrator); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/Demo/Domain/Service/Administrator/SendActivationEmailInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Demo\Domain\Service\Administrator; | ||
|
||
use Demo\Domain\Model\Administrator\AdministratorInterface; | ||
|
||
interface SendActivationEmailInterface | ||
{ | ||
public function execute(AdministratorInterface $administrator): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,4 @@ public function findByUsername(string $username): ?AdministratorInterface | |
|
||
return $administrator; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,4 @@ public function __construct( | |
$entityPersisterInterface | ||
); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/Demo/Infrastructure/Service/Administrator/SendActivationEmail.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Demo\Infrastructure\Service\Administrator; | ||
|
||
use Demo\Domain\Model\Administrator\AdministratorInterface; | ||
use Demo\Domain\Model\Administrator\AdministratorRepository; | ||
use Demo\Domain\Service\Administrator\SendActivationEmailInterface; | ||
use Ivoz\Core\Domain\Model\Mailer\Message; | ||
use Ivoz\Core\Domain\Service\MailerClientInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class SendActivationEmail implements SendActivationEmailInterface | ||
{ | ||
public function __construct( | ||
private MailerClientInterface $mailer, | ||
private LoggerInterface $logger | ||
) { | ||
} | ||
|
||
public function execute(AdministratorInterface $administrator): void | ||
{ | ||
$administratorId = $administrator->getId(); | ||
$body = 'Hello, please activate your account following this link: ' . PHP_EOL | ||
. 'https://10.189.4.23/demo/api/activate_admin/' . $administratorId . PHP_EOL; | ||
|
||
$mail = new Message(); | ||
$mail->setBody($body, 'text/plain') | ||
->setSubject('Activate your new account') | ||
->setFromAddress('[email protected]') | ||
->setFromName('Irontec demo') | ||
->setToAddress($administrator->getEmail()); | ||
|
||
try { | ||
$this->mailer->send($mail); | ||
} catch (\Exception $e) { | ||
$errorMsg = 'Unable to send activation email'; | ||
$this->logger->error($errorMsg . ':' . $e->getMessage()); | ||
|
||
throw new \DomainException( | ||
$errorMsg, | ||
$e->getCode(), | ||
$e | ||
); | ||
} | ||
} | ||
} |