Skip to content

Commit

Permalink
feature: add a controller to change a user password
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimorillo committed Mar 17, 2024
1 parent 2ce9067 commit c9d0fd3
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 19 deletions.
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions configuration/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use Source\Shared\EventBus\EventRepository;
use Source\Shared\EventBus\EventRepositoryInMySQL;
use Source\Shared\MysqlClient\MysqlClient;
use Source\User\Domain\ValueObject\UserRepositoryInterface;
use Source\User\Infrastructure\Repository\UserRepositoryInMySQL;

return [
EventRepository::class => new EventRepositoryInMySQL(MysqlClient::getConnection()),
UserRepositoryInterface::class => new UserRepositoryInMySQL(MysqlClient::getConnection()),
];
9 changes: 5 additions & 4 deletions configuration/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// User Routes
global $app;

use Source\User\Application\Controller\ChangePasswordController;
use Source\User\Application\Controller\CreateAUserController;

// Add the routes here like in the following example:
$app->post('/user', \Source\User\Application\Controller\CreateAUserController::class);
//$app->delete('/example/{ExampleId}', DeleteAnExampleController::class);
//$app->put('/example/{exampleId}', UpdateAnExampleController::class);
//$app->get('/example/criteria/the-criteria', FindAnExampleByCriteriaController::class);
$app->post('/user', CreateAUserController::class);
$app->put('/user', ChangePasswordController::class);
1 change: 0 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<?php

require dirname(__DIR__) . '/src/Shared/kernel.php';
14 changes: 12 additions & 2 deletions src/Shared/kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
declare(strict_types = 1);

use DI\ContainerBuilder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Factory\AppFactory;
use Source\Shared\DependencyInjector;
use Source\Shared\MysqlClient\MysqlClient;
Expand All @@ -13,7 +16,7 @@
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__, 2));
$dotenv->load();

MysqlClient::connect($_ENV['MYSQL_HOST'], $_ENV['MYSQL_USER'], $_ENV['MYSQL_PASS'], $_ENV['MYSQL_PORT']);
MysqlClient::connect($_ENV['MYSQL_HOST'], $_ENV['MYSQL_USER'], $_ENV['MYSQL_PASS'], (int)$_ENV['MYSQL_PORT']);
MysqlClient::selectDatabase($_ENV['MYSQL_DB']);

$dependencyInjector = new DependencyInjector();
Expand All @@ -27,7 +30,14 @@
$app = AppFactory::create();

$app->addRoutingMiddleware();
$errorMiddleware = $app->addErrorMiddleware(false, true, true);
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
$app->add(function (ServerRequestInterface $request, RequestHandlerInterface $requestHandler) {
$response = $requestHandler->handle($request);
$response->withHeader('Content-Type', 'application/json');
$response->withStatus(404);
$response->getBody()->write(json_encode([]));
return $response;
});

require dirname(__DIR__, 2) . '/configuration/routes.php';

Expand Down
37 changes: 37 additions & 0 deletions src/User/Application/Controller/ChangePasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);

namespace Source\User\Application\Controller;

use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Source\Shared\Application\Controller;
use Source\Shared\CQRS\CommandBus\CommandBus;
use Source\User\Application\Command\ChangePasswordCommand;

class ChangePasswordController implements Controller
{
private CommandBus $commandBus;

public function __construct(CommandBus $commandBus)
{
$this->commandBus = $commandBus;
}

public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
array $args
): ResponseInterface {
try {
$params = $request->getParams();
$command = new ChangePasswordCommand($params['email'], $params['password']);
$this->commandBus->handle($command);
return $response->withStatus(200);
} catch (Exception $e) {
return $response->withStatus($e->getCode(), $e->getMessage());
}
}
}
14 changes: 9 additions & 5 deletions src/User/Application/Controller/CreateAUserController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace Source\User\Application\Controller;

Expand All @@ -15,14 +15,18 @@ class CreateAUserController implements Controller
{
private CommandBus $commandBus;

public function __construct(CommandBus $commandBus) {
public function __construct(CommandBus $commandBus)
{
$this->commandBus = $commandBus;
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
array $args
): ResponseInterface {
try {
$parameters = json_decode($request->getBody()->getContents(), true);
$parameters = $request->getParams();
$command = new CreateUserCommand($parameters['email'], $parameters['password']);
$this->commandBus->handle($command);
return $response->withStatus(200);
Expand Down
27 changes: 27 additions & 0 deletions tests/User/Application/Controller/ChangePasswordControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types = 1);

namespace Tests\User\Application\Controller;

use Source\Shared\MysqlClient\MysqlClient;
use Source\User\Infrastructure\Repository\UserRepositoryInMySQL;
use Tests\Acceptance\AcceptanceTestCase;
use Tests\Fixtures\Users;

class ChangePasswordControllerTest extends AcceptanceTestCase
{
public function testCanChangeAPassword(): void
{
$user = Users::aUser();
$userRepository = new UserRepositoryInMySQL(MysqlClient::getConnection());
$userRepository->save($user);
$response = $this->client->put('/user', [
'form_params' => [
'email' => $user->getEmail()->toString(),
'password' => 'changed-password',
],
]);
self::assertEquals(200, $response->getStatusCode());
}
}

0 comments on commit c9d0fd3

Please sign in to comment.